CCM NG: Form for Person asset

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@6154 8810af33-2d31-482b-a856-94f89814c4df
jensp 2019-07-26 07:38:33 +00:00
parent 0ed66483ff
commit 736f5efe4d
2 changed files with 96 additions and 13 deletions

View File

@ -36,8 +36,13 @@ import com.arsdigita.util.LockableImpl;
import org.librecms.assets.Person;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
@ -50,8 +55,11 @@ import static org.librecms.CmsConstants.*;
public class PersonForm extends AbstractContactableEntityForm<Person> {
private TextField surnameField;
private TextField givenNameField;
private TextField prefixField;
private TextField suffixField;
private Submit addPersonNameButton;
@ -77,7 +85,7 @@ public class PersonForm extends AbstractContactableEntityForm<Person> {
CMS_BUNDLE));
givenNameField = new TextField("givenName");
add(givenNameLabel);
add(surnameLabel);
add(givenNameField);
final Label prefixLabel = new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.person.prefix",
@ -107,22 +115,38 @@ public class PersonForm extends AbstractContactableEntityForm<Person> {
CMS_BUNDLE));
add(birthdateLabel);
birthdateField = new Date("birthdate");
add(birthdateField);
}
@Override
protected Class<Person> getAssetClass() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return Person.class;
}
@Override
protected void showLocale(PageState state) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
protected void showLocale(final PageState state) {
// Nothing
}
@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.
protected Map<String, Object> collectData(
final FormSectionEvent event) throws FormProcessException {
final PageState state = event.getPageState();
final Map<String, Object> data = new HashMap<>();
data.put(PersonFormController.SURNAME, surnameField.getValue(state));
data.put(PersonFormController.GIVENNAME,
givenNameField.getValue(state));
data.put(PersonFormController.PREFIX, prefixField.getValue(state));
data.put(PersonFormController.SUFFIX, suffixField.getValue(state));
data.put(PersonFormController.BIRTHDATE,
birthdateField.getValue(state));
return data;
}
@Override
@ -130,15 +154,50 @@ public class PersonForm extends AbstractContactableEntityForm<Person> {
super.init(event);
// ToDo
throw new UnsupportedOperationException();
final PageState state = event.getPageState();
final Map<String, Object> data = getController()
.getAssetData(getSelectedAssetId(state),
getAssetClass(),
getSelectedLocale(state));
if (data.containsKey(PersonFormController.SURNAME)) {
surnameField.setValue(state,
data.get(PersonFormController.SURNAME));
}
if (data.containsKey(PersonFormController.GIVENNAME)) {
givenNameField.setValue(state,
data.get(PersonFormController.GIVENNAME));
}
if (data.containsKey(PersonFormController.PREFIX)) {
prefixField.setValue(state, data.get(PersonFormController.PREFIX));
}
if (data.containsKey(PersonFormController.SUFFIX)) {
suffixField.setValue(state, data.get(PersonFormController.SUFFIX));
}
if (data.containsKey(PersonFormController.BIRTHDATE)) {
birthdateField.setValue(state,
data.get(PersonFormController.BIRTHDATE));
}
}
@Override
public void process(final FormSectionEvent event) throws
FormProcessException {
// ToDo
throw new UnsupportedOperationException();
if (addPersonNameButton.equals(event.getSource())) {
final PersonFormController controller
= (PersonFormController) getController();
controller.addPersonName(getSelectedAssetId(event.getPageState()));
} else {
super.process(event);
}
}
private Table buildPersonNamesTable() {
@ -218,6 +277,7 @@ public class PersonForm extends AbstractContactableEntityForm<Person> {
private final Iterator<String[]> personNames;
private String[] currentPersonName;
private int row;
public PersonNamesTableModel(final List<String[]> personNames) {

View File

@ -25,7 +25,11 @@ import org.librecms.assets.PersonManager;
import org.librecms.assets.PersonName;
import org.librecms.assets.PersonRepository;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -44,15 +48,23 @@ 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
@ -82,7 +94,12 @@ public class PersonFormController
.collect(Collectors.toList());
data.put(PERSON_NAMES, names);
data.put(BIRTHDATE, asset.getBirthdate());
final LocalDate birthdate = asset.getBirthdate();
if (birthdate != null) {
final Instant instant = Instant.from(birthdate);
final Date birthdateValue = Date.from(instant);
data.put(BIRTHDATE, birthdateValue);
}
return data;
}
@ -123,7 +140,13 @@ public class PersonFormController
if (data.containsKey(BIRTHDATE)) {
asset.setBirthdate((LocalDate) data.get(BIRTHDATE));
final Date birthdateValue = (Date) data.get(BIRTHDATE);
final Instant instant = birthdateValue.toInstant();
final LocalDate birthdate = LocalDateTime
.ofInstant(instant, ZoneId.systemDefault())
.toLocalDate();
asset.setBirthdate(birthdate);
}
final String surname = (String) data.get(SURNAME);