Changeset 3989 for trunk

Show
Ignore:
Timestamp:
19/10/08 16:09:39 (3 years ago)
Author:
michael
Message:

Various minor fixes:

  • Treat dates as dd/MM/yyyy to workaround locale issues (TODO: make this more flexible)
  • Return XHTML as text/html to support IE better
  • Always set the id attribute for Entity Resources?, not just the first for an Entity.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/java/org/sarugo/restlet/jpa/EntityFinder.java

    r3978 r3989  
    8888        } else { 
    8989            throw new IllegalArgumentException( 
    90                     "Resource must be an @EntityResource or @Entity."); 
     90                    "Resource must be an @EntityResource."); 
    9191        } 
    9292        this.entityIdClass = EntityHelper.findIdType(entityClass); 
  • trunk/src/main/java/org/sarugo/restlet/jpa/EntityRouter.java

    r3978 r3989  
    289289                if (!entities.containsKey(finder.getEntityClass())) { 
    290290                    entities.put(finder.getEntityClass(), route); 
    291                     finder.setIdAttribute(route.getTemplate() 
    292                             .getVariableNames().get(0)); 
    293                 } 
     291                } 
     292                finder.setIdAttribute(route.getTemplate() 
     293                        .getVariableNames().get(0)); 
    294294                getRoutes().add(route); 
    295295                return route; 
  • trunk/src/main/java/org/sarugo/restlet/jpa/PersistenceApplication.java

    r3677 r3989  
    5858    /** Clears the thread-local {@link EntityManager}. */ 
    5959    public void clearEntityManager() { 
    60         entityManager.set(null); 
     60        entityManager.remove(); 
    6161    } 
    6262 
  • trunk/src/main/java/org/sarugo/restlet/jpa/converter/Converter.java

    r3978 r3989  
    44import java.beans.PropertyEditorManager; 
    55import java.lang.reflect.Constructor; 
     6import java.text.DateFormat; 
     7import java.text.ParseException; 
     8import java.text.SimpleDateFormat; 
     9import java.util.Date; 
    610 
    711import org.sarugo.restlet.jpa.PersistenceApplication; 
     
    913public abstract class Converter { 
    1014 
    11     private final PersistenceApplication application; 
     15       private final PersistenceApplication application; 
    1216 
    13     public Converter(PersistenceApplication application) { 
    14         this.application = application; 
    15    
     17       public Converter(PersistenceApplication application) { 
     18               this.application = application; 
     19       
    1620 
    17     protected final PersistenceApplication getApplication() { 
    18         return application; 
    19    
     21       protected final PersistenceApplication getApplication() { 
     22               return application; 
     23       
    2024 
    21     protected <T extends Object> T convertToTypeOrEntity(Class<T> type, 
    22             String value) { 
    23         T result = null; 
    24         try { 
    25             result = type.cast(getApplication().getEntity(value)); 
    26         } catch (Exception e) { 
    27             // leave as null 
    28        
    29         if (result == null) { 
    30             result = convertToType(type, value); 
    31        
    32         return result; 
    33    
     25       protected <T extends Object> T convertToTypeOrEntity(Class<T> type, 
     26                       String value) { 
     27               T result = null; 
     28               try { 
     29                       result = type.cast(getApplication().getEntity(value)); 
     30               } catch (Exception e) { 
     31                       // leave as null 
     32               
     33               if (result == null) { 
     34                       result = convertToType(type, value); 
     35               
     36               return result; 
     37       
    3438 
    35     public static <T extends Object> T convertToType(Class<T> type, String value) { 
    36         // Short cut for String types 
    37         if (type.isAssignableFrom(String.class)) { 
    38             return type.cast(value); 
    39        
     39       public static <T extends Object> T convertToType(Class<T> type, String value) { 
     40               // Short cut for String types 
     41               if (type.isAssignableFrom(String.class)) { 
     42                       return type.cast(value); 
     43               
    4044 
    41         // Short cut for null 
    42         if (value == null) { 
    43             return null; 
    44        
     45               // Short cut for null 
     46               if (value == null) { 
     47                       return null; 
     48               
    4549 
    46         // Otherwise, check if the type is an enum 
    47         if (type.isEnum()) { 
    48             for (T constant : type.getEnumConstants()) { 
    49                 if (((Enum<?>) constant).name().equals(value)) { 
    50                     return constant; 
    51                
    52            
    53             return null; 
    54        
     50               // Otherwise, check if the type is an enum 
     51               if (type.isEnum()) { 
     52                       for (T constant : type.getEnumConstants()) { 
     53                               if (((Enum<?>) constant).name().equals(value)) { 
     54                                       return constant; 
     55                               
     56                       
     57                       return null; 
     58               
    5559 
    56         // Otherwise, use JavaBean coercion 
    57         try { 
    58             PropertyEditor e = PropertyEditorManager.findEditor(type); 
    59             e.setAsText(value); 
    60             return type.cast(e.getValue()); 
    61         } catch (Exception e) { 
    62             // Ignore 
    63         } 
     60                // Otherwise, check for date 
     61                if (type.isAssignableFrom(Date.class)) { 
     62                        try { 
     63                                return type.cast(new SimpleDateFormat("dd/MM/yyyy") 
     64                                                .parse(value)); 
     65                        } catch (ParseException e) { 
     66                                // Ignore 
     67                        } 
     68                } 
    6469 
    65         // Otherwise, try a String constructor 
    66         try { 
    67             Constructor<T> c = type.getConstructor(String.class); 
    68             return c.newInstance(value); 
    69         } catch (Exception e) { 
    70             // Ignore 
    71         } 
     70                // Otherwise, use JavaBean coercion 
     71                try { 
     72                        PropertyEditor e = PropertyEditorManager.findEditor(type); 
     73                        e.setAsText(value); 
     74                        return type.cast(e.getValue()); 
     75                } catch (Exception e) { 
     76                        // Ignore 
     77                } 
    7278 
    73         // If all else failed, just return null 
    74         return null; 
    75     } 
     79                // Otherwise, try a String constructor 
     80                try { 
     81                        Constructor<T> c = type.getConstructor(String.class); 
     82                        return c.newInstance(value); 
     83                } catch (Exception e) { 
     84                        // Ignore 
     85                } 
     86 
     87                // If all else failed, just return null 
     88                return null; 
     89        } 
    7690} 
  • trunk/src/main/java/org/sarugo/restlet/jpa/converter/XTCConverter.java

    r3978 r3989  
    3131 
    3232    public MediaType getMediaType() { 
    33         return MediaType.APPLICATION_XHTML_XML; 
     33        return MediaType.TEXT_HTML; 
    3434    } 
    3535