Changeset 3677

Show
Ignore:
Timestamp:
05/03/08 22:48:40 (4 years ago)
Author:
michael
Message:

Adding back some converters.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ivy.xml

    r3666 r3677  
    1111    <dependency org="com.noelios.restlet" name="com.noelios.restlet.ext.simple" rev="1.1-M2" conf="test->default"/> 
    1212    <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"/> 
    1314    <dependency org="javax.persistence" name="persistence-api" rev="1.0" conf="compile->default"/> 
    1415    <dependency org="junit" name="junit" rev="3.8.1" conf="test->default"/> 
  • trunk/src/main/java/org/sarugo/restlet/jpa/EntityHelper.java

    r3613 r3677  
    1717package org.sarugo.restlet.jpa; 
    1818 
    19 import java.beans.PropertyEditor; 
    20 import java.beans.PropertyEditorManager; 
    21 import java.lang.reflect.Constructor; 
    2219import java.lang.reflect.Field; 
    2320import java.lang.reflect.Method; 
     
    104101    } 
    105102 
    106     public static <T extends Object> T convertToType(Class<T> type, 
    107             String value, EntityRouter router) { 
    108         // Short cut for String types 
    109         if (type.isAssignableFrom(String.class)) { 
    110             return type.cast(value); 
    111         } 
    112  
    113         // Short cut for null 
    114         if (value == null) { 
    115             return null; 
    116         } 
    117  
    118         // Otherwise, check if the type is an enum 
    119         if (type.isEnum()) { 
    120             for (T constant : type.getEnumConstants()) { 
    121                 if (((Enum) constant).name().equals(value)) { 
    122                     return constant; 
    123                 } 
    124             } 
    125             return null; 
    126         } 
    127  
    128         // Otherwise, use JavaBean coercion 
    129         try { 
    130             PropertyEditor e = PropertyEditorManager.findEditor(type); 
    131             e.setAsText(value); 
    132             return type.cast(e.getValue()); 
    133         } catch (Exception e) { 
    134             // Ignore 
    135         } 
    136  
    137         // Otherwise check if the type is an entity type 
    138         if (router != null) { 
    139             @SuppressWarnings("unchecked") 
    140             T entity = (T) router.getEntity(value); 
    141             if (entity != null) { 
    142                 return entity; 
    143             } 
    144         } 
    145  
    146         // Otherwise, try a String constructor 
    147         try { 
    148             Constructor<T> c = type.getConstructor(String.class); 
    149             return c.newInstance(value); 
    150         } catch (Exception e) { 
    151             // Ignore 
    152         } 
    153  
    154         // If all else failed, just return null 
    155         return null; 
    156     } 
    157  
    158103} 
  • trunk/src/main/java/org/sarugo/restlet/jpa/EntityRouter.java

    r3666 r3677  
    3030import org.restlet.Route; 
    3131import org.restlet.Router; 
    32 import org.restlet.data.Method; 
    3332import org.restlet.data.Reference; 
    3433import org.restlet.data.Request; 
     
    7473    private Reference baseRef; 
    7574 
    76     private Template defaultRouteTemplate; 
     75    private Template template; 
    7776 
    7877    /** 
     
    112111    } 
    113112 
    114     /** 
    115      * Convert an entity to a URL based on an attached {@link EntityFinder}. 
    116      *  
    117      * @param entity 
    118      *            The entity to get a URL for. 
    119      * @return The entity's URL relative to this router. 
    120      * @see #attachEntity(String, EntityFinder) 
    121      */ 
    122     public String getEntityURL(Object entity) { 
    123         if (isChild()) { 
    124             return getParent().getEntityURL(entity); 
    125         } else { 
    126             return doGetEntityURL(entity); 
    127         } 
    128     } 
    129  
    130     private String doGetEntityURL(Object entity) { 
     113    String getEntityURL(Object entity) { 
    131114        String url = null; 
    132115        if (entity != null) { 
     
    140123                // format the entity's URL 
    141124                if (route == getDefaultRoute()) { 
    142                     entityUrl = defaultRouteTemplate.format(attributes); 
     125                    entityUrl = template.format(attributes); 
    143126                } else { 
    144127                    entityUrl = route.getTemplate().format(attributes); 
     
    149132                    // add the router's URL template if we have no default route 
    150133                    if (getDefaultRoute() == null) { 
    151                         parentUrl = defaultRouteTemplate.format(attributes); 
     134                        parentUrl = template.format(attributes); 
    152135                    } 
    153136                    @SuppressWarnings("unchecked") 
     
    155138                            entity); 
    156139                    if (parentEntity != null) { 
    157                         String parentEntityUrl = getParent().doGetEntityURL( 
     140                        String parentEntityUrl = getParent().getEntityURL( 
    158141                                parentEntity); 
    159142                        if (parentEntityUrl == null) { 
     
    170153                        while (parent != null) { 
    171154                            if (parent.isChild()) { 
    172                                 parentUrl = parent.defaultRouteTemplate 
    173                                         .format(attributes) 
     155                                parentUrl = parent.template.format(attributes) 
    174156                                        + parentUrl; 
    175157                            } else if (parent.getBaseRef() != null) { 
     
    188170                // otherwise check if our children can map the entity 
    189171                for (EntityRouter child : children) { 
    190                     url = child.doGetEntityURL(entity); 
     172                    url = child.getEntityURL(entity); 
    191173                    if (url != null) { 
    192174                        break; 
     
    198180    } 
    199181 
    200     /** 
    201      * Locate an entity instance of the given class by its URL. 
    202      *  
    203      * @param url 
    204      *            The entity URL as returned by {@link #getEntityURL(Object)}. 
    205      * @return The entity instance matching the given URL, or null if none was 
    206      *         found. 
    207      */ 
    208     public Object getEntity(String url) { 
    209         return getEntity(new Request(Method.GET, url)); 
    210     } 
    211  
    212     /** 
    213      * Locate an entity instance of the given class by its URL. 
    214      *  
    215      * @param url 
    216      *            The entity URL as returned by {@link #getEntityURL(Object)}. 
    217      * @return The entity instance matching the given URL, or null if none was 
    218      *         found. 
    219      */ 
    220     public Object getEntity(Request request) { 
    221         if (isChild()) { 
    222             return getParent().getEntity(request); 
    223         } else { 
    224             if (baseRef != null 
    225                     && request.getResourceRef().toString(true, false) 
    226                             .startsWith(baseRef.toString(true, false))) { 
    227                 request.getResourceRef().setBaseRef(baseRef); 
    228             } 
    229             return doGetEntity(request); 
    230         } 
    231     } 
    232  
    233     private Object doGetEntity(Request request) { 
     182    Object getEntity(Request request) { 
     183        if (baseRef != null 
     184                && request.getResourceRef().toString(true, false).startsWith( 
     185                        baseRef.toString(true, false))) { 
     186            request.getResourceRef().setBaseRef(baseRef); 
     187        } 
    234188        Object entity = null; 
    235189        Route next = (Route) getNext(request, new Response(request)); 
     
    287241                        } 
    288242                        request.getResourceRef().setBaseRef(baseRef); 
    289                         entity = ((EntityRouter) target).doGetEntity(request); 
     243                        entity = ((EntityRouter) target).getEntity(request); 
    290244                    } 
    291245                } 
     
    335289                EntityRouter router = (EntityRouter) next; 
    336290                Route route = super.attach(uriPattern, target); 
    337                 router.setDefaultRouteTemplate(route.getTemplate()); 
     291                router.setTemplate(route.getTemplate()); 
    338292                children.add(router); 
    339293                return route; 
     
    381335    } 
    382336 
    383     public EntityRouter getParent() { 
     337    private EntityRouter getParent() { 
    384338        return parent; 
    385339    } 
    386340 
    387     public boolean isChild() { 
     341    private boolean isChild() { 
    388342        return parent != null; 
    389343    } 
     
    393347    } 
    394348 
    395     public EntityRouter setBaseRef(String baseRef) { 
    396         return setBaseRef(new Reference(baseRef)); 
    397     } 
    398  
    399     public EntityRouter setBaseRef(Reference baseRef) { 
     349    public void setBaseRef(String baseRef) { 
     350        setBaseRef(new Reference(baseRef)); 
     351    } 
     352 
     353    public void setBaseRef(Reference baseRef) { 
     354        if (isChild()) { 
     355            throw new IllegalStateException( 
     356                    "BaseRef is only applicable to the root entity router."); 
     357        } 
    400358        this.baseRef = baseRef; 
    401         return this; 
    402     } 
    403  
    404     public void setDefaultRouteTemplate(Template template) { 
    405         defaultRouteTemplate = template; 
     359    } 
     360 
     361    private void setTemplate(Template template) { 
     362        this.template = template; 
    406363        Route route = getDefaultRoute(); 
    407364        if (route != null && route instanceof EntityRoute) { 
  • trunk/src/main/java/org/sarugo/restlet/jpa/PersistenceApplication.java

    r3666 r3677  
    66import org.restlet.Application; 
    77import org.restlet.Context; 
     8import org.restlet.data.Method; 
     9import org.restlet.data.Request; 
    810 
    911public class PersistenceApplication extends Application { 
     
    6466    } 
    6567 
     68    public String getEntityURL(Object entity) { 
     69        return entityRouter.getEntityURL(entity); 
     70    } 
     71 
     72    /** 
     73     * Locate an entity instance of the given class by its URL. 
     74     *  
     75     * @param url 
     76     *            The entity URL as returned by {@link #getEntityURL(Object)}. 
     77     * @return The entity instance matching the given URL, or null if none was 
     78     *         found. 
     79     */ 
     80    public Object getEntity(String url) { 
     81        return getEntity(new Request(Method.GET, url)); 
     82    } 
     83 
     84    /** 
     85     * Locate an entity instance of the given class by its URL. 
     86     *  
     87     * @param url 
     88     *            The entity URL as returned by {@link #getEntityURL(Object)}. 
     89     * @return The entity instance matching the given URL, or null if none was 
     90     *         found. 
     91     */ 
     92    public Object getEntity(Request request) { 
     93        return entityRouter.getEntity(request); 
     94    } 
     95 
    6696} 
  • trunk/src/main/java/org/sarugo/restlet/jpa/converter/FormEntityUpdateConverter.java

    r2956 r3677  
    99import java.text.ParseException; 
    1010import java.util.Date; 
    11 import java.util.logging.Level; 
    1211 
    1312import org.restlet.data.Form; 
    1413import org.restlet.data.MediaType; 
    15 import org.restlet.data.Status; 
    1614import org.restlet.resource.Representation; 
    17 import org.restlet.resource.StringRepresentation; 
    18 import org.sarugo.restlet.jpa.PersistenceConverter; 
    19 import org.sarugo.restlet.jpa.PersistenceResource; 
     15import org.sarugo.restlet.jpa.PersistenceApplication; 
    2016 
    21 public class FormEntityUpdateConverter extends PersistenceConverter { 
     17public class FormEntityUpdateConverter extends Converter { 
    2218 
    23         @Override 
    24         public boolean updateEntity(Object entity, PersistenceResource resource) { 
    25                 Representation representation = resource.getRequest().getEntity(); 
    26                 if (!MediaType.APPLICATION_WWW_FORM.equals(representation 
    27                                 .getMediaType())) { 
    28                         return false; 
    29                 } 
    30                 Form data = resource.getRequest().getEntityAsForm(); 
    31                 try { 
    32                         for (PropertyDescriptor p : Introspector.getBeanInfo( 
    33                                         entity.getClass()).getPropertyDescriptors()) { 
    34                                 String value = data.getFirstValue(p.getName()); 
    35                                 if (value != null && p.getWriteMethod() != null) { 
    36                                         Object v = PersistenceConverter.convertToType(p 
    37                                                         .getPropertyType(), value); 
    38                                         if (v != null) { 
    39                                                 p.getWriteMethod().invoke(entity, v); 
    40                                         } else if (p.getPropertyType().isAssignableFrom(Date.class)) { 
    41                                                 try { 
    42                                                         p.getWriteMethod().invoke(entity, 
    43                                                                         Timestamp.valueOf(value)); 
    44                                                 } catch (IllegalArgumentException e) { 
    45                                                         p.getWriteMethod().invoke( 
    46                                                                         entity, 
    47                                                                         DateFormat.getDateTimeInstance().parse( 
    48                                                                                         value)); 
    49                                                 } 
    50                                         } else { 
    51                                                 throw new ParseException("Unable to convert value \"" 
    52                                                                 + value + "\" to type " + p.getPropertyType(), 
    53                                                                 0); 
    54                                         } 
    55                                 } 
    56                         } 
    57                 } catch (IntrospectionException e) { 
    58                         resource.getLogger().log(Level.WARNING, 
    59                                         "Unable to update entity from form.", e); 
    60                         resource.getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); 
    61                 } catch (IllegalAccessException e) { 
    62                         resource.getLogger().log(Level.WARNING, 
    63                                         "Unable to update entity from form.", e); 
    64                         resource.getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); 
    65                 } catch (IllegalArgumentException e) { 
    66                         resource.getLogger().log(Level.WARNING, 
    67                                         "Unable to update entity from form.", e); 
    68                         resource.getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); 
    69                 } catch (InvocationTargetException e) { 
    70                         resource.getLogger().log(Level.WARNING, 
    71                                         "Unable to update entity from form.", e); 
    72                         resource.getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); 
    73                 } catch (ParseException e) { 
    74                         resource.getLogger().log(Level.INFO, 
    75                                         "Unable to update entity from form.", e); 
    76                         resource.getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 
    77                         resource.getResponse().setEntity( 
    78                                         new StringRepresentation(e.toString())); 
    79                 } 
    80                 return true; 
    81         } 
     19    public FormEntityUpdateConverter(PersistenceApplication application) { 
     20        super(application); 
     21    } 
     22     
     23    @Override 
     24    public MediaType getMediaType() { 
     25        return MediaType.APPLICATION_WWW_FORM; 
     26    } 
     27 
     28    @Override 
     29    public boolean updateEntity(Object entity, Representation representation) 
     30            throws ParseException { 
     31        if (!MediaType.APPLICATION_WWW_FORM.equals(representation 
     32                .getMediaType())) { 
     33            return false; 
     34        } 
     35        Form data = new Form(representation); 
     36        try { 
     37            for (PropertyDescriptor p : Introspector.getBeanInfo( 
     38                    entity.getClass()).getPropertyDescriptors()) { 
     39                String value = data.getFirstValue(p.getName()); 
     40                if (value != null && p.getWriteMethod() != null) { 
     41                    Object v = convertToTypeOrEntity(p.getPropertyType(), value); 
     42                    if (v != null) { 
     43                        p.getWriteMethod().invoke(entity, v); 
     44                    } else if (p.getPropertyType().isAssignableFrom(Date.class)) { 
     45                        try { 
     46                            p.getWriteMethod().invoke(entity, 
     47                                    Timestamp.valueOf(value)); 
     48                        } catch (IllegalArgumentException e) { 
     49                            p.getWriteMethod().invoke( 
     50                                    entity, 
     51                                    DateFormat.getDateTimeInstance().parse( 
     52                                            value)); 
     53                        } 
     54                    } else { 
     55                        throw new ParseException("Unable to convert value \"" 
     56                                + value + "\" to type " + p.getPropertyType(), 
     57                                0); 
     58                    } 
     59                } 
     60            } 
     61        } catch (IntrospectionException e) { 
     62            throw new RuntimeException(e); 
     63        } catch (IllegalAccessException e) { 
     64            throw new RuntimeException(e); 
     65        } catch (IllegalArgumentException e) { 
     66            throw new RuntimeException(e); 
     67        } catch (InvocationTargetException e) { 
     68            throw new RuntimeException(e); 
     69        } 
     70        return true; 
     71    } 
     72 
     73    @Override 
     74    public Representation represent(Object entity) { 
     75        Form form = new Form(); 
     76        try { 
     77            for (PropertyDescriptor p : Introspector.getBeanInfo( 
     78                    entity.getClass()).getPropertyDescriptors()) { 
     79                form.add(p.getName(), String.valueOf(p.getReadMethod().invoke( 
     80                        entity))); 
     81            } 
     82        } catch (IntrospectionException e) { 
     83            throw new RuntimeException(e); 
     84        } catch (IllegalAccessException e) { 
     85            throw new RuntimeException(e); 
     86        } catch (IllegalArgumentException e) { 
     87            throw new RuntimeException(e); 
     88        } catch (InvocationTargetException e) { 
     89            throw new RuntimeException(e); 
     90        } 
     91        return form.getWebRepresentation(); 
     92    } 
    8293 
    8394} 
  • trunk/src/main/java/org/sarugo/restlet/jpa/converter/JSONConverter.java

    r2945 r3677  
    2121import java.beans.PropertyDescriptor; 
    2222import java.lang.reflect.InvocationTargetException; 
    23 import java.util.ArrayList
     23import java.text.ParseException
    2424import java.util.HashMap; 
    25 import java.util.List; 
    2625import java.util.Map; 
    2726import java.util.logging.Level; 
     
    3029import org.restlet.resource.Representation; 
    3130import org.restlet.resource.StringRepresentation; 
    32 import org.restlet.resource.Variant; 
    33 import org.sarugo.restlet.jpa.PersistenceResource; 
    34 import org.sarugo.restlet.jpa.PersistenceConverter; 
     31import org.sarugo.restlet.jpa.PersistenceApplication; 
    3532 
    3633import com.sdicons.json.mapper.JSONMapper; 
    3734import com.sdicons.json.mapper.MapperException; 
    3835 
    39 public class JSONConverter extends PersistenceConverter { 
     36public class JSONConverter extends Converter { 
     37 
     38        public JSONConverter(PersistenceApplication application) { 
     39        super(application); 
     40    } 
     41 
     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; 
     51    } 
    4052 
    4153        @Override 
    42         public void initEntityVariants(PersistenceResource resource) { 
    43                 resource.getVariants().add(new Variant(MediaType.APPLICATION_JSON)); 
    44         } 
    45  
    46         @Override 
    47         public Representation getEntityRepresentation(Variant variant, 
    48                         PersistenceResource resource) { 
    49                 if (MediaType.APPLICATION_JSON.equals(variant.getMediaType())) { 
     54        public Representation represent(Object entity) { 
    5055                        try { 
    5156                                // Read each property from the entity and store in a map. 
     
    5459                                Map<String, Object> entityValues = new HashMap<String, Object>(); 
    5560                                for (PropertyDescriptor p : Introspector.getBeanInfo( 
    56                                                 resource.getModel()).getPropertyDescriptors()) { 
     61                                                entity.getClass()).getPropertyDescriptors()) { 
    5762                                        if (p.getWriteMethod() != null && p.getReadMethod() != null) { 
    5863                                                Object value = p.getReadMethod().invoke( 
    59                                                                 resource.getEntity()); 
    60                                                 if (resource.getRouter().getName(value.getClass()) != null) { 
    61                                                         // Convert "known" entities to URLs 
    62                                                        value = resource.getEntityReference(value) 
    63                                                                        .toString(false, false)
     64                                                                entity); 
     65                        // Convert "known" entities to URLs 
     66                        String url = getApplication().getEntityURL(value); 
     67                                                if (url != null) { 
     68                                                        value = url
    6469                                                } 
    6570                                                entityValues.put(p.getName(), value); 
     
    6772                                } 
    6873                                return new StringRepresentation(JSONMapper.toJSON(entityValues) 
    69                                                 .render(true), variant.getMediaType()); 
     74                                                .render(true), getMediaType()); 
    7075                        } catch (MapperException e) { 
    71                                 resource.getLogger().log(Level.WARNING, 
     76                                getApplication().getLogger().log(Level.WARNING, 
    7277                                                "Unable to map entity to JSON", e); 
    7378                        } catch (IntrospectionException e) { 
    74                                 resource.getLogger().log(Level.WARNING, 
     79                                getApplication().getLogger().log(Level.WARNING, 
    7580                                                "Unable to map entity to JSON", e); 
    7681                        } catch (IllegalArgumentException e) { 
    77                                 resource.getLogger().log(Level.WARNING, 
     82                                getApplication().getLogger().log(Level.WARNING, 
    7883                                                "Unable to map entity to JSON", e); 
    7984                        } catch (IllegalAccessException e) { 
    80                                 resource.getLogger().log(Level.WARNING, 
     85                                getApplication().getLogger().log(Level.WARNING, 
    8186                                                "Unable to map entity to JSON", e); 
    8287                        } catch (InvocationTargetException e) { 
    83                                 resource.getLogger().log(Level.WARNING, 
     88                                getApplication().getLogger().log(Level.WARNING, 
    8489                                                "Unable to map entity to JSON", e); 
    8590                        } 
    86                 } 
    87                 return null; 
    88         } 
    89  
    90         @Override 
    91         public void initEntityListVariants(PersistenceResource resource) { 
    92                 resource.getVariants().add(new Variant(MediaType.APPLICATION_JSON)); 
    93         } 
    94  
    95         @Override 
    96         public Representation getEntityListRepresentation(Variant variant, 
    97                         PersistenceResource resource) { 
    98                 if (MediaType.APPLICATION_JSON.equals(variant.getMediaType())) { 
    99                         try { 
    100                                 List<String> urlList = new ArrayList<String>(resource 
    101                                                 .getEntities().size()); 
    102                                 for (Object entity : resource.getEntities()) { 
    103                                         urlList.add(resource.getEntityURI(entity)); 
    104                                 } 
    105                                 Map<String, Object> listObject = new HashMap<String, Object>(1, 1.0f); 
    106                                 listObject.put("entities", urlList); 
    107                                 return new StringRepresentation(JSONMapper.toJSON(listObject) 
    108                                                 .render(true), variant.getMediaType()); 
    109                         } catch (MapperException e) { 
    110                                 resource.getLogger().log(Level.WARNING, 
    111                                                 "Unable to map entity list to JSON", e); 
    112                         } catch (IllegalArgumentException e) { 
    113                                 resource.getLogger().log(Level.WARNING, 
    114                                                 "Unable to map entity list to JSON", e); 
    115                         } 
    116                 } 
    11791                return null; 
    11892        } 
  • trunk/src/main/java/org/sarugo/restlet/jpa/converter/StringEntityConverter.java

    r2945 r3677  
    1717package org.sarugo.restlet.jpa.converter; 
    1818 
     19import java.text.ParseException; 
     20 
    1921import org.restlet.data.MediaType; 
    2022import org.restlet.resource.Representation; 
    2123import org.restlet.resource.StringRepresentation; 
    22 import org.restlet.resource.Variant; 
    23 import org.sarugo.restlet.jpa.PersistenceResource; 
    24 import org.sarugo.restlet.jpa.PersistenceConverter; 
     24import org.sarugo.restlet.jpa.PersistenceApplication; 
    2525 
    26 public class StringEntityConverter extends PersistenceConverter { 
     26public class StringEntityConverter extends Converter { 
    2727 
    28         @Override 
    29         public void initEntityVariants(PersistenceResource resource) { 
    30                 resource.getVariants().add(new Variant(MediaType.TEXT_PLAIN)); 
    31         } 
     28    public StringEntityConverter(PersistenceApplication application) { 
     29        super(application); 
     30    } 
    3231 
    33         @Override 
    34         public Representation getEntityRepresentation(Variant variant, 
    35                         PersistenceResource resource) { 
    36                 if (MediaType.TEXT_PLAIN.equals(variant.getMediaType())) { 
    37                         return new StringRepresentation(resource.getEntity().toString()); 
    38                 } 
    39                 return null; 
    40         } 
     32    @Override 
     33    public MediaType getMediaType() { 
     34        return MediaType.TEXT_PLAIN; 
     35    } 
     36 
     37    @Override 
     38    public Representation represent(Object entity) { 
     39        return new StringRepresentation(String.valueOf(entity)); 
     40    } 
     41 
     42    @Override 
     43    public boolean updateEntity(Object entity, Representation representation) 
     44            throws ParseException { 
     45        // TODO Auto-generated method stub 
     46        return false; 
     47    } 
    4148 
    4249} 
  • trunk/src/main/java/org/sarugo/restlet/jpa/resource/EntityInstance.java

    r3666 r3677  
    2626import org.restlet.resource.Resource; 
    2727import org.sarugo.restlet.jpa.EntityFinder; 
    28 import org.sarugo.restlet.jpa.EntityHelper; 
    2928import org.sarugo.restlet.jpa.EntityRouter; 
    3029import org.sarugo.restlet.jpa.PersistenceApplication; 
     30import org.sarugo.restlet.jpa.converter.Converter; 
    3131 
    3232/** 
     
    9595        String value = (String) attributes.get(finder.getIdAttribute()); 
    9696        if (value != null) { 
    97             id = EntityHelper.convertToType(finder.getEntityIdClass(), value, 
    98                     null); 
     97            id = Converter.convertToType(finder.getEntityIdClass(), value); 
    9998        } 
    10099        return id; 
  • trunk/src/test/java/org/sarugo/restlet/jpa/URLMappingTests.java

    r3666 r3677  
    6363 
    6464    public void testGetEntityURL() { 
    65         assertEquals("/test/foo/" + foo.id, router.getEntityURL(foo)); 
     65        assertEquals("/test/foo/" + foo.id, app.getEntityURL(foo)); 
    6666        assertEquals("/test/foo/" + foo.id + "/bar/" + bar.id, router 
    6767                .getEntityURL(bar)); 
    6868        Route barRoute = router.attach("/bar/{barId}", BarResource.class); 
    69         assertEquals("/bar/" + bar.id, router.getEntityURL(bar)); 
     69        assertEquals("/bar/" + bar.id, app.getEntityURL(bar)); 
    7070        router.attach("/baz/{barId}", barRoute.getNext()); 
    71         assertEquals("/bar/" + bar.id, router.getEntityURL(bar)); 
     71        assertEquals("/bar/" + bar.id, app.getEntityURL(bar)); 
    7272        router.setBaseRef("http://server/path"); 
    7373        assertEquals("http://server/path/bar/" + bar.id, router 
     
    7676 
    7777    public void testGetEntity() { 
    78         FooEntity foundFoo = (FooEntity) router.getEntity("/test/foo/" + foo.id); 
     78        FooEntity foundFoo = (FooEntity) app.getEntity("/test/foo/" + foo.id); 
    7979        assertEquals(foo.id, foundFoo.id); 
    80         foundFoo = (FooEntity) router.getEntity("/test/foo/" + foo.id + 120485); 
     80        foundFoo = (FooEntity) app.getEntity("/test/foo/" + foo.id + 120485); 
    8181        assertNull(foundFoo); 
    82         foundFoo = (FooEntity) router.getEntity("/test/fool/" + foo.id); 
     82        foundFoo = (FooEntity) app.getEntity("/test/fool/" + foo.id); 
    8383        assertNull(foundFoo); 
    84         BarEntity foundBar = (BarEntity) router.getEntity("/test/foo/" + foo.id 
     84        BarEntity foundBar = (BarEntity) app.getEntity("/test/foo/" + foo.id 
    8585                + "/bar/" + bar.id); 
    8686        assertEquals(bar.id, foundBar.id); 
    8787        Route barRoute = router.attach("/bar/{barId}", BarResource.class); 
    88         foundBar = (BarEntity) router.getEntity("/bar/" + bar.id); 
     88        foundBar = (BarEntity) app.getEntity("/bar/" + bar.id); 
    8989        assertEquals(bar.id, foundBar.id); 
    9090        router.attach("/baz/{barId}", barRoute.getNext()); 
    91         foundBar = (BarEntity) router.getEntity("/baz/" + bar.id); 
     91        foundBar = (BarEntity) app.getEntity("/baz/" + bar.id); 
    9292        assertEquals(bar.id, foundBar.id); 
    9393        router.setBaseRef("http://server/path"); 
    94         foundBar = (BarEntity) router.getEntity("http://server/path/bar/" 
     94        foundBar = (BarEntity) app.getEntity("http://server/path/bar/" 
    9595                + bar.id); 
    9696        assertEquals(bar.id, foundBar.id); 
    97         foundBar = (BarEntity) router.getEntity("/bar/" + bar.id); 
     97        foundBar = (BarEntity) app.getEntity("/bar/" + bar.id); 
    9898        assertEquals(bar.id, foundBar.id); 
    9999    }