@Test public void deleteStudy_shouldDeleteGivenStudy() throws Exception { final String studyName = "Study Name"; List<StudyDef> studies = studyManagerService.getStudies(); Assert.assertEquals("There are 4 studies", 4, studies.size()); Assert.assertNull(getStudy(studyName, studies)); StudyDef study = new StudyDef(); study.setName(studyName); study.setCreator(userService.getUsers().get(0)); study.setDateCreated(new Date()); studyManagerService.saveStudy(study); studies = studyManagerService.getStudies(); Assert.assertEquals("Added 1 study, now there are 5", 5, studies.size()); study = getStudy(studyName, studies); Assert.assertNotNull(study); studyManagerService.deleteStudy(study); studies = studyManagerService.getStudies(); Assert.assertEquals("Deleted the study so there are 4 studies again", 4, studies.size()); Assert.assertNull(getStudy(studyName, studies)); }
/** * Gets a study object for a given name from a list of study objects. * * @param name the name of the study to look for. * @param studies the list of study objects. * @return the study object that matches the given name. */ private StudyDef getStudy(String name, List<StudyDef> studies) { for (StudyDef study : studies) { if (study.getName().equals(name)) return study; } return null; }
/** * Adds a new Item according to selected item on the <tt>Tree View.</tt> * * <p>If a <Study</tt> is selected, a new <tt>Study</tt> will be added. * * <p>If a <tt>Form</tt> is selected, a new <tt>Form</tt> will be added. * * <p>If a <tt>Form Version</tt> is selected, a new <tt>Form Version</tt> will be added. */ public static void addNewItem(String xForm, Tree tree, List<StudyDef> studies, PopupPanel popup) { TreeItem item = tree.getSelectedItem(); if (item == null || item.getUserObject() instanceof StudyDef) { StudyDef studyDef = new StudyDef(0, "New Study" + (tree.getItemCount() + 1)); studyDef.setCreator(Context.getAuthenticatedUser()); studyDef.setDateCreated(new Date()); studyDef.setDirty(true); TreeItem root = new CompositeTreeItem(new TreeItemWidget(images.note(), studyDef.getName(), popup)); root.setUserObject(studyDef); tree.addItem(root); studies.add(studyDef); tree.setSelectedItem(root); // Automatically add a new form addNewChildItem(tree, studies, popup); } else if (item.getUserObject() instanceof FormDef) { TreeItem parent = item.getParentItem(); FormDef formDef = new FormDef( 0, "New Form" + (parent.getChildCount() + 1), (StudyDef) parent.getUserObject()); formDef.setCreator(Context.getAuthenticatedUser()); formDef.setDateCreated(new Date()); formDef.setDirty(true); item = addImageItem(parent, formDef.getName(), images.drafts(), formDef, null, popup); ((StudyDef) parent.getUserObject()).addForm(formDef); tree.setSelectedItem(item); parent.setState(true); // Automatically add a new form version addNewChildItem(tree, studies, popup); } else if (item.getUserObject() instanceof FormDefVersion) { TreeItem parent = item.getParentItem(); FormDefVersion formDefVersion = new FormDefVersion( 0, "v" + (parent.getChildCount() + 1), (FormDef) parent.getUserObject()); formDefVersion.setCreator(Context.getAuthenticatedUser()); formDefVersion.setDateCreated(new Date()); formDefVersion.getFormDef().turnOffOtherDefaults(formDefVersion); formDefVersion.setDirty(true); if (xForm != null) formDefVersion.setXform(xForm); item = addImageItem( parent, formDefVersion.getName(), images.markRead(), formDefVersion, null, popup); ((FormDef) parent.getUserObject()).addVersion(formDefVersion); tree.setSelectedItem(item); parent.setState(true); } }
/** * Loads a study and its contents in this view. * * @param studyDef the study definition object. */ public static void loadStudy(StudyDef studyDef) { TreeItem studyRoot = new CompositeTreeItem(new TreeItemWidget(images.note(), studyDef.getName(), popup)); studyRoot.setUserObject(studyDef); tree.addItem(studyRoot); if (studyDef.getForms() != null) { for (FormDef def : studyDef.getForms()) loadForm(def, studyRoot, false); } Utilities.selectFirstItemOnTreeView(tree); }
/** * Removes the given <tt>Form Def Tree Item</tt> from the Parent. * * @param item <tt>TreeItem</tt> to remove. * @param parent <tt>Parent</tt> of <tt>Tree Item.</tt> */ public static void removeFormDefItem(TreeItem item, TreeItem parent) { Object userObj = item.getUserObject(); Object parentUserObj = parent.getUserObject(); if (userObj instanceof FormDefVersion) { ((FormDef) parentUserObj).removeVersion((FormDefVersion) userObj); ((FormDef) parentUserObj).setDirty(true); ((StudyDef) parent.getParentItem().getUserObject()).setDirty(true); } else if (userObj instanceof FormDef) { ((StudyDef) parentUserObj).removeForm((FormDef) userObj); ((StudyDef) parentUserObj).setDirty(true); } }
/** * Loads User Mapped Studies. * * @param studies List of <tt>StudyDefs</tt> from which to filter out <tt>UserStudyMaps.</tt> * @param user <tt>User</tt> to load <tt>StudyDefs</tt> for. */ public static void loadStudiesAccordingToUserPrivileges( List<StudyDef> studies, User user, List<UserStudyMap> mappedStudies) { if (mappedStudies != null) { List<UserStudyMap> userMappedStudies = RolesListUtil.getPermissionResolver().getUserMappedStudies(user, mappedStudies); if (userMappedStudies != null && userMappedStudies.size() > 0) { for (UserStudyMap x : userMappedStudies) { for (StudyDef def : studies) { if (x.getStudyId() == def.getStudyId()) loadStudy(def); } } } } }
/** * Sets the Properties of the selected Item on the <tt>Tree View.</tt> * * @param item <tt>Tree Item</tt> to set properties for. * @param tree <tt>Tree View</tt> to set properties on. * @param popup <tt>Popup</tt> for the <tt>Tree View.</tt> */ public static void changeEditableProperties(Object item, Tree tree, PopupPanel popup) { TreeItem treeItem = tree.getSelectedItem(); if (item == null) return; // How can this happen? if (item instanceof StudyDef) { StudyDef studyDef = (StudyDef) item; treeItem.setWidget(new TreeItemWidget(images.note(), studyDef.getName(), popup)); treeItem.setTitle(studyDef.getDescription()); studyDef.setDirty(true); } else if (item instanceof FormDef) { FormDef formDef = (FormDef) item; treeItem.setWidget(new TreeItemWidget(images.drafts(), formDef.getName(), popup)); treeItem.setTitle(formDef.getDescription()); formDef.setDirty(true); } else if (item instanceof FormDefVersion) { FormDefVersion formDefVersion = (FormDefVersion) item; treeItem.setWidget(new TreeItemWidget(images.markRead(), formDefVersion.getName(), popup)); treeItem.setTitle(formDefVersion.getDescription()); formDefVersion.setDirty(true); } }
/** * Constructs a <code>StudyDef Tree item root</code> to bind other items to. * * @param xDef <code>StudyDef</code> for whom to create root. * @return Constructed <code>Tree Item.</code> */ private static TreeItem constructTreeItem(StudyDef xDef) { TreeItem root = new CompositeTreeItem(new TreeItemWidget(images.note(), xDef.getName(), popup)); if (root.getUserObject() != null) { if (!root.getUserObject().equals(xDef)) { root.setUserObject(xDef); tree.addItem(root); } } else { root.setUserObject(xDef); tree.addItem(root); } return root; }
/** * Bind <tt>StudyDef</tt> contents. * * @param tree <tt>Tree View</tt> to bind <tt>StudyDef</tt> to. * @param popup <tt>Popup</tt> to bind to the <tt>Tree View.</tt> * @param editable <tt>Editable</tt> we are checking contents for. */ private static void setStudyContent(final Tree tree, final PopupPanel popup, Editable editable) { StudyDef studyDef = (StudyDef) editable; studyDef.setCreator(Context.getAuthenticatedUser()); studyDef.setDateCreated(new Date()); studyDef.setDirty(true); TreeItem root = new CompositeTreeItem(new TreeItemWidget(images.note(), studyDef.getName(), popup)); root.setUserObject(studyDef); tree.addItem(root); Context.getStudies().add(studyDef); tree.setSelectedItem(root); List<FormDef> forms = studyDef.getForms(); if (forms != null) { for (FormDef formDef : forms) importForm(tree, root, formDef, popup); } }