r3924 - in trunk: pollen-rest-api/src/main/java/org/chorem/pollen/rest/api pollen-services/src/main/java/org/chorem/pollen/services
Author: tchemit Date: 2014-05-09 07:47:25 +0200 (Fri, 09 May 2014) New Revision: 3924 Url: http://forge.chorem.org/projects/pollen/repository/revisions/3924 Log: improve copy visitor + remove old code in Render Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/TopiaEntityCopyVisitor.java Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java 2014-05-08 12:57:53 UTC (rev 3923) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRender.java 2014-05-09 05:47:25 UTC (rev 3924) @@ -49,19 +49,6 @@ */ public class PollenRender<T> extends Render { -// /** -// * To specify entity collections to include in binded entities. -// * -// * @author tchemit <chemit@codelutin.com> -// * @since 2.0 -// */ -// @Retention(RetentionPolicy.RUNTIME) -// @Target(ElementType.METHOD) -// public @interface ExposeCollection { -// -// public String[] values() default {}; -// } - protected T model; public PollenRender(T model) { @@ -75,12 +62,6 @@ HttpServletResponse response = context.getResponse(); response.setContentType("application/json"); -// String[] includeCollection = null; -// ExposeCollection annotation = call.getCurrent().getMethod().getAnnotation(ExposeCollection.class); -// if (annotation != null) { -// includeCollection = annotation.values(); -// } - Object map; ObjectMapper mapper; @@ -125,7 +106,6 @@ } - // protected <M> Object toMap(M model, String... includeCollection) { protected <M> Object toMap(M model) { if (model instanceof InvalidFormException) { @@ -157,140 +137,6 @@ return model; -// if (model instanceof Iterable<?>) { -// -// // collection of objects -// Iterable<?> objects = (Iterable<?>) model; -// List<Object> result = Lists.newArrayList(); -// for (Object object : objects) { -// Object objectMap = toMap(object, includeCollection); -// result.add(objectMap); -// } -// -// return result; -// -// } -// -// // single object -// // FIXME Do this in service, not here -// -// if (model instanceof TopiaEntity) { -// -// // entity need to transform it -// -// TopiaEntity topiaEntity = (TopiaEntity) model; -// -// Class<? extends TopiaEntity> entitytype = PollenEntityEnum.getContractClass(topiaEntity.getClass()); -// PollenEntityBinder<TopiaEntity> binder = BinderFactory.newBinder(entitytype, entitytype, null, PollenEntityBinder.class); -// Map<String, Object> map = binder.obtainProperties(topiaEntity); -// Map<String, Object> result = Maps.newTreeMap(); -// -// for (Map.Entry<String, Object> entry : map.entrySet()) { -// String propertyName = entry.getKey(); -// Object propertyValue = entry.getValue(); -// if (propertyValue instanceof TopiaEntity) { -// result.put(propertyName, toMap(propertyValue)); -// continue; -// } -// -// result.put(propertyName, propertyValue); -// -// } -// -// // treat collections -// -// if (includeCollection != null) { -// for (String propertyName : includeCollection) { -// Iterable<?> collectionValue = binder.getCollectionValue(topiaEntity, propertyName); -// result.put(propertyName, toMap(collectionValue)); -// -// } -// } -// -// return result; -// -// } - -// return model; - } -// protected static class PollenEntityBinder<E extends TopiaEntity> extends Binder<E, E> { -// -// /** Logger. */ -// private static final Log log = -// LogFactory.getLog(PollenEntityBinder.class); -// -// private static final long serialVersionUID = 1L; -// -// protected List<String> simpleProperties; -// -// public PollenEntityBinder() { -// } -// -// @Override -// protected void setModel(BinderModel<E, E> model) { -// super.setModel(model); -// this.simpleProperties = Lists.newArrayList(); -// for (String property : model.getSourceDescriptors()) { -// if (model.getCollectionType(property) == null) { -// simpleProperties.add(property); -// } -// } -// } -// -// /** -// * Obtain from the given object all properties registered in the binder -// * model. -// * -// * @param source the bean to read -// * @return the map of properties obtained indexed by their property name, -// * or an empty map is the given {@code from} is {@code null}. -// * @since 2.3 -// */ -// public Map<String, Object> obtainProperties(E source) { -// if (source == null) { -// // special limit case -// return Collections.emptyMap(); -// } -// -// Map<String, Object> result = Maps.newTreeMap(); -// for (String sourceProperty : simpleProperties) { -// -// -// try { -// Object read; -// Method readMethod = model.getSourceReadMethod(sourceProperty); -// read = readMethod.invoke(source); -// if (log.isDebugEnabled()) { -// log.debug("property " + sourceProperty + ", type : " + -// readMethod.getReturnType() + ", value = " + read); -// } -// if (readMethod.getReturnType().isPrimitive() && -// ObjectUtil.getNullValue( -// readMethod.getReturnType()).equals(read)) { -// // for primitive type case, force nullity -// read = null; -// } -// -// if (read != null) { -// result.put(sourceProperty, read); -// } -// } catch (IllegalAccessException | InvocationTargetException e) { -// throw new PollenTechnicalException(e); -// } -// } -// return result; -// } -// -// public Iterable<?> getCollectionValue(E source, String sourceProperty) { -// try { -// Method readMethod = model.getSourceReadMethod(sourceProperty); -// Object result = readMethod.invoke(source); -// return (Iterable<?>) result; -// } catch (IllegalAccessException | InvocationTargetException e) { -// throw new PollenTechnicalException(e); -// } -// } -// } } Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/TopiaEntityCopyVisitor.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/TopiaEntityCopyVisitor.java 2014-05-08 12:57:53 UTC (rev 3923) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/TopiaEntityCopyVisitor.java 2014-05-09 05:47:25 UTC (rev 3924) @@ -54,7 +54,7 @@ * <p/> * When a entity is started, the previous current entity goes at the top of the stack. * <p/> - * When a entity is ended, the current entity goes is marked as the last ended, + * When a entity is ended, the current entity goes is marked as the last, * and the last entity of the stack goes back to the current state. */ protected Deque<TopiaEntity> stack = new ArrayDeque<>(); @@ -65,9 +65,9 @@ protected TopiaEntity current; /** - * The last ended entity (means the last entity entering in the {@link #end(TopiaEntity)} method). + * The last entity (means the last entity entering in the {@link #end(TopiaEntity)} method). */ - protected TopiaEntity lastEnded; + protected TopiaEntity last; /** * To get entity concrete class from a entity instance. @@ -95,7 +95,7 @@ source.accept(visitor); - E target = (E) visitor.lastEnded; + E target = (E) visitor.last; return target; @@ -123,7 +123,7 @@ try { - E target = (E) visitor.lastEnded; + E target = visitor.copy(source); targets.add(target); @@ -148,7 +148,7 @@ @Override public void start(TopiaEntity source) { - boolean accept = acceptEntity(source); + boolean accept = source != null && acceptEntity(source); if (accept) { @@ -184,15 +184,16 @@ @Override public void end(TopiaEntity source) { - boolean accept = acceptEntity(source); + boolean accept = source != null && acceptEntity(source); if (accept) { - lastEnded = current; + last = current; if (!stack.isEmpty()) { current = stack.pop(); + } } @@ -201,71 +202,38 @@ @Override public void visit(TopiaEntity entity, String propertyName, Class<?> type, Object source) { - if (source != null) { + Object target = copy(source); - Object target = copy(source); + setPropertyValue(propertyName, target); - if (target != null) { - - setPropertyValue(propertyName, target); - - } - - } } @Override - public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, Object source) { + public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, Object sources) { - if (source != null) { + if (sources != null) { - Object target = createCollectionInstance(source); + Collection<Object> targets = createCollectionInstance(sources); - setPropertyValue(propertyName, target); + setPropertyValue(propertyName, targets); - } + Collection<?> collectionSource = (Collection<?>) sources; - } + for (Object source : collectionSource) { - @Override - public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, int index, Object source) { + Object target = copy(source); - if (source != null) { + targets.add(target); - Object target = copy(source); - - // get the collection - - Collection<?> collection; - try { - - collection = (Collection<?>) propertyUtilsBean.getSimpleProperty(current, propertyName); - - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - - throw new PollenTechnicalException("Could not get property " + propertyName, e); - } - // ensure size is ok + } - while (collection.size() <= index) { + } - collection.add(null); + @Override + public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, int index, Object source) { - } - - try { - - propertyUtilsBean.setIndexedProperty(current, propertyName, index, target); - - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - - throw new PollenTechnicalException("Could not set indexed property " + propertyName + " at index " + index, e); - - } - - } } @Override @@ -273,20 +241,20 @@ stack.clear(); alreadyExplored.clear(); - lastEnded = null; + last = null; current = null; } - protected Object copy(Object source) { + protected <O> O copy(O source) { - Object target; + O target; - if (acceptEntity(source)) { + if (source != null && acceptEntity(source)) { ((TopiaEntity) source).accept(this); - target = lastEnded; + target = (O) last; } else {
participants (1)
-
tchemit@users.chorem.org