diff --git a/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectContactAddForm.java b/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectContactAddForm.java index 648ea03..0d06620 100644 --- a/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectContactAddForm.java +++ b/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectContactAddForm.java @@ -5,8 +5,11 @@ */ package com.arsdigita.cms.contenttypes.ui; +import com.arsdigita.bebop.FormData; import com.arsdigita.bebop.FormProcessException; import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.Text; import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.event.FormSubmissionListener; import com.arsdigita.bebop.event.PrintEvent; @@ -27,6 +30,7 @@ import org.apache.logging.log4j.Logger; import org.libreccm.cdi.utils.CdiUtil; import org.librecms.CmsConstants; import org.librecms.assets.ContactableEntity; +import org.scientificcms.contenttypes.sciproject.SciProject; import org.scientificcms.contenttypes.sciproject.SciProjectConstants; import java.util.ResourceBundle; @@ -53,7 +57,7 @@ public class SciProjectContactAddForm private SciProjectContactsStep editStep; - private Label selectedContactLabel; + private Text selectedContactLabel; public SciProjectContactAddForm(final ItemSelectionModel itemModel, final SciProjectContactsStep editStep, @@ -74,7 +78,7 @@ public class SciProjectContactAddForm SciProjectConstants.SCI_PROJECT_BUNDLE)); add(searchWidget); - selectedContactLabel = new Label(); + selectedContactLabel = new Text(); add(selectedContactLabel); final ParameterModel contactTypeParam @@ -127,21 +131,110 @@ public class SciProjectContactAddForm @Override public void init(final FormSectionEvent event) throws FormProcessException { - throw new UnsupportedOperationException("ToDo"); + final FormData data = event.getFormData(); + final PageState state = event.getPageState(); + + final ContactableEntity selectedContact = editStep.getSelectedContact(); + final String selectedType = editStep.getSelectedContactType(); + + if (selectedContact == null) { + selectedContactLabel.setVisible(state, false); + } else { + data.put(SEARCH, selectedContact); + data.put(CONTACT_TYPE, selectedType); + + searchWidget.setVisible(state, false); + selectedContactLabel.setText(selectedContact.getDisplayName()); + selectedContactLabel.setVisible(state, true); + } + + setVisible(state, true); } @Override public void process(final FormSectionEvent event) throws FormProcessException { - throw new UnsupportedOperationException("ToDo"); + final FormData data = event.getFormData(); + final PageState state = event.getPageState(); + final SciProject project = (SciProject) getItemSelectionModel() + .getSelectedItem(state); + + if (getSaveCancelSection().getSaveButton().isSelected(state)) { + + final ContactableEntity selected = editStep.getSelectedContact(); + + if (selected == null) { + final ContactableEntity contact = (ContactableEntity) data + .get(SEARCH); + + final String type = (String) data.get(CONTACT_TYPE); + + getController().addContact(project.getObjectId(), + contact.getObjectId(), + type); + } else { + + final String type = (String) data.get(CONTACT_TYPE); + + getController().updateContactType(project.getObjectId(), + selected.getObjectId(), + type); + } + } + + init(event); } @Override public void submitted(final FormSectionEvent event) throws FormProcessException { - throw new UnsupportedOperationException("ToDo"); + final PageState state = event.getPageState(); + if (getSaveCancelSection().getCancelButton().isSelected(state)) { + + editStep.setSelectedContact(null); + editStep.setSelectedContactType(null); + + init(event); + } + } + + @Override + public void validate(final FormSectionEvent event) + throws FormProcessException { + + final PageState state = event.getPageState(); + final FormData data = event.getFormData(); + + if (editStep.getSelectedContact() == null + && (data.get(SEARCH) == null)) { + + data.addError(new GlobalizedMessage( + "cms.contenttypes.ui.sciproject.select_contact.no_contact_selected", + SciProjectConstants.SCI_PROJECT_BUNDLE)); + + return; + } + + if (editStep.getSelectedContact() == null) { + + final SciProject project = (SciProject) getItemSelectionModel() + .getSelectedItem(state); + + final ContactableEntity selected = (ContactableEntity) data + .get(SEARCH); + + if (getController().hasContact(project.getObjectId(), + selected.getObjectId())) { + + data.addError(new GlobalizedMessage( + "cms.contenttypes.ui.sciproject.select_contact.already_added", + SciProjectConstants.SCI_PROJECT_BUNDLE + )); + } + } + } private SciProjectController getController() { diff --git a/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectContactsStep.java b/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectContactsStep.java index b50f8b2..9aeef99 100644 --- a/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectContactsStep.java +++ b/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectContactsStep.java @@ -43,7 +43,7 @@ public class SciProjectContactsStep extends SimpleEditStep { super(itemModel, parent, selectedLanguageParam, parameterSuffix); final BasicItemForm addContactSheet = new SciProjectContactAddForm( - itemModel, this); + itemModel, this, selectedLanguageParam); add(SciProjectUiConstants.ADD_CONTACT_SHEET_NAME, new GlobalizedMessage( diff --git a/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectController.java b/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectController.java index f60dfe8..d38de3e 100644 --- a/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectController.java +++ b/sci-types-project/src/main/java/com/arsdigita/cms/contenttypes/ui/SciProjectController.java @@ -6,14 +6,19 @@ package com.arsdigita.cms.contenttypes.ui; import org.libreccm.configuration.ConfigurationManager; +import org.librecms.assets.ContactableEntity; +import org.librecms.assets.ContactableEntityRepository; +import org.scientificcms.contenttypes.sciproject.Contact; import org.scientificcms.contenttypes.sciproject.SciProject; import org.scientificcms.contenttypes.sciproject.SciProjectConfig; +import org.scientificcms.contenttypes.sciproject.SciProjectMananger; import org.scientificcms.contenttypes.sciproject.SciProjectRepository; import java.time.LocalDate; import java.util.Date; import java.util.Locale; import java.util.Map; +import java.util.Optional; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; @@ -28,18 +33,103 @@ class SciProjectController { @Inject private ConfigurationManager confManager; - + + @Inject + private ContactableEntityRepository contactableRepository; + + @Inject + private SciProjectMananger projectMananger; + @Inject private SciProjectRepository projectRepository; public String getContactTypesBundleName() { - + final SciProjectConfig conf = confManager .findConfiguration(SciProjectConfig.class); - + return conf.getContactTypesBundleName(); } - + + @Transactional(Transactional.TxType.REQUIRED) + public void addContact(final long projectId, + final long contactableId, + final String contactType) { + + final SciProject project = projectRepository + .findById(projectId, SciProject.class) + .orElseThrow( + () -> new IllegalArgumentException( + String.format("No SciProject with ID %d found.", + projectId)) + ); + + final ContactableEntity contact = contactableRepository + .findById(contactableId) + .orElseThrow(() -> new IllegalArgumentException( + String.format("No ContactableEntity with ID %d found.", + contactableId))); + + projectMananger.addContact(contact, project, contactType); + } + + @Transactional(Transactional.TxType.REQUIRED) + public void updateContactType(final long projectId, + final long contactableId, + final String contactType) { + + final SciProject project = projectRepository + .findById(projectId, SciProject.class) + .orElseThrow( + () -> new IllegalArgumentException( + String.format("No SciProject with ID %d found.", + projectId)) + ); + + final ContactableEntity contactable = contactableRepository + .findById(contactableId) + .orElseThrow(() -> new IllegalArgumentException( + String.format("No ContactableEntity with ID %d found.", + contactableId))); + + final Optional contact = project + .getContacts() + .stream() + .filter(current -> filterContact(current, project, contactable)) + .findAny(); + + if (contact.isPresent()) { + + contact.get().setContactType(contactType); + + projectRepository.save(project); + } + } + + @Transactional(Transactional.TxType.REQUIRED) + public boolean hasContact(final long projectId, + final long contactableId) { + + final SciProject project = projectRepository + .findById(projectId, SciProject.class) + .orElseThrow( + () -> new IllegalArgumentException( + String.format("No SciProject with ID %d found.", + projectId)) + ); + + final ContactableEntity contactable = contactableRepository + .findById(contactableId) + .orElseThrow(() -> new IllegalArgumentException( + String.format("No ContactableEntity with ID %d found.", + contactableId))); + + return project + .getContacts() + .stream() + .anyMatch(current -> filterContact(current, project, contactable)); + } + @Transactional(Transactional.TxType.REQUIRED) public void save(final long projectId, final Locale selectedLocale, @@ -68,4 +158,12 @@ class SciProjectController { projectRepository.save(project); } + private boolean filterContact(final Contact contact, + final SciProject project, + final ContactableEntity contactable) { + + return contact.getProject().equals(project) + && contact.getContactable().equals(contactable); + } + }