SciProjectContactAddForm finished
parent
a88124f013
commit
d6e2b8b7a9
|
|
@ -5,8 +5,11 @@
|
||||||
*/
|
*/
|
||||||
package com.arsdigita.cms.contenttypes.ui;
|
package com.arsdigita.cms.contenttypes.ui;
|
||||||
|
|
||||||
|
import com.arsdigita.bebop.FormData;
|
||||||
import com.arsdigita.bebop.FormProcessException;
|
import com.arsdigita.bebop.FormProcessException;
|
||||||
import com.arsdigita.bebop.Label;
|
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.FormSectionEvent;
|
||||||
import com.arsdigita.bebop.event.FormSubmissionListener;
|
import com.arsdigita.bebop.event.FormSubmissionListener;
|
||||||
import com.arsdigita.bebop.event.PrintEvent;
|
import com.arsdigita.bebop.event.PrintEvent;
|
||||||
|
|
@ -27,6 +30,7 @@ import org.apache.logging.log4j.Logger;
|
||||||
import org.libreccm.cdi.utils.CdiUtil;
|
import org.libreccm.cdi.utils.CdiUtil;
|
||||||
import org.librecms.CmsConstants;
|
import org.librecms.CmsConstants;
|
||||||
import org.librecms.assets.ContactableEntity;
|
import org.librecms.assets.ContactableEntity;
|
||||||
|
import org.scientificcms.contenttypes.sciproject.SciProject;
|
||||||
import org.scientificcms.contenttypes.sciproject.SciProjectConstants;
|
import org.scientificcms.contenttypes.sciproject.SciProjectConstants;
|
||||||
|
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
@ -53,7 +57,7 @@ public class SciProjectContactAddForm
|
||||||
|
|
||||||
private SciProjectContactsStep editStep;
|
private SciProjectContactsStep editStep;
|
||||||
|
|
||||||
private Label selectedContactLabel;
|
private Text selectedContactLabel;
|
||||||
|
|
||||||
public SciProjectContactAddForm(final ItemSelectionModel itemModel,
|
public SciProjectContactAddForm(final ItemSelectionModel itemModel,
|
||||||
final SciProjectContactsStep editStep,
|
final SciProjectContactsStep editStep,
|
||||||
|
|
@ -74,7 +78,7 @@ public class SciProjectContactAddForm
|
||||||
SciProjectConstants.SCI_PROJECT_BUNDLE));
|
SciProjectConstants.SCI_PROJECT_BUNDLE));
|
||||||
add(searchWidget);
|
add(searchWidget);
|
||||||
|
|
||||||
selectedContactLabel = new Label();
|
selectedContactLabel = new Text();
|
||||||
add(selectedContactLabel);
|
add(selectedContactLabel);
|
||||||
|
|
||||||
final ParameterModel contactTypeParam
|
final ParameterModel contactTypeParam
|
||||||
|
|
@ -127,21 +131,110 @@ public class SciProjectContactAddForm
|
||||||
@Override
|
@Override
|
||||||
public void init(final FormSectionEvent event) throws FormProcessException {
|
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
|
@Override
|
||||||
public void process(final FormSectionEvent event) throws
|
public void process(final FormSectionEvent event) throws
|
||||||
FormProcessException {
|
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
|
@Override
|
||||||
public void submitted(final FormSectionEvent event) throws
|
public void submitted(final FormSectionEvent event) throws
|
||||||
FormProcessException {
|
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() {
|
private SciProjectController getController() {
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ public class SciProjectContactsStep extends SimpleEditStep {
|
||||||
super(itemModel, parent, selectedLanguageParam, parameterSuffix);
|
super(itemModel, parent, selectedLanguageParam, parameterSuffix);
|
||||||
|
|
||||||
final BasicItemForm addContactSheet = new SciProjectContactAddForm(
|
final BasicItemForm addContactSheet = new SciProjectContactAddForm(
|
||||||
itemModel, this);
|
itemModel, this, selectedLanguageParam);
|
||||||
|
|
||||||
add(SciProjectUiConstants.ADD_CONTACT_SHEET_NAME,
|
add(SciProjectUiConstants.ADD_CONTACT_SHEET_NAME,
|
||||||
new GlobalizedMessage(
|
new GlobalizedMessage(
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,19 @@
|
||||||
package com.arsdigita.cms.contenttypes.ui;
|
package com.arsdigita.cms.contenttypes.ui;
|
||||||
|
|
||||||
import org.libreccm.configuration.ConfigurationManager;
|
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.SciProject;
|
||||||
import org.scientificcms.contenttypes.sciproject.SciProjectConfig;
|
import org.scientificcms.contenttypes.sciproject.SciProjectConfig;
|
||||||
|
import org.scientificcms.contenttypes.sciproject.SciProjectMananger;
|
||||||
import org.scientificcms.contenttypes.sciproject.SciProjectRepository;
|
import org.scientificcms.contenttypes.sciproject.SciProjectRepository;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import javax.enterprise.context.RequestScoped;
|
import javax.enterprise.context.RequestScoped;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
@ -29,6 +34,12 @@ class SciProjectController {
|
||||||
@Inject
|
@Inject
|
||||||
private ConfigurationManager confManager;
|
private ConfigurationManager confManager;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private ContactableEntityRepository contactableRepository;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private SciProjectMananger projectMananger;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private SciProjectRepository projectRepository;
|
private SciProjectRepository projectRepository;
|
||||||
|
|
||||||
|
|
@ -40,6 +51,85 @@ class SciProjectController {
|
||||||
return conf.getContactTypesBundleName();
|
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> 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)
|
@Transactional(Transactional.TxType.REQUIRED)
|
||||||
public void save(final long projectId,
|
public void save(final long projectId,
|
||||||
final Locale selectedLocale,
|
final Locale selectedLocale,
|
||||||
|
|
@ -68,4 +158,12 @@ class SciProjectController {
|
||||||
projectRepository.save(project);
|
projectRepository.save(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean filterContact(final Contact contact,
|
||||||
|
final SciProject project,
|
||||||
|
final ContactableEntity contactable) {
|
||||||
|
|
||||||
|
return contact.getProject().equals(project)
|
||||||
|
&& contact.getContactable().equals(contactable);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue