diff --git a/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentCreateStep.java b/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentCreateStep.java index afd7ba2..287e8a2 100644 --- a/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentCreateStep.java +++ b/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentCreateStep.java @@ -1,13 +1,16 @@ package org.scientificcms.contenttypes.scidepartment.ui; import org.libreccm.l10n.GlobalizationHelper; +import org.libreccm.l10n.LocalizedString; +import org.libreccm.workflow.Workflow; import org.librecms.contentsection.ContentItemManager; import org.librecms.contentsection.ContentItemRepository; import org.librecms.ui.contentsections.documents.AbstractMvcDocumentCreateStep; import org.scientificcms.contenttypes.scidepartment.SciDepartment; -import org.scientificcms.contenttypes.sciproject.SciProject; +import java.util.Locale; import java.util.Map; +import java.util.Optional; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; @@ -118,7 +121,7 @@ public class SciDepartmentCreateStep .stream() .filter( type -> type.getContentItemClass().equals( - SciProject.class.getName() + SciDepartment.class.getName() ) ) .findAny() @@ -140,8 +143,124 @@ public class SciDepartmentCreateStep } @Override - public String createItem(Map maps) { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + public String createItem(final Map formParams) { + if (!formParams.containsKey(FORM_PARAM_NAME) + || formParams.get(FORM_PARAM_NAME) == null + || formParams.get(FORM_PARAM_NAME).length == 0) { + addMessage( + "danger", + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("createstep.name.error.missing") + ); + return showCreateStep(); + } + + name = formParams.get(FORM_PARAM_NAME)[0]; + if (!name.matches("^([a-zA-Z0-9_-]*)$")) { + addMessage( + "danger", + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("createstep.name.error.invalid") + ); + return showCreateStep(); + } + + if (!formParams.containsKey(FORM_PARAM_TITLE) + || formParams.get(FORM_PARAM_TITLE) == null + || formParams.get(FORM_PARAM_TITLE).length == 0) { + addMessage( + "danger", + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("createstep.title.error.missing") + ); + return showCreateStep(); + } + title = formParams.get(FORM_PARAM_TITLE)[0]; + + if (!formParams.containsKey(FORM_PARAM_SHORT_DESCRIPTION) + || formParams.get(FORM_PARAM_SHORT_DESCRIPTION) == null + || formParams.get(FORM_PARAM_SHORT_DESCRIPTION).length == 0) { + addMessage( + "danger", + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("createstep.summary.error.missing") + ); + return showCreateStep(); + } + shortDescription = formParams.get(FORM_PARAM_SHORT_DESCRIPTION)[0]; + + if (!formParams.containsKey(FORM_PARAM_INITIAL_LOCALE) + || formParams.get(FORM_PARAM_INITIAL_LOCALE) == null + || formParams.get(FORM_PARAM_INITIAL_LOCALE).length == 0) { + addMessage( + "danger", + globalizationHelper.getLocalizedTextsUtil( + getBundle() + ).getText("createstep.initial_locale.error.missing") + ); + return showCreateStep(); + } + final Locale locale = new Locale( + formParams.get(FORM_PARAM_INITIAL_LOCALE)[0] + ); + + if (!formParams.containsKey(FORM_PARAM_SELECTED_WORKFLOW) + || formParams.get(FORM_PARAM_SELECTED_WORKFLOW) == null + || formParams.get(FORM_PARAM_SELECTED_WORKFLOW).length == 0) { + addMessage( + "danger", + globalizationHelper.getLocalizedTextsUtil( + getBundle() + ).getText("createstep.workflow.none_selected") + ); + return showCreateStep(); + } + + selectedWorkflow = formParams.get(FORM_PARAM_SELECTED_WORKFLOW)[0]; + + final Optional workflowResult = getContentSection() + .getWorkflowTemplates() + .stream() + .filter(template -> template.getUuid().equals(selectedWorkflow)) + .findAny(); + + if (!workflowResult.isPresent()) { + addMessage( + "danger", + globalizationHelper.getLocalizedTextsUtil( + getBundle() + ).getText("createstep.workflow.error.not_available") + ); + return showCreateStep(); + } + + if (!getMessages().isEmpty()) { + return showCreateStep(); + } + + final SciDepartment department = itemManager.createContentItem( + name, + getContentSection(), + getFolder(), + workflowResult.get(), + SciDepartment.class, + locale + ); + + department.getTitle().putValue(locale, title); + department.getShortDescription().putValue(locale, shortDescription); + itemRepo.save(department); + + return String.format( + "redirect:%s/documents/%s/%s/@scidepartment-basicproperties", + getContentSectionLabel(), + getFolderPath(), + name + ); } } diff --git a/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentDescriptionMembersModel.java b/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentDescriptionMembersModel.java index 517219a..71f40a7 100644 --- a/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentDescriptionMembersModel.java +++ b/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentDescriptionMembersModel.java @@ -37,7 +37,7 @@ public class SciDepartmentDescriptionMembersModel { return Collections.unmodifiableList(members); } - protected void setMembers(final List member) { + protected void setMembers(final List members) { this.members = new ArrayList<>(members); } diff --git a/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentDescriptionStep.java b/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentDescriptionStep.java index 085f5b4..04db08b 100644 --- a/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentDescriptionStep.java +++ b/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentDescriptionStep.java @@ -1086,10 +1086,15 @@ public class SciDepartmentDescriptionStep extends AbstractMvcAuthoringStep { ); textsModel.setAvailableTextKeys( texts - .keySet() + .entrySet() .stream() - .filter(key -> !textsModel.getTexts().containsKey(key)) - .collect(Collectors.toList()) + .filter(entry -> !textsModel.getTexts().containsKey(entry.getKey())) + .collect( + Collectors.toMap( + entry -> entry.getKey(), + entry -> entry.getValue() + ) + ) ); contactsModel.setCanEdit(canEdit); diff --git a/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentTextsModel.java b/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentTextsModel.java index 6b0c1db..2ee4060 100644 --- a/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentTextsModel.java +++ b/sci-types-department/src/main/java/org/scientificcms/contenttypes/scidepartment/ui/SciDepartmentTextsModel.java @@ -1,10 +1,10 @@ package org.scientificcms.contenttypes.scidepartment.ui; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.List; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.SortedMap; import javax.enterprise.context.RequestScoped; import javax.inject.Named; @@ -19,7 +19,7 @@ public class SciDepartmentTextsModel { private boolean canEdit; - private List availableTextKeys; + private Map availableTextKeys; private Map texts; @@ -35,12 +35,14 @@ public class SciDepartmentTextsModel { this.canEdit = canEdit; } - public List getAvailableTextKeys() { - return Collections.unmodifiableList(availableTextKeys); + public Map getAvailableTextKeys() { + return Collections.unmodifiableMap(availableTextKeys); } - protected void setAvailableTextKeys(final List availableTextKeys) { - this.availableTextKeys = new ArrayList<>(availableTextKeys); + protected void setAvailableTextKeys( + final Map availableTextKeys + ) { + this.availableTextKeys = new LinkedHashMap<>(availableTextKeys); } public Map getTexts() { @@ -48,7 +50,7 @@ public class SciDepartmentTextsModel { } protected void setTexts(final Map texts) { - this.texts = new HashMap<>(texts); + this.texts = new LinkedHashMap<>(texts); } public String getSelectedText() { diff --git a/sci-types-department/src/main/resources/WEB-INF/views/org/scientificcms/contenttypes/scidepartment/ui/create-scidepartment.xhtml b/sci-types-department/src/main/resources/WEB-INF/views/org/scientificcms/contenttypes/scidepartment/ui/create-scidepartment.xhtml index c669db6..5070c91 100644 --- a/sci-types-department/src/main/resources/WEB-INF/views/org/scientificcms/contenttypes/scidepartment/ui/create-scidepartment.xhtml +++ b/sci-types-department/src/main/resources/WEB-INF/views/org/scientificcms/contenttypes/scidepartment/ui/create-scidepartment.xhtml @@ -52,9 +52,9 @@