CCM NG: Form for Person asset

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@6153 8810af33-2d31-482b-a856-94f89814c4df
jensp 2019-07-25 19:20:56 +00:00
parent 4de5725f7c
commit 0ed66483ff
6 changed files with 566 additions and 20 deletions

View File

@ -18,19 +18,16 @@
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.bebop.ActionLink;
import com.arsdigita.bebop.BoxPanel;
import com.arsdigita.bebop.Component;
import com.arsdigita.bebop.ControlLink;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.Page;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.SimpleContainer;
import com.arsdigita.bebop.Table;
import com.arsdigita.bebop.Text;
import com.arsdigita.bebop.event.ActionEvent;
import com.arsdigita.bebop.event.ActionListener;
import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.bebop.event.PrintEvent;
import com.arsdigita.bebop.event.PrintListener;
@ -55,20 +52,13 @@ import com.arsdigita.util.LockableImpl;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.CmsConstants;
import org.librecms.assets.ContactEntry;
import org.librecms.assets.ContactEntryKeyByLabelComparator;
import org.librecms.assets.ContactEntryKey;
import org.librecms.assets.ContactEntryKeyRepository;
import org.librecms.assets.ContactableEntity;
import org.librecms.assets.ContactableEntityManager;
import org.librecms.assets.PostalAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.TooManyListenersException;
import java.util.stream.Collectors;
import static org.librecms.CmsConstants.*;
@ -178,6 +168,7 @@ public abstract class AbstractContactableEntityForm<T extends ContactableEntity>
if (selectedAssetId != null) {
// ToDo
throw new UnsupportedOperationException();
}
}

View File

