/** * Copies an act item for each product referred to in its template. * * @param editor the editor * @param template the product template * @return the acts generated from the template */ protected List<Act> createTemplateActs(ActItemEditor editor, Product template) { List<Act> result = new ArrayList<>(); ActRelationshipCollectionPropertyEditor collection = getEditor(); IMObjectCopier copier = new IMObjectCopier(new ActItemCopyHandler()); Collection<TemplateProduct> includes = getProductIncludes(template, editor.getPatient()); Act act = (Act) editor.getObject(); Act copy = act; // replace the existing act with the first Date startTime = act.getActivityStartTime(); for (TemplateProduct include : includes) { if (copy == null) { // copy the act, and associate the product List<IMObject> objects = copier.apply(act); copy = (Act) objects.get(0); LayoutContext context = new DefaultLayoutContext(getContext()); context.setComponentFactory(new ReadOnlyComponentFactory(context)); editor = (ActItemEditor) createEditor(copy, context); // reset the start-time, which may have been set by the editor copy.setActivityStartTime(startTime); // create the component - must do this to ensure that the product editor is created editor.getComponent(); } setTemplateProduct(editor, template, include); collection.add(copy); collection.setEditor(copy, editor); setModified(copy, true); result.add(copy); copy = null; } return result; }
/** * Helper to copy an act. * * @param object the object to copy * @param type the expected type of the object * @param handler the copy handler * @param startTime the start time of the copied object * @param save if <tt>true</tt>, save the copied objects * @return the copied objects */ private List<IMObject> copy( Act object, String type, IMObjectCopyHandler handler, Date startTime, boolean save) { if (!TypeHelper.isA(object, type)) { throw new IllegalArgumentException( "Expected a " + type + " for argument 'object'" + ", but got a" + object.getArchetypeId().getShortName()); } IMObjectCopier copier = new IMObjectCopier(handler, service); List<IMObject> objects = copier.apply(object); Act act = (Act) objects.get(0); act.setActivityStartTime(startTime); if (save) { service.save(objects); } return objects; }