Changeset 3584
- Timestamp:
- 04/02/08 14:49:12 (4 years ago)
- Files:
-
- trunk/build.properties (modified) (1 diff)
- trunk/ivy.xml (modified) (1 diff)
- trunk/src/main/java/org/sarugo/xtc/restlet/jpa/TemplateConverter.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/build.properties
r3013 r3584 1 1 project.name=xtc 2 2 project.rev=0.4.1 3 project.status= integration3 project.status=release trunk/ivy.xml
r3011 r3584 15 15 <dependencies> 16 16 <dependency org="de.odysseus.juel" name="juel" rev="2.1.0" conf="default"/> 17 <dependency org="sarugo" name="restlet-jpa" rev=" latest.${project.status}" conf="restlet,compile->default"/>18 <dependency org="org.restlet" name="org.restlet" rev="1. 0.4" conf="restlet,compile->default"/>19 <dependency org="com.noelios.restlet" name="com.noelios.restlet.ext.simple" rev="1. 0.4" conf="compile->default"/>17 <dependency org="sarugo" name="restlet-jpa" rev="0.2" conf="restlet,compile->default"/> 18 <dependency org="org.restlet" name="org.restlet" rev="1.1-SNAPSHOT" conf="restlet,compile->default"/> 19 <dependency org="com.noelios.restlet" name="com.noelios.restlet.ext.simple" rev="1.1-SNAPSHOT" conf="compile->default"/> 20 20 <dependency org="javax.servlet" name="servlet-api" rev="2.5" conf="compile->default" /> 21 <dependency org="com.noelios.restlet" name="com.noelios.restlet.ext.net" rev="1. 0.4" conf="test->default"/>21 <dependency org="com.noelios.restlet" name="com.noelios.restlet.ext.net" rev="1.1-SNAPSHOT" conf="test->default"/> 22 22 <dependency org="junit" name="junit" rev="3.8.1" conf="test->default"/> 23 23 <dependency org="xmlunit" name="xmlunit" rev="1.0" conf="test->default"/> trunk/src/main/java/org/sarugo/xtc/restlet/jpa/TemplateConverter.java
r2958 r3584 18 18 19 19 import java.io.IOException; 20 import java.util.Collection; 21 import java.util.Collections; 20 22 21 23 import javax.el.ELException; … … 24 26 import org.restlet.resource.Representation; 25 27 import org.restlet.resource.Variant; 26 import org.sarugo.restlet.jpa.PersistenceConverter; 27 import org.sarugo.restlet.jpa.PersistenceResource; 28 import org.sarugo.restlet.jpa.converter.OutputConverter; 29 import org.sarugo.restlet.jpa.resource.EntityCollection; 30 import org.sarugo.restlet.jpa.resource.EntityInstance; 31 import org.sarugo.restlet.jpa.resource.EntityResource; 28 32 import org.sarugo.xtc.Template; 29 33 import org.sarugo.xtc.TemplateException; … … 31 35 import org.sarugo.xtc.restlet.TemplateRepresentation; 32 36 33 public class TemplateConverter extends PersistenceConverter{37 public class TemplateConverter<E> implements OutputConverter<E> { 34 38 35 private Template template;39 private Template template; 36 40 37 private MediaType mediaType;41 private MediaType mediaType; 38 42 39 private Template listTemplate;43 private Template listTemplate; 40 44 41 public TemplateConverter(String listTemplate) throws TemplateException,42 ELException, IOException {43 this(listTemplate, MediaType.APPLICATION_XHTML_XML);44 }45 public TemplateConverter(String listTemplate) throws TemplateException, 46 ELException, IOException { 47 this(listTemplate, MediaType.APPLICATION_XHTML_XML); 48 } 45 49 46 public TemplateConverter(String listTemplate, MediaType mediaType)47 throws TemplateException, ELException, IOException {48 this(listTemplate, null, mediaType);49 }50 public TemplateConverter(String listTemplate, MediaType mediaType) 51 throws TemplateException, ELException, IOException { 52 this(listTemplate, null, mediaType); 53 } 50 54 51 public TemplateConverter(String listTemplate, String template)52 throws TemplateException, ELException, IOException {53 this(listTemplate, template, MediaType.APPLICATION_XHTML_XML);54 }55 public TemplateConverter(String listTemplate, String template) 56 throws TemplateException, ELException, IOException { 57 this(listTemplate, template, MediaType.APPLICATION_XHTML_XML); 58 } 55 59 56 public TemplateConverter(String listTemplate, String template,57 MediaType mediaType) throws TemplateException, ELException,58 IOException {59 setListTemplate(listTemplate);60 setTemplate(template);61 this.mediaType = mediaType;62 }60 public TemplateConverter(String listTemplate, String template, 61 MediaType mediaType) throws TemplateException, ELException, 62 IOException { 63 setListTemplate(listTemplate); 64 setTemplate(template); 65 this.mediaType = mediaType; 66 } 63 67 64 @Override 65 public void initEntityVariants(PersistenceResource resource) { 66 if (template != null) { 67 resource.getVariants().add(new Variant(mediaType)); 68 } 69 } 68 public Collection<Variant> getVariants() { 69 return Collections.singleton(new Variant(getMediaType())); 70 } 70 71 71 @Override 72 public Representation getEntityRepresentation(Variant variant, 73 PersistenceResource resource) { 74 if (template != null && mediaType.equals(variant.getMediaType())) { 75 TemplateRepresentation templateRepresentation = new TemplateRepresentation( 76 template, mediaType); 77 templateRepresentation.setVariable("entity", resource.getEntity()); 78 templateRepresentation.setVariable("resource", resource); 79 return templateRepresentation; 80 } 81 return null; 82 } 72 public Representation represent(EntityResource<E> entityResource, 73 Variant variant) { 74 if (entityResource instanceof EntityCollection) { 75 return represent((EntityCollection<E>) entityResource, variant); 76 } else if (entityResource instanceof EntityInstance) { 77 return represent((EntityInstance<E>) entityResource, variant); 78 } else { 79 return null; 80 } 81 } 83 82 84 @Override 85 public void initEntityListVariants(PersistenceResource resource) { 86 if (listTemplate != null) { 87 resource.getVariants().add(new Variant(mediaType)); 88 } 89 } 83 public Representation represent(EntityInstance<E> resource, Variant variant) { 84 if (template != null && mediaType.equals(variant.getMediaType())) { 85 TemplateRepresentation templateRepresentation = new TemplateRepresentation( 86 template, mediaType); 87 templateRepresentation.setVariable("entity", resource.getEntity()); 88 templateRepresentation.setVariable("resource", resource); 89 return templateRepresentation; 90 } 91 return null; 92 } 90 93 91 @Override 92 public Representation getEntityListRepresentation(Variant variant, 93 PersistenceResource resource) { 94 if (listTemplate != null && mediaType.equals(variant.getMediaType())) { 95 TemplateRepresentation templateRepresentation = new TemplateRepresentation( 96 listTemplate, mediaType); 97 templateRepresentation.setVariable("entities", resource 98 .getEntities()); 99 templateRepresentation.setVariable("resource", resource); 100 return templateRepresentation; 101 } 102 return null; 103 } 94 public Representation represent(EntityCollection<E> resource, 95 Variant variant) { 96 if (listTemplate != null && mediaType.equals(variant.getMediaType())) { 97 TemplateRepresentation templateRepresentation = new TemplateRepresentation( 98 listTemplate, mediaType); 99 templateRepresentation.setVariable("entities", resource 100 .getEntityList()); 101 templateRepresentation.setVariable("resource", resource); 102 return templateRepresentation; 103 } 104 return null; 105 } 104 106 105 public Template getListTemplate() {106 return listTemplate;107 }107 public Template getListTemplate() { 108 return listTemplate; 109 } 108 110 109 public void setListTemplate(String listTemplate) throws TemplateException,110 ELException, IOException {111 if (listTemplate == null) {112 this.listTemplate = null;113 } else {114 this.listTemplate = TemplateFactory.getInstance().getTemplate(115 listTemplate);116 }117 }111 public void setListTemplate(String listTemplate) throws TemplateException, 112 ELException, IOException { 113 if (listTemplate == null) { 114 this.listTemplate = null; 115 } else { 116 this.listTemplate = TemplateFactory.getInstance().getTemplate( 117 listTemplate); 118 } 119 } 118 120 119 public void setListTemplate(Template listTemplate) {120 this.listTemplate = listTemplate;121 }121 public void setListTemplate(Template listTemplate) { 122 this.listTemplate = listTemplate; 123 } 122 124 123 public MediaType getMediaType() {124 return mediaType;125 }125 public MediaType getMediaType() { 126 return mediaType; 127 } 126 128 127 public void setMediaType(MediaType mediaType) {128 this.mediaType = mediaType;129 }129 public void setMediaType(MediaType mediaType) { 130 this.mediaType = mediaType; 131 } 130 132 131 public Template getTemplate() {132 return template;133 }133 public Template getTemplate() { 134 return template; 135 } 134 136 135 public void setTemplate(String template) throws TemplateException,136 ELException, IOException {137 if (template == null) {138 this.template = null;139 } else {140 this.template = TemplateFactory.getInstance().getTemplate(template);141 }142 }137 public void setTemplate(String template) throws TemplateException, 138 ELException, IOException { 139 if (template == null) { 140 this.template = null; 141 } else { 142 this.template = TemplateFactory.getInstance().getTemplate(template); 143 } 144 } 143 145 144 public void setTemplate(Template template) {145 this.template = template;146 }146 public void setTemplate(Template template) { 147 this.template = template; 148 } 147 149 148 150 }
