| 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 | } |
|---|
| 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 | } |
|---|
| 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 | } |
|---|
| 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 | } |
|---|
| 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 | } |
|---|
| 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 | } |
|---|
| 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 | } |
|---|
| 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 | } |
|---|