- Timestamp:
- 12/03/08 17:14:06 (4 years ago)
- Files:
-
- trunk/build.xml (modified) (2 diffs)
- trunk/ivy.xml (modified) (2 diffs)
- trunk/src/main/java/org/sarugo/restlet/jpa/EntityFinder.java (modified) (7 diffs)
- trunk/src/main/java/org/sarugo/restlet/jpa/EntityRouter.java (modified) (6 diffs)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/Converter.java (modified) (2 diffs)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/ConverterType.java (added)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/EntityConverter.java (added)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/EntityConverters.java (added)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/FormConverter.java (moved) (moved from trunk/src/main/java/org/sarugo/restlet/jpa/converter/FormEntityUpdateConverter.java) (3 diffs)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/InputConverter.java (added)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/JSONConverter.java (modified) (2 diffs)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/OutputConverter.java (added)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/StringConverter.java (moved) (moved from trunk/src/main/java/org/sarugo/restlet/jpa/converter/StringEntityConverter.java) (1 diff)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/XTCConverter.java (added)
- trunk/src/main/java/org/sarugo/restlet/jpa/resource/EntityInstance.java (modified) (9 diffs)
- trunk/src/main/java/org/sarugo/restlet/jpa/resource/EntityList.java (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/build.xml
r2863 r3691 1 <project name="restlet-jpa" default="help" >1 <project name="restlet-jpa" default="help" xmlns:ivy="antlib:org.apache.ivy.ant"> 2 2 <!-- Sarugo common ant configuration --> 3 3 <property name="ant.lib.dir" value="ant" /> … … 17 17 18 18 <target name="download-build.common"> 19 <get src="${build.common.url}" dest="${build.common.file}" usetimestamp="true" />19 <get ignoreerrors="true" src="${build.common.url}" dest="${build.common.file}" usetimestamp="true" /> 20 20 </target> 21 22 <import file="${build.common.file}" optional="true" />23 21 22 <import file="${build.common.file}" optional="true" /> 23 24 24 <target name="bootstrap" description="Download build dependencies and libraries"> 25 25 <mkdir dir="${ant.lib.dir}" /> 26 <antcall target="download-build.common"/> 27 <antcall target="common.bootstrap"/> 26 <antcall target="download-build.common" /> 27 <antcall target="common.bootstrap" /> 28 </target> 29 30 <target name="project.package"> 31 <mkdir dir="${dist.dir}" /> 32 <jar destfile="${dist.dir}/${project.name}-${build.rev}.jar"> 33 <fileset dir="${build.dir}/main/java"> 34 <include name="**/*.class" /> 35 <exclude name="org/sarugo/xtc/restlet/jpa/converter/JSONConverter.class" /> 36 <exclude name="org/sarugo/xtc/restlet/jpa/converter/XTCConverter.class" /> 37 </fileset> 38 <fileset dir="${build.dir}/main/resources" /> 39 </jar> 40 <jar destfile="${dist.dir}/${project.name}-json-${build.rev}.jar"> 41 <fileset dir="${build.dir}/main/java"> 42 <include name="org/sarugo/xtc/restlet/jpa/converter/JSONConverter.class" /> 43 </fileset> 44 </jar> 45 <jar destfile="${dist.dir}/${project.name}-xtc-${build.rev}.jar"> 46 <fileset dir="${build.dir}/main/java"> 47 <include name="org/sarugo/xtc/restlet/jpa/converter/XTCConverter.class" /> 48 </fileset> 49 </jar> 50 <!-- Use ivy to copy the runtime dependencies to distribution directory --> 51 <ivy:retrieve pattern="${dist.dir}/[artifact]-[revision].[ext]" conf="default" useOrigin="true" /> 52 <property name="skip.common.package" value="true" /> 28 53 </target> 29 54 trunk/ivy.xml
r3677 r3691 4 4 <configurations> 5 5 <conf name="default" description="Runtime configuration"/> 6 <conf name="json" description="JSON converter" extends="default"/> 7 <conf name="xtc" description="XTC converter" extends="default"/> 6 8 <conf name="compile" visibility="private" extends="default"/> 7 9 <conf name="test" visibility="private" extends="default,compile"/> 8 10 </configurations> 11 <publications> 12 <artifact name="${project.name}" conf="default" /> 13 <artifact name="${project.name}-json" conf="json" /> 14 <artifact name="${project.name}-xtc" conf="xtc" /> 15 </publications> 9 16 <dependencies> 10 <dependency org="org.restlet" name="org.restlet" rev="1.1-M2" conf="compile->default"/> 17 <!-- Restlet --> 18 <dependency org="org.restlet" name="org.restlet" rev="1.1-M2" conf="default"/> 11 19 <dependency org="com.noelios.restlet" name="com.noelios.restlet.ext.simple" rev="1.1-M2" conf="test->default"/> 12 20 <dependency org="com.noelios.restlet" name="com.noelios.restlet.ext.net" rev="1.1-M2" conf="test->default"/> 13 <dependency org="com.sdicons.jsontools" name="jsontools-core" rev="1.5" conf="default"/> 14 <dependency org="javax.persistence" name="persistence-api" rev="1.0" conf="compile->default"/> 15 <dependency org="junit" name="junit" rev="3.8.1" conf="test->default"/> 21 <!-- JSON --> 22 <dependency org="com.sdicons.jsontools" name="jsontools-core" rev="1.5" conf="json->default"/> 23 <!-- XTC --> 24 <dependency org="sarugo" name="xtc" rev="0.4.3" conf="xtc->restlet"/> 25 <!-- JPA --> 26 <dependency org="javax.persistence" name="persistence-api" rev="1.0" conf="default"/> 16 27 <dependency org="org.hibernate" name="hibernate" rev="3.2.4.sp1" conf="test->default"/> 17 28 <dependency org="org.hibernate" name="hibernate-entitymanager" rev="3.3.1.ga" conf="test->default"/> … … 19 30 <dependency org="org.hibernate" name="hibernate-commons-annotations" rev="3.3.0.ga" conf="test->default"/> 20 31 <dependency org="hsqldb" name="hsqldb" rev="1.8.0.7" conf="test->default" /> 32 <!-- JUnit --> 33 <dependency org="junit" name="junit" rev="3.8.1" conf="test->default"/> 21 34 </dependencies> 22 35 </ivy-module> trunk/src/main/java/org/sarugo/restlet/jpa/EntityFinder.java
r3666 r3691 21 21 import java.lang.reflect.InvocationTargetException; 22 22 import java.lang.reflect.Method; 23 import java.util.ArrayList; 23 24 import java.util.Collections; 25 import java.util.List; 24 26 import java.util.Map; 25 27 import java.util.logging.Level; … … 33 35 import org.restlet.data.Response; 34 36 import org.restlet.resource.Resource; 37 import org.sarugo.restlet.jpa.converter.Converter; 38 import org.sarugo.restlet.jpa.converter.EntityConverter; 39 import org.sarugo.restlet.jpa.converter.EntityConverters; 40 import org.sarugo.restlet.jpa.converter.InputConverter; 41 import org.sarugo.restlet.jpa.converter.OutputConverter; 35 42 import org.sarugo.restlet.jpa.resource.EntityInstance; 36 43 … … 49 56 private final Class<? extends Object> entityIdClass; 50 57 58 private final List<InputConverter> inputConverters; 59 60 private final List<OutputConverter> outputConverters; 61 51 62 private final Method getEntityMethod; 52 63 … … 60 71 * Creates an entity finder. 61 72 * 62 * @param router 63 * The {@link EntityRouter} this finder will be be attached to. 64 * @param entityClass 65 * The entity class this finder operates on. 73 * @param app 74 * The {@link PersistenceApplication} this finder is associated 75 * with. 66 76 * @param resource 67 77 * The entity instance resource. 68 78 */ 69 79 @SuppressWarnings("unchecked") 70 public EntityFinder(EntityRouter router, Class<? extends Resource> resource) { 71 super(router.getContext(), resource); 80 public EntityFinder(PersistenceApplication app, 81 Class<? extends Resource> resource) { 82 super(app.getContext()); 72 83 this.idAttribute = "id"; 73 84 EntityResource erAnnotation = resource 74 85 .getAnnotation(EntityResource.class); 75 this.entityClass = erAnnotation.entity(); 86 if (Handler.class.isAssignableFrom(resource) && erAnnotation != null) { 87 this.entityClass = erAnnotation.entity(); 88 setTargetClass((Class<? extends Handler>) resource); 89 } else { 90 throw new IllegalArgumentException( 91 "Resource must be an @EntityResource or @Entity."); 92 } 76 93 this.entityIdClass = EntityHelper.findIdType(entityClass); 94 this.inputConverters = new ArrayList<InputConverter>(); 95 this.outputConverters = new ArrayList<OutputConverter>(); 96 EntityConverters convertersAnnotation = resource 97 .getAnnotation(EntityConverters.class); 98 if (convertersAnnotation != null) { 99 for (EntityConverter c : convertersAnnotation.value()) { 100 addConverter(c, app); 101 } 102 } else { 103 EntityConverter c = resource.getAnnotation(EntityConverter.class); 104 if (c != null) { 105 addConverter(c, app); 106 } 107 } 77 108 Method m = null; 78 109 try { … … 96 127 } 97 128 this.getIdAttributesMethod = m; 129 } 130 131 private void addConverter(EntityConverter c, PersistenceApplication app) { 132 try { 133 Converter instance = c.value().getConstructor( 134 PersistenceApplication.class).newInstance(app); 135 switch (c.type()) { 136 case INPUT: 137 inputConverters.add((InputConverter) instance); 138 break; 139 140 case OUTPUT: 141 outputConverters.add((OutputConverter) instance); 142 break; 143 144 case BOTH: 145 inputConverters.add((InputConverter) instance); 146 outputConverters.add((OutputConverter) instance); 147 break; 148 149 case UNSPECIFIED: 150 if (instance instanceof InputConverter) { 151 inputConverters.add((InputConverter) instance); 152 } 153 if (instance instanceof OutputConverter) { 154 outputConverters.add((OutputConverter) instance); 155 } 156 break; 157 } 158 } catch (RuntimeException e) { 159 throw e; 160 } catch (Exception e) { 161 getLogger().log(Level.WARNING, 162 "Exception instantiating converter - ignoring.", e); 163 } 98 164 } 99 165 … … 133 199 * override {@link #getIdAttributes(Object)}. 134 200 */ 135 public EntityFindersetIdAttribute(String idAttribute) {201 public void setIdAttribute(String idAttribute) { 136 202 this.idAttribute = idAttribute; 137 return this;138 203 } 139 204 … … 278 343 } 279 344 345 /** Used by the default {@link EntityInstance} implementation. */ 346 public List<InputConverter> getInputConverters() { 347 return inputConverters; 348 } 349 350 /** Used by the default {@link EntityInstance} implementation. */ 351 public List<OutputConverter> getOutputConverters() { 352 return outputConverters; 353 } 354 280 355 } trunk/src/main/java/org/sarugo/restlet/jpa/EntityRouter.java
r3677 r3691 65 65 } 66 66 67 private final PersistenceApplication application; 68 67 69 private final EntityRouter parent; 68 70 … … 88 90 EntityRouter(PersistenceApplication application) { 89 91 super(application.getContext()); 92 this.application = application; 90 93 this.parent = null; 91 94 this.entities = new HashMap<Class, EntityRoute>(); … … 96 99 public EntityRouter(EntityRouter parent) { 97 100 super(parent.getContext()); 101 this.application = parent.application; 98 102 this.parent = parent; 99 103 this.entities = new HashMap<Class, EntityRoute>(); … … 104 108 public EntityRouter(EntityRouter parent, Class<? extends Resource> resource) { 105 109 super(parent.getContext()); 110 this.application = parent.application; 106 111 this.parent = parent; 107 112 this.entities = new HashMap<Class, EntityRoute>(); … … 263 268 } 264 269 270 @SuppressWarnings("unchecked") 265 271 @Override 266 272 public Route attach(String uriPattern, Class<? extends Resource> targetClass) { 267 273 if (targetClass.isAnnotationPresent(EntityResource.class)) { 268 return attach(uriPattern, new EntityFinder(this, targetClass)); 274 return attach(uriPattern, 275 new EntityFinder(application, targetClass)); 276 } else if (Resource.class.isAssignableFrom(targetClass)) { 277 return super.attach(uriPattern, 278 (Class<? extends Resource>) targetClass); 269 279 } else { 270 return super.attach(uriPattern, targetClass); 280 throw new IllegalArgumentException( 281 "Target class must be an @Entity or Resource"); 271 282 } 272 283 } … … 301 312 } 302 313 314 @SuppressWarnings("unchecked") 303 315 @Override 304 316 public final Route attachDefault( 305 317 Class<? extends Resource> defaultTargetClass) { 306 318 if (defaultTargetClass.isAnnotationPresent(EntityResource.class)) { 307 return attachDefault(new EntityFinder(this, defaultTargetClass)); 319 return attachDefault(new EntityFinder(application, 320 defaultTargetClass)); 321 } else if (Resource.class.isAssignableFrom(defaultTargetClass)) { 322 return super 323 .attachDefault((Class<? extends Resource>) defaultTargetClass); 308 324 } else { 309 return super.attachDefault(defaultTargetClass); 325 throw new IllegalArgumentException( 326 "Target class must be an @Entity or Resource"); 310 327 } 311 328 } trunk/src/main/java/org/sarugo/restlet/jpa/converter/Converter.java
r3677 r3691 4 4 import java.beans.PropertyEditorManager; 5 5 import java.lang.reflect.Constructor; 6 import java.text.ParseException;7 6 8 import org.restlet.data.MediaType;9 import org.restlet.resource.Representation;10 7 import org.sarugo.restlet.jpa.PersistenceApplication; 11 8 … … 21 18 return application; 22 19 } 23 24 public abstract MediaType getMediaType();25 26 public abstract boolean updateEntity(Object entity,27 Representation representation) throws ParseException;28 29 public abstract Representation represent(Object entity);30 20 31 21 protected <T extends Object> T convertToTypeOrEntity(Class<T> type, trunk/src/main/java/org/sarugo/restlet/jpa/converter/FormConverter.java
r3677 r3691 13 13 import org.restlet.data.MediaType; 14 14 import org.restlet.resource.Representation; 15 import org.restlet.resource.Resource; 15 16 import org.sarugo.restlet.jpa.PersistenceApplication; 17 import org.sarugo.restlet.jpa.resource.EntityInstance; 16 18 17 public class FormEntityUpdateConverter extends Converter { 19 public class FormConverter extends Converter implements InputConverter, 20 OutputConverter { 18 21 19 public Form EntityUpdateConverter(PersistenceApplication application) {22 public FormConverter(PersistenceApplication application) { 20 23 super(application); 21 24 } 22 23 @Override 25 24 26 public MediaType getMediaType() { 25 27 return MediaType.APPLICATION_WWW_FORM; 26 28 } 27 29 28 @Override 29 public boolean updateEntity(Object entity, Representation representation) 30 public boolean updateEntity(Object entity, Resource resource) 30 31 throws ParseException { 31 if (! MediaType.APPLICATION_WWW_FORM.equals(representation32 .getMediaType())) {32 if (!getMediaType().equals( 33 resource.getRequest().getEntity().getMediaType(), true)) { 33 34 return false; 34 35 } 35 Form data = new Form(representation);36 Form data = resource.getRequest().getEntityAsForm(); 36 37 try { 37 38 for (PropertyDescriptor p : Introspector.getBeanInfo( … … 71 72 } 72 73 73 @Override 74 public Representation represent(Object entity) { 74 private Representation represent(Object entity) { 75 75 Form form = new Form(); 76 76 try { … … 92 92 } 93 93 94 public Representation represent(Resource resource) { 95 Representation representation = null; 96 if (resource instanceof EntityInstance) { 97 representation = represent(((EntityInstance) resource).getEntity()); 98 } 99 return representation; 100 } 101 94 102 } trunk/src/main/java/org/sarugo/restlet/jpa/converter/JSONConverter.java
r3677 r3691 21 21 import java.beans.PropertyDescriptor; 22 22 import java.lang.reflect.InvocationTargetException; 23 import java.text.ParseException;24 23 import java.util.HashMap; 25 24 import java.util.Map; … … 28 27 import org.restlet.data.MediaType; 29 28 import org.restlet.resource.Representation; 29 import org.restlet.resource.Resource; 30 30 import org.restlet.resource.StringRepresentation; 31 31 import org.sarugo.restlet.jpa.PersistenceApplication; 32 import org.sarugo.restlet.jpa.resource.EntityInstance; 32 33 33 34 import com.sdicons.json.mapper.JSONMapper; 34 35 import com.sdicons.json.mapper.MapperException; 35 36 36 public class JSONConverter extends Converter {37 public class JSONConverter extends Converter implements OutputConverter { 37 38 38 public JSONConverter(PersistenceApplication application) {39 public JSONConverter(PersistenceApplication application) { 39 40 super(application); 40 41 } 41 42 42 @Override 43 public MediaType getMediaType() { 44 return MediaType.APPLICATION_JSON; 45 } 46 47 @Override 48 public boolean updateEntity(Object entity, Representation representation) throws ParseException { 49 // TODO Auto-generated method stub 50 return false; 43 public MediaType getMediaType() { 44 return MediaType.APPLICATION_JSON; 51 45 } 52 46 53 @Override 54 public Representation represent(Object entity) { 55 try { 56 // Read each property from the entity and store in a map. 57 // For properties which point to other entities, convert 58 // to a URL and store that instead of the object. 59 Map<String, Object> entityValues = new HashMap<String, Object>(); 60 for (PropertyDescriptor p : Introspector.getBeanInfo( 61 entity.getClass()).getPropertyDescriptors()) { 62 if (p.getWriteMethod() != null && p.getReadMethod() != null) { 63 Object value = p.getReadMethod().invoke( 64 entity); 65 // Convert "known" entities to URLs 66 String url = getApplication().getEntityURL(value); 67 if (url != null) { 68 value = url; 69 } 70 entityValues.put(p.getName(), value); 71 } 72 } 73 return new StringRepresentation(JSONMapper.toJSON(entityValues) 74 .render(true), getMediaType()); 75 } catch (MapperException e) { 76 getApplication().getLogger().log(Level.WARNING, 77 "Unable to map entity to JSON", e); 78 } catch (IntrospectionException e) { 79 getApplication().getLogger().log(Level.WARNING, 80 "Unable to map entity to JSON", e); 81 } catch (IllegalArgumentException e) { 82 getApplication().getLogger().log(Level.WARNING, 83 "Unable to map entity to JSON", e); 84 } catch (IllegalAccessException e) { 85 getApplication().getLogger().log(Level.WARNING, 86 "Unable to map entity to JSON", e); 87 } catch (InvocationTargetException e) { 88 getApplication().getLogger().log(Level.WARNING, 89 "Unable to map entity to JSON", e); 90 } 91 return null; 92 } 47 private Representation represent(Object entity) { 48 try { 49 // Read each property from the entity and store in a map. 50 // For properties which point to other entities, convert 51 // to a URL and store that instead of the object. 52 Map<String, Object> entityValues = new HashMap<String, Object>(); 53 for (PropertyDescriptor p : Introspector.getBeanInfo( 54 entity.getClass()).getPropertyDescriptors()) { 55 if (p.getWriteMethod() != null && p.getReadMethod() != null) { 56 Object value = p.getReadMethod().invoke(entity); 57 // Convert "known" entities to URLs 58 String url = getApplication().getEntityURL(value); 59 if (url != null) { 60 value = url; 61 } 62 entityValues.put(p.getName(), value); 63 } 64 } 65 return new StringRepresentation(JSONMapper.toJSON(entityValues) 66 .render(true), getMediaType()); 67 } catch (MapperException e) { 68 getApplication().getLogger().log(Level.WARNING, 69 "Unable to map entity to JSON", e); 70 } catch (IntrospectionException e) { 71 getApplication().getLogger().log(Level.WARNING, 72 "Unable to map entity to JSON", e); 73 } catch (IllegalArgumentException e) { 74 getApplication().getLogger().log(Level.WARNING, 75 "Unable to map entity to JSON", e); 76 } catch (IllegalAccessException e) { 77 getApplication().getLogger().log(Level.WARNING, 78 "Unable to map entity to JSON", e); 79 } catch (InvocationTargetException e) { 80 getApplication().getLogger().log(Level.WARNING, 81 "Unable to map entity to JSON", e); 82 } 83 return null; 84 } 85 86 public Representation represent(Resource resource) { 87 Representation representation = null; 88 if (resource instanceof EntityInstance) { 89 representation = represent(((EntityInstance) resource).getEntity()); 90 } 91 return representation; 92 } 93 93 94 94 } trunk/src/main/java/org/sarugo/restlet/jpa/converter/StringConverter.java
r3677 r3691 17 17 package org.sarugo.restlet.jpa.converter; 18 18 19 import java.text.ParseException;20 21 19 import org.restlet.data.MediaType; 22 20 import org.restlet.resource.Representation; 21 import org.restlet.resource.Resource; 23 22 import org.restlet.resource.StringRepresentation; 24 23 import org.sarugo.restlet.jpa.PersistenceApplication; 24 import org.sarugo.restlet.jpa.resource.EntityInstance; 25 25 26 public class String EntityConverter extendsConverter {26 public class StringConverter extends Converter implements OutputConverter { 27 27 28 public String EntityConverter(PersistenceApplication application) {28 public StringConverter(PersistenceApplication application) { 29 29 super(application); 30 30 } 31 31 32 @Override33 32 public MediaType getMediaType() { 34 33 return MediaType.TEXT_PLAIN; 35 34 } 36 35 37 @Override 38 public Representation represent(Object entity) { 36 private Representation represent(Object entity) { 39 37 return new StringRepresentation(String.valueOf(entity)); 40 38 } 41 39 42 @Override 43 public boolean updateEntity(Object entity, Representation representation) 44 throws ParseException { 45 // TODO Auto-generated method stub 46 return false; 40 public Representation represent(Resource resource) { 41 Representation representation = null; 42 if (resource instanceof EntityInstance) { 43 representation = represent(((EntityInstance) resource).getEntity()); 44 } 45 return representation; 47 46 } 48 47 trunk/src/main/java/org/sarugo/restlet/jpa/resource/EntityInstance.java
r3677 r3691 17 17 package org.sarugo.restlet.jpa.resource; 18 18 19 import java.text.ParseException; 20 import java.util.ArrayList; 21 import java.util.List; 19 22 import java.util.Map; 20 23 … … 24 27 import org.restlet.data.Request; 25 28 import org.restlet.data.Response; 29 import org.restlet.data.Status; 30 import org.restlet.resource.Representation; 26 31 import org.restlet.resource.Resource; 32 import org.restlet.resource.ResourceException; 33 import org.restlet.resource.Variant; 27 34 import org.sarugo.restlet.jpa.EntityFinder; 28 import org.sarugo.restlet.jpa.EntityRouter;29 35 import org.sarugo.restlet.jpa.PersistenceApplication; 30 36 import org.sarugo.restlet.jpa.converter.Converter; 37 import org.sarugo.restlet.jpa.converter.InputConverter; 38 import org.sarugo.restlet.jpa.converter.OutputConverter; 31 39 32 40 /** … … 41 49 private final EntityFinder finder; 42 50 51 private final List<InputConverter> inputConverters; 52 53 private final List<OutputConverter> outputConverters; 54 43 55 public EntityInstance(EntityFinder finder) { 44 56 this.finder = finder; 57 if (finder != null) { 58 this.inputConverters = new ArrayList<InputConverter>(finder 59 .getInputConverters()); 60 this.outputConverters = new ArrayList<OutputConverter>(finder 61 .getOutputConverters()); 62 } else { 63 this.inputConverters = new ArrayList<InputConverter>(); 64 this.outputConverters = new ArrayList<OutputConverter>(); 65 } 45 66 } 46 67 … … 51 72 */ 52 73 @SuppressWarnings("unchecked") 53 public finalvoid init(Context context, Request request, Response response) {74 public void init(Context context, Request request, Response response) { 54 75 super.init(context, request, response); 55 initEntity(request, response); 76 initEntity(); 77 initConverters(); 78 for (OutputConverter c : getOutputConverters()) { 79 getVariants().add(new Variant(c.getMediaType())); 80 } 56 81 } 57 82 … … 68 93 */ 69 94 @SuppressWarnings("unchecked") 70 protected void initEntity( Request request, Response response) {71 Object id = getId( request.getAttributes());72 if (id != null ) {95 protected void initEntity() { 96 Object id = getId(getRequest().getAttributes()); 97 if (id != null && finder != null) { 73 98 entity = (E) getEntityManager().find(finder.getEntityClass(), id); 74 99 } 100 } 101 102 protected void initConverters() { 75 103 } 76 104 … … 93 121 protected Object getId(Map<String, Object> attributes) { 94 122 Object id = null; 95 String value = (String) attributes.get(finder.getIdAttribute()); 96 if (value != null) { 97 id = Converter.convertToType(finder.getEntityIdClass(), value); 123 if (finder != null) { 124 String value = (String) attributes.get(finder.getIdAttribute()); 125 if (value != null) { 126 id = Converter.convertToType(finder.getEntityIdClass(), value); 127 } 98 128 } 99 129 return id; … … 101 131 102 132 /** The {@link PersistenceApplication}. */ 103 public finalPersistenceApplication getApplication() {133 public PersistenceApplication getApplication() { 104 134 return (PersistenceApplication) super.getApplication(); 105 }106 107 /** The root {@link EntityRouter} that created this resource. */108 protected final EntityRouter getRouter() {109 return getApplication().getEntityRouter();110 135 } 111 136 … … 119 144 * {@link #getApplication()}. 120 145 */ 121 p ublic finalEntityManager getEntityManager() {146 protected EntityManager getEntityManager() { 122 147 return getApplication().getEntityManager(); 148 } 149 150 protected final List<InputConverter> getInputConverters() { 151 return inputConverters; 152 } 153 154 protected final List<OutputConverter> getOutputConverters() { 155 return outputConverters; 123 156 } 124 157 … … 134 167 } 135 168 169 @Override 170 public boolean isModifiable() { 171 return !getInputConverters().isEmpty(); 172 } 173 174 @Override 175 public boolean allowPost() { 176 return false; 177 } 178 179 @Override 180 public Representation represent(Variant variant) throws ResourceException { 181 Representation representation = null; 182 for (OutputConverter c : getOutputConverters()) { 183 if (variant.getMediaType().equals(c.getMediaType(), true)) { 184 representation = c.represent(this); 185 if (representation != null) { 186 break; 187 } 188 } 189 } 190 return representation; 191 } 192 193 @Override 194 public void storeRepresentation(Representation representation) 195 throws ResourceException { 196 if (!isAvailable()) { 197 throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND); 198 } 199 boolean stored = false; 200 for (InputConverter c : getInputConverters()) { 201 try { 202 stored = c.updateEntity(getEntity(), this); 203 } catch (ParseException e) { 204 throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e); 205 } 206 if (stored) { 207 break; 208 } 209 } 210 // TODO commit? 211 if (!stored) { 212 throw new ResourceException( 213 Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE); 214 } 215 } 216 217 @Override 218 public void removeRepresentations() throws ResourceException { 219 if (!isAvailable()) { 220 throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND); 221 } 222 getEntityManager().remove(getEntity()); 223 getEntityManager().flush(); 224 // TODO commit? 225 } 226 136 227 } trunk/src/main/java/org/sarugo/restlet/jpa/resource/EntityList.java
r3666 r3691 1 1 package org.sarugo.restlet.jpa.resource; 2 2 3 import java.text.ParseException; 3 4 import java.util.ArrayList; 4 5 import java.util.List; … … 12 13 import org.restlet.data.Request; 13 14 import org.restlet.data.Response; 15 import org.restlet.data.Status; 16 import org.restlet.resource.Representation; 14 17 import org.restlet.resource.Resource; 15 import org.sarugo.restlet.jpa.EntityRouter; 18 import org.restlet.resource.ResourceException; 19 import org.restlet.resource.Variant; 16 20 import org.sarugo.restlet.jpa.PersistenceApplication; 17 18 public class EntityList<E> extends Resource { 19 20 private String queryName; 21 22 private int start = 1; 23 24 private int maxResults = 30; 25 26 private boolean hasMore = false; 27 28 private List<E> list; 29 30 private String searchString; 21 import org.sarugo.restlet.jpa.converter.InputConverter; 22 import org.sarugo.restlet.jpa.converter.OutputConverter; 23 24 public abstract class EntityList<E> extends Resource { 25 26 private final List<InputConverter> inputConverters; 27 28 private final List<OutputConverter> outputConverters; 29 30 protected String queryName; 31 32 protected int start = 1; 33 34 protected int maxResults = 30; 35 36 protected boolean hasMore = false; 37 38 protected List<E> list; 39 40 protected String searchString; 41 42 public EntityList() { 43 this.inputConverters = new ArrayList<InputConverter>(); 44 this.outputConverters = new ArrayList<OutputConverter>(); 45 } 31 46 32 47 public void init(Context context, Request request, Response response) { 33 48 super.init(context, request, response); 34 initList();35 }36 37 @SuppressWarnings("unchecked")38 protected void initList() {39 49 Form params = getRequest().getResourceRef().getQueryAsForm(); 40 50 queryName = params.getFirstValue("query"); … … 47 57 } 48 58 } 59 initList(); 60 initConverters(); 61 for (OutputConverter c : getOutputConverters()) { 62 getVariants().add(new Variant(c.getMediaType())); 63 } 64 } 65 66 @SuppressWarnings("unchecked") 67 protected void initList() { 49 68 Query q = getJPAQuery(); 50 69 if (searchString != null && searchString.length() > 0) { … … 62 81 list = list.subList(0, maxResults); 63 82 } 83 } 84 85 protected void initConverters() { 64 86 } 65 87 … … 76 98 } 77 99 100 protected E createEntity() { 101 return null; 102 } 103 78 104 /** The {@link PersistenceApplication}. */ 79 public final PersistenceApplication getApplication() { 80 return (PersistenceApplication) getApplication(); 81 } 82 83 /** The root {@link EntityRouter} that created this resource. */ 84 protected final EntityRouter getRouter() { 85 return getApplication().getEntityRouter(); 105 public PersistenceApplication getApplication() { 106 return (PersistenceApplication) super.getApplication(); 86 107 } 87 108 … … 90 111 * {@link #getApplication()}. 91 112 */ 92 p ublic finalEntityManager getEntityManager() {113 protected EntityManager getEntityManager() { 93 114 return getApplication().getEntityManager(); 115 } 116 117 protected List<InputConverter> getInputConverters() { 118 return inputConverters; 119 } 120 121 protected List<OutputConverter> getOutputConverters() { 122 return outputConverters; 94 123 } 95 124 … … 126 155 } 127 156 157 @Override 158 public boolean allowPost() { 159 return !getInputConverters().isEmpty(); 160 } 161 162 @Override 163 public Representation represent(Variant variant) throws ResourceException { 164 Representation representation = null; 165 for (OutputConverter c : getOutputConverters()) { 166 if (variant.getMediaType().equals(c.getMediaType(), true)) { 167 representation = c.represent(this); 168 if (representation != null) { 169 break; 170 } 171 } 172 } 173 return representation; 174 } 175 176 @Override 177 public void acceptRepresentation(Representation representation) 178 throws ResourceException { 179 if (!isAvailable()) { 180 throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND); 181 } 182 boolean stored = false; 183 E entity = createEntity(); 184 if (entity == null) { 185 throw new ResourceException(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); 186 } 187 for (InputConverter c : getInputConverters()) { 188 try { 189 stored = c.updateEntity(entity, this); 190 } catch (ParseException e) { 191 throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e); 192 } 193 if (stored) { 194 break; 195 } 196 } 197 if (!stored) { 198 throw new ResourceException( 199 Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE); 200 } else { 201 getEntityManager().persist(entity); 202 getEntityManager().flush(); 203 // TODO commit? 204 getResponse().setLocationRef(getApplication().getEntityURL(entity)); 205 getResponse().setStatus(Status.SUCCESS_CREATED); 206 } 207 } 208 128 209 }
