- Timestamp:
- 19/10/08 16:09:39 (3 years ago)
- Files:
-
- trunk/src/main/java/org/sarugo/restlet/jpa/EntityFinder.java (modified) (1 diff)
- trunk/src/main/java/org/sarugo/restlet/jpa/EntityRouter.java (modified) (1 diff)
- trunk/src/main/java/org/sarugo/restlet/jpa/PersistenceApplication.java (modified) (1 diff)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/Converter.java (modified) (2 diffs)
- trunk/src/main/java/org/sarugo/restlet/jpa/converter/XTCConverter.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/java/org/sarugo/restlet/jpa/EntityFinder.java
r3978 r3989 88 88 } else { 89 89 throw new IllegalArgumentException( 90 "Resource must be an @EntityResource or @Entity.");90 "Resource must be an @EntityResource."); 91 91 } 92 92 this.entityIdClass = EntityHelper.findIdType(entityClass); trunk/src/main/java/org/sarugo/restlet/jpa/EntityRouter.java
r3978 r3989 289 289 if (!entities.containsKey(finder.getEntityClass())) { 290 290 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)); 294 294 getRoutes().add(route); 295 295 return route; trunk/src/main/java/org/sarugo/restlet/jpa/PersistenceApplication.java
r3677 r3989 58 58 /** Clears the thread-local {@link EntityManager}. */ 59 59 public void clearEntityManager() { 60 entityManager. set(null);60 entityManager.remove(); 61 61 } 62 62 trunk/src/main/java/org/sarugo/restlet/jpa/converter/Converter.java
r3978 r3989 4 4 import java.beans.PropertyEditorManager; 5 5 import java.lang.reflect.Constructor; 6 import java.text.DateFormat; 7 import java.text.ParseException; 8 import java.text.SimpleDateFormat; 9 import java.util.Date; 6 10 7 11 import org.sarugo.restlet.jpa.PersistenceApplication; … … 9 13 public abstract class Converter { 10 14 11 private final PersistenceApplication application;15 private final PersistenceApplication application; 12 16 13 public Converter(PersistenceApplication application) {14 this.application = application;15 }17 public Converter(PersistenceApplication application) { 18 this.application = application; 19 } 16 20 17 protected final PersistenceApplication getApplication() {18 return application;19 }21 protected final PersistenceApplication getApplication() { 22 return application; 23 } 20 24 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 null28 }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 } 34 38 35 public static <T extends Object> T convertToType(Class<T> type, String value) {36 // Short cut for String types37 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 } 40 44 41 // Short cut for null42 if (value == null) {43 return null;44 }45 // Short cut for null 46 if (value == null) { 47 return null; 48 } 45 49 46 // Otherwise, check if the type is an enum47 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 } 55 59 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 } 64 69 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 } 72 78 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 } 76 90 } trunk/src/main/java/org/sarugo/restlet/jpa/converter/XTCConverter.java
r3978 r3989 31 31 32 32 public MediaType getMediaType() { 33 return MediaType. APPLICATION_XHTML_XML;33 return MediaType.TEXT_HTML; 34 34 } 35 35
