public static Iterable freemarkerWrapperToIterable(Object object) { if (object == null) throw new WindupException("Illegal null passed to freemarkerWrapperToIterable method!"); if (object instanceof DefaultListAdapter) { return (Iterable)((DefaultListAdapter) object).getWrappedObject(); } else if (object instanceof DefaultIterableAdapter) { return (Iterable)((DefaultIterableAdapter) object).getWrappedObject(); } else { throw new WindupException("Unrecognized type: " + object.getClass()); } }
@Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { SimpleScalar name = (SimpleScalar) params.get("name"); if (params.containsKey("value")) { Object valueObject = params.get("value"); Object value; if (valueObject == null) { value = null; } else if (valueObject instanceof WrapperTemplateModel) { value = ((WrapperTemplateModel) valueObject).getWrappedObject(); } else if (valueObject instanceof TemplateScalarModel) { value = ((TemplateScalarModel) valueObject).getAsString(); } else if (valueObject instanceof TemplateNumberModel) { value = ((TemplateNumberModel) valueObject).getAsNumber(); } else if (valueObject instanceof TemplateDateModel) { value = ((TemplateDateModel) valueObject).getAsDate(); } else if (valueObject instanceof TemplateBooleanModel) { value = ((TemplateBooleanModel) valueObject).getAsBoolean(); } else { throw new UnsupportedOperationException( String.format("Type %s is not supported yet in this context.", valueObject.getClass().getSimpleName())); } TemplateModel generatedParamsObject = env.getGlobalVariables().get(FreeMarkerSqlSource.GENERATED_PARAMS_KEY); List generatedParams; if (generatedParamsObject instanceof DefaultListAdapter) { generatedParams = (List) ((DefaultListAdapter) generatedParamsObject).getWrappedObject(); } else { generatedParams = ((GeneratedParamsTemplateModel) generatedParamsObject).getGeneratedParams(); } String generatedParamName = "_p" + generatedParams.size(); env.getOut().write(String.format("#{%s}", generatedParamName)); generatedParams.add(value); } else { env.getOut().write(String.format("#{%s}", name)); } }
@Override public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException { ExecutionStatistics.get().begin(NAME); if (arguments.size() != 1) throw new TemplateModelException("Error, method expects one argument (an Iterable)"); DefaultListAdapter listModelArg = (DefaultListAdapter) arguments.get(0); List<ApplicationReportModel> list = (List<ApplicationReportModel>) listModelArg.getWrappedObject(); Collections.sort(list, new CreateApplicationListReportRuleProvider.AppRootFileNameComparator()); ExecutionStatistics.get().end(NAME); return list; }
@SuppressWarnings("unchecked") private Iterable<Object> getIterable(Object arg) throws TemplateModelException { if (arg instanceof BeanModel) { BeanModel beanModel = (BeanModel) arg; return (Iterable<Object>) beanModel.getWrappedObject(); } else if (arg instanceof SimpleSequence) { SimpleSequence simpleSequence = (SimpleSequence) arg; return (Iterable<Object>) simpleSequence.toList(); } else if (arg instanceof DefaultIterableAdapter) { DefaultIterableAdapter adapter = (DefaultIterableAdapter) arg; return (Iterable<Object>) adapter.getAdaptedObject(Iterable.class); } else if (arg instanceof DefaultListAdapter) { DefaultListAdapter defaultListAdapter = (DefaultListAdapter) arg; return (Iterable<Object>) defaultListAdapter.getWrappedObject(); } else { throw new WindupException("Unrecognized type passed to: " + getMethodName() + ": " + arg.getClass().getCanonicalName()); } }
/** * Supposed to convert to simple sequence. * <p> * WARN: Bypasses auto-escaping for complex maps, caller must decide how to handle. * (e.g. the object wrapper used to rewrap the result). * <p> * DEV NOTE: I stopped writing/testing this when found out most of the problems w.r.t. collections are not * the FTL types this time but the way they're used in Ofbiz templates. * FTL's CollectionModel (subclass of TemplateCollectionModel) is supposed to cover everything and * won't suffer from the same problems maps have. */ @SuppressWarnings("unchecked") @Deprecated private static TemplateSequenceModel toSimpleSequence(TemplateModel object, ObjectWrapper objectWrapper) throws TemplateModelException { if (object instanceof TemplateSequenceModel) { return (TemplateSequenceModel) object; } else if (object instanceof WrapperTemplateModel) { WrapperTemplateModel wrapperModel = (WrapperTemplateModel) object; // WARN: bypasses auto-escaping Object wrappedObject = wrapperModel.getWrappedObject(); if (wrappedObject instanceof List) { return DefaultListAdapter.adapt((List<Object>) wrappedObject, (RichObjectWrapper) objectWrapper); } else if (wrappedObject instanceof Object[]) { return DefaultArrayAdapter.adapt((Object[]) wrappedObject, (ObjectWrapperAndUnwrapper) objectWrapper); } else if (wrappedObject instanceof Set) { throw new UnsupportedOperationException("Not yet implemented"); } else if (wrappedObject instanceof Collection) { throw new UnsupportedOperationException("Not yet implemented"); } else if (wrappedObject instanceof Iterable) { throw new UnsupportedOperationException("Not yet implemented"); } else { throw new TemplateModelException("Cannot convert bean-wrapped object of type " + (object != null ? object.getClass() : "null") + " to simple sequence"); } } else if (object instanceof TemplateCollectionModel) { TemplateCollectionModel collModel = (TemplateCollectionModel) object; SimpleSequence res = new SimpleSequence(objectWrapper); TemplateModelIterator it = collModel.iterator(); while(it.hasNext()) { res.add(it.next()); } return res; } else { throw new TemplateModelException("Cannot convert object of type " + (object != null ? object.getClass() : "null") + " to simple sequence"); } }