@ -0,0 +1,264 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.Table;
import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.bebop.form.Date;
import com.arsdigita.bebop.form.Submit;
import com.arsdigita.bebop.form.TextField;
import com.arsdigita.bebop.table.TableColumn;
import com.arsdigita.bebop.table.TableColumnModel;
import com.arsdigita.bebop.table.TableModel;
import com.arsdigita.bebop.table.TableModelBuilder;
import com.arsdigita.cms.ui.assets.AssetPane;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.util.LockableImpl;
import org.librecms.assets.Person;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static org.librecms.CmsConstants.*;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class PersonForm extends AbstractContactableEntityForm<Person> {
private TextField surnameField;
private TextField givenNameField;
private TextField prefixField;
private TextField suffixField;
private Submit addPersonNameButton;
private Date birthdateField;
public PersonForm(final AssetPane assetPane) {
super(assetPane);
}
@Override
protected void addPropertyWidgets() {
final Label surnameLabel = new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.person.surname",
CMS_BUNDLE));
surnameField = new TextField("surname");
add(surnameLabel);
add(surnameField);
final Label givenNameLabel = new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.person.given_name",
CMS_BUNDLE));
givenNameField = new TextField("givenName");
add(givenNameLabel);
add(surnameLabel);
final Label prefixLabel = new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.person.prefix",
CMS_BUNDLE
));
prefixField = new TextField("prefix");
add(prefixLabel);
add(prefixField);
final Label suffixLabel = new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.person.suffix",
CMS_BUNDLE
));
suffixField = new TextField("suffix");
add(suffixLabel);
add(suffixField);
add(buildPersonNamesTable());
addPersonNameButton = new Submit(new GlobalizedMessage(
"cms.ui.authoring.assets.person.add_name",
CMS_BUNDLE));
add(addPersonNameButton);
final Label birthdateLabel = new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.person.birthdate",
CMS_BUNDLE));
add(birthdateLabel);
birthdateField = new Date("birthdate");
}
@Override
protected Class<Person> getAssetClass() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
protected void showLocale(PageState state) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
protected Map<String, Object> collectData(FormSectionEvent event) throws
FormProcessException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void init(final FormSectionEvent event) throws FormProcessException {
super.init(event);
// ToDo
throw new UnsupportedOperationException();
}
@Override
public void process(final FormSectionEvent event) throws
FormProcessException {
// ToDo
throw new UnsupportedOperationException();
}
private Table buildPersonNamesTable() {
final Table table = new Table();
final TableColumnModel columnModel = table.getColumnModel();
columnModel.add(new TableColumn(
0,
new Label(
new GlobalizedMessage(
"cms.ui.authoring.assets.person.surname",
CMS_BUNDLE
)
)
));
columnModel.add(new TableColumn(
1,
new Label(
new GlobalizedMessage(
"cms.ui.authoring.assets.person.givenName",
CMS_BUNDLE
)
)
));
columnModel.add(new TableColumn(
2,
new Label(
new GlobalizedMessage(
"cms.ui.authoring.assets.person.prefix",
CMS_BUNDLE
)
)
));
columnModel.add(new TableColumn(
3,
new Label(
new GlobalizedMessage(
"cms.ui.authoring.assets.person.suffix",
CMS_BUNDLE
)
)
));
table.setModelBuilder(new PersonNamesTableModelBuilder());
table.setEmptyView(new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.person.names.none")));
return table;
}
private class PersonNamesTableModelBuilder
extends LockableImpl
implements TableModelBuilder {
@Override
public TableModel makeModel(final Table table, final PageState state) {
final Long selectedPersonId = getSelectedAssetId(state);
if (selectedPersonId == null) {
throw new RuntimeException("No asset selected.");
}
final PersonFormController controller
= (PersonFormController) getController();
final List<String[]> personNames = controller
.getPersonNames(selectedPersonId);
return new PersonNamesTableModel(personNames);
}
}
private class PersonNamesTableModel implements TableModel {
private final Iterator<String[]> personNames;
private String[] currentPersonName;
private int row;
public PersonNamesTableModel(final List<String[]> personNames) {
this.personNames = Objects
.requireNonNull(personNames,
"Can't create PersonNamesTableModel without a "
+ "list of person names.")
.iterator();
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public boolean nextRow() {
if (personNames.hasNext()) {
currentPersonName = personNames.next();
row++;
return true;
} else {
return false;
}
}
@Override
public Object getElementAt(final int columnIndex) {
return currentPersonName[columnIndex];
}
@Override
public Object getKeyAt(final int columnIndex) {
return row;
}
}
}

View File

@ -0,0 +1,155 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.cms.ui.assets.IsControllerForAssetType;
import org.librecms.assets.Person;
import org.librecms.assets.PersonManager;
import org.librecms.assets.PersonName;
import org.librecms.assets.PersonRepository;
import java.time.LocalDate;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@IsControllerForAssetType(Person.class)
public class PersonFormController
extends AbstractContactableEntityFormController<Person> {
protected static final String SUFFIX = "suffix";
protected static final String PREFIX = "prefix";
protected static final String GIVENNAME = "givenName";
protected static final String SURNAME = "surname";
protected static final String BIRTHDATE = "birthdate";
protected static final String PERSON_NAMES = "personNames";
protected static final int SURNAME_INDEX = 0;
protected static final int GIVENNAME_INDEX = 1;
protected static final int PREFIX_INDEX = 2;
protected static final int SUFFIX_INDEX = 3;
@Inject
private PersonRepository personRepository;
@Inject
private PersonManager personManager;
@Override
protected Map<String, Object> getAssetData(final Person asset,
final Locale selectedLocale) {
final Map<String, Object> data = super.getAssetData(asset,
selectedLocale);
final PersonName personName = asset.getPersonName();
data.put(SURNAME, personName.getSurname());
data.put(GIVENNAME, personName.getGivenName());
data.put(PREFIX, personName.getPrefix());
data.put(SUFFIX, personName.getSuffix());
final List<String[]> names = asset
.getPersonNames()
.subList(0, asset.getPersonNames().size() - 1)
.stream()
.map(this::convertPersonName)
.collect(Collectors.toList());
data.put(PERSON_NAMES, names);
data.put(BIRTHDATE, asset.getBirthdate());
return data;
}
protected List<String[]> getPersonNames(final Long personId) {
final Person person = personRepository
.findById(personId)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No Person with ID %d found.", personId)));
return person
.getPersonNames()
.subList(0, person.getPersonNames().size() - 1)
.stream()
.map(this::convertPersonName)
.collect(Collectors.toList());
}
private String[] convertPersonName(final PersonName name) {
final String[] result = new String[4];
result[SURNAME_INDEX] = name.getSurname();
result[GIVENNAME_INDEX] = name.getGivenName();
result[PREFIX_INDEX] = name.getPrefix();
result[SUFFIX_INDEX] = name.getSuffix();
return result;
}
@Override
public void updateAssetProperties(final Person asset,
final Locale selectedLocale,
final Map<String, Object> data) {
super.updateAssetProperties(asset, selectedLocale, data);
if (data.containsKey(BIRTHDATE)) {
asset.setBirthdate((LocalDate) data.get(BIRTHDATE));
}
final String surname = (String) data.get(SURNAME);
final String givenName = (String) data.get(GIVENNAME);
final String prefix = (String) data.get(PREFIX);
final String suffix = (String) data.get(SUFFIX);
asset.getPersonName().setSurname(surname);
asset.getPersonName().setGivenName(givenName);
asset.getPersonName().setPrefix(prefix);
asset.getPersonName().setSuffix(suffix);
}
public void addPersonName(final long personId) {
final Person person = personRepository
.findById(personId)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No Person with ID %d found.", personId)));
personManager.addPersonName(person);
}
@Override
public Person createAsset() {
return new Person();
}
}

View File

@ -18,6 +18,10 @@
*/
package org.librecms.assets;
import com.arsdigita.cms.ui.assets.forms.PersonForm;
import static org.libreccm.core.CoreConstants.*;
import org.hibernate.envers.Audited;
import java.time.LocalDate;
@ -26,15 +30,16 @@ import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.persistence.AssociationOverride;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import static org.librecms.CmsConstants.*;
import static org.librecms.assets.AssetConstants.*;
/**
* An asset representing a person.
@ -44,11 +49,11 @@ import static org.librecms.CmsConstants.*;
@Entity
@Table(name = "PERSONS", schema = DB_SCHEMA)
@Audited
//@AssetType(assetForm = PersonForm.class,
// labelBundle = ASSETS_BUNDLE,
// labelKey = "person.label",
// descriptionBundle = ASSETS_BUNDLE,
// descriptionKey = "person.description")
@AssetType(assetForm = PersonForm.class,
labelBundle = ASSETS_BUNDLE,
labelKey = "person.label",
descriptionBundle = ASSETS_BUNDLE,
descriptionKey = "person.description")
public class Person extends ContactableEntity {
private static final long serialVersionUID = 1L;
@ -234,8 +239,8 @@ public class Person extends ContactableEntity {
public String toString(final String data) {
return super.toString(String.format(
"surname = \"%s\", "
+ "personNames = \"%s\", "
// "surname = \"%s\", +"
"personNames = \"%s\", "
// + "givenName = \"%s\", "
// + "prefix = \"%s\", "
// + "suffix = \"%s\", "

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.librecms.assets;
import java.util.Objects;
import javax.ejb.TransactionAttribute;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class PersonManager {
@Inject
private PersonRepository personRepository;
@Transactional(Transactional.TxType.REQUIRED)
public void addPersonName(final Person toPerson) {
final PersonName current = Objects
.requireNonNull(toPerson, "Can't add a name to Person null.")
.getPersonName();
toPerson.addPersonName(current);
personRepository.save(toPerson);
}
@Transactional(Transactional.TxType.REQUIRED)
public void removePersonName(final Person person,
final PersonName personName) {
person.removePersonName(personName);
personRepository.save(person);
}
}

View File

@ -0,0 +1,72 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.librecms.assets;
import org.libreccm.core.AbstractEntityRepository;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class PersonRepository extends AbstractEntityRepository<Long, Person>{
private static final long serialVersionUID = 1L;
@Override
public Class<Person> getEntityClass() {
return Person.class;
}
@Override
public String getIdAttributeName() {
return "objectId";
}
@Override
public Long getIdOfEntity(final Person entity) {
return entity.getObjectId();
}
@Override
public boolean isNew(final Person entity) {
return entity.getObjectId() == 0;
}
public List<Person> findBySurname(final String surname) {
Objects.requireNonNull(surname);
return findAll()
.stream()
.filter(
person -> surname.equals(person.getPersonName().getSurname())
)
.collect(Collectors.toList());
}
}