From d2bdaca4552ffda9ba445ebc385b0bac3c634e4e Mon Sep 17 00:00:00 2001 From: jensp Date: Tue, 30 Jul 2019 13:07:34 +0000 Subject: [PATCH] CCM NG: Renders for some assets git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@6157 8810af33-2d31-482b-a856-94f89814c4df --- .../assets/ContactableEntityRenderer.java | 89 +++++++++++++++++++ .../assets/OrganizationRenderer.java | 54 +++++++++++ .../pagemodel/assets/PersonRenderer.java | 85 ++++++++++++++++++ .../assets/PostalAddressRenderer.java | 56 ++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 ccm-cms/src/main/java/org/librecms/pagemodel/assets/ContactableEntityRenderer.java create mode 100644 ccm-cms/src/main/java/org/librecms/pagemodel/assets/OrganizationRenderer.java create mode 100644 ccm-cms/src/main/java/org/librecms/pagemodel/assets/PersonRenderer.java create mode 100644 ccm-cms/src/main/java/org/librecms/pagemodel/assets/PostalAddressRenderer.java diff --git a/ccm-cms/src/main/java/org/librecms/pagemodel/assets/ContactableEntityRenderer.java b/ccm-cms/src/main/java/org/librecms/pagemodel/assets/ContactableEntityRenderer.java new file mode 100644 index 000000000..46ce40a54 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pagemodel/assets/ContactableEntityRenderer.java @@ -0,0 +1,89 @@ +/* + * 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.pagemodel.assets; + +import org.librecms.assets.ContactEntry; +import org.librecms.assets.ContactableEntity; +import org.librecms.assets.Organization; +import org.librecms.assets.PostalAddress; +import org.librecms.contentsection.Asset; + +import java.util.Locale; +import java.util.Map; +import java.util.stream.Collectors; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class ContactableEntityRenderer extends AbstractAssetRenderer { + + @Inject + private AssetRenderers assetRenderers; + + @Override + protected void renderAsset(final Asset asset, + final Locale language, + final Map result) { + + final ContactableEntity contactable; + if (asset instanceof ContactableEntity) { + contactable = (ContactableEntity) asset; + } else { + return; + } + + final Map contactEntries = buildContactEntries( + contactable); + result.put("contactEntries", contactEntries); + + if (contactable.getPostalAddress() != null) { + + final AbstractAssetRenderer postalAddressRenderer = assetRenderers + .findRenderer(PostalAddress.class); + final PostalAddress postalAddress = contactable.getPostalAddress(); + + result.put("postalAddress", + postalAddressRenderer.render(postalAddress, language)); + } + } + + private Map buildContactEntries( + final ContactableEntity contactable) { + + return contactable + .getContactEntries() + .stream() + .map(this::buildContactEntry) + .collect(Collectors.toMap(entry -> entry[0], entry -> entry[1])); + } + + private String[] buildContactEntry(final ContactEntry entry) { + + final String key = entry.getKey().getEntryKey(); + final String value = entry.getValue(); + + return new String[]{key, value}; + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/pagemodel/assets/OrganizationRenderer.java b/ccm-cms/src/main/java/org/librecms/pagemodel/assets/OrganizationRenderer.java new file mode 100644 index 000000000..7585c781b --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pagemodel/assets/OrganizationRenderer.java @@ -0,0 +1,54 @@ +/* + * 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.pagemodel.assets; + +import org.librecms.assets.Organization; +import org.librecms.contentsection.Asset; + +import java.util.Locale; +import java.util.Map; + +import javax.enterprise.context.RequestScoped; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@AssetRenderer(renders = Organization.class) +public class OrganizationRenderer extends ContactableEntityRenderer { + + @Override + protected void renderAsset(final Asset asset, + final Locale language, + final Map result) { + + super.renderAsset(asset, language, result); + + final Organization organization; + if (asset instanceof Organization) { + organization = (Organization) asset; + } else { + return; + } + + result.put("organizationName", organization.getName()); + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/pagemodel/assets/PersonRenderer.java b/ccm-cms/src/main/java/org/librecms/pagemodel/assets/PersonRenderer.java new file mode 100644 index 000000000..49edc2046 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pagemodel/assets/PersonRenderer.java @@ -0,0 +1,85 @@ +/* + * 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.pagemodel.assets; + +import org.librecms.assets.Person; +import org.librecms.assets.PersonName; +import org.librecms.contentsection.Asset; + +import java.util.Locale; +import java.util.Map; + +import javax.enterprise.context.RequestScoped; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@AssetRenderer(renders = Person.class) +public class PersonRenderer extends ContactableEntityRenderer { + + @Override + protected void renderAsset(final Asset asset, + final Locale language, + final Map result) { + + super.renderAsset(asset, language, result); + + final Person person; + if (asset instanceof Person) { + person = (Person) asset; + } else { + return; + } + + final PersonName personName = person.getPersonName(); + if (personName != null) { + + if (personName.getSurname() != null + && !personName.getSurname().isEmpty()) { + + result.put("surname", personName.getSurname()); + } + + if (personName.getGivenName() != null + && !personName.getGivenName().isEmpty()) { + + result.put("givenName", personName.getGivenName()); + } + + if (personName.getPrefix() != null + && !personName.getPrefix().isEmpty()) { + + result.put("prefix", personName.getPrefix()); + } + + if (personName.getSuffix() != null + && !personName.getSuffix().isEmpty()) { + + result.put("suffix", personName.getSuffix()); + } + } + + if (person.getBirthdate() != null) { + result.put("birthdate", person.getBirthdate()); + } + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/pagemodel/assets/PostalAddressRenderer.java b/ccm-cms/src/main/java/org/librecms/pagemodel/assets/PostalAddressRenderer.java new file mode 100644 index 000000000..0ee384389 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pagemodel/assets/PostalAddressRenderer.java @@ -0,0 +1,56 @@ +/* + * 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.pagemodel.assets; + +import org.librecms.assets.PostalAddress; +import org.librecms.contentsection.Asset; + +import java.util.Locale; +import java.util.Map; + +import javax.enterprise.context.RequestScoped; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@AssetRenderer(renders = PostalAddress.class) +public class PostalAddressRenderer extends AbstractAssetRenderer { + + @Override + protected void renderAsset(final Asset asset, + final Locale language, + final Map result) { + + final PostalAddress postalAddress; + if (asset instanceof PostalAddress) { + postalAddress = (PostalAddress) asset; + } else { + return; + } + + result.put("address", postalAddress.getAddress()); + result.put("city", postalAddress.getCity()); + result.put("isoCountryCode", postalAddress.getIsoCountryCode()); + result.put("postalCode", postalAddress.getPostalCode()); + result.put("state", postalAddress.getState()); + } + +}