From abc4ec7ab3373b77b0d41ef267d0350250e10213 Mon Sep 17 00:00:00 2001 From: jensp Date: Tue, 23 Jul 2019 15:42:09 +0000 Subject: [PATCH] ContactableEntity Asset, Organization Asset * Migrations for ContactEntryKey * More work on the form for managing assets of these types git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@6149 8810af33-2d31-482b-a856-94f89814c4df --- .../cms/ui/assets/AbstractAssetForm.java | 52 +++--- .../assets/AbstractAssetFormController.java | 102 ++++++++++++ .../forms/AbstractContactableEntityForm.java | 149 +++++++++++++++--- ...tractContactableEntityFormController.java} | 32 +++- .../cms/ui/assets/forms/OrganizationForm.java | 16 ++ .../org/librecms/assets/ContactEntry.java | 30 +++- .../org/librecms/assets/ContactEntryKey.java | 13 +- .../ContactEntryKeyByLabelComparator.java | 68 ++++++++ .../assets/ContactableEntityRepository.java | 57 +++++++ .../h2/V7_0_0_21__contact_entry_keys.sql | 63 ++++++++ .../h2/V7_0_0_22__contact_entry_aud_fix.sql | 1 + .../pgsql/V7_0_0_21__contact_entry_keys.sql | 63 ++++++++ .../V7_0_0_22__contact_entry_aud_fix.sql | 1 + 13 files changed, 599 insertions(+), 48 deletions(-) create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetFormController.java rename ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/{ContactableEntityFormController.java => AbstractContactableEntityFormController.java} (67%) create mode 100644 ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyByLabelComparator.java create mode 100644 ccm-cms/src/main/java/org/librecms/assets/ContactableEntityRepository.java create mode 100644 ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/h2/V7_0_0_21__contact_entry_keys.sql create mode 100644 ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/h2/V7_0_0_22__contact_entry_aud_fix.sql create mode 100644 ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/pgsql/V7_0_0_21__contact_entry_keys.sql create mode 100644 ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/pgsql/V7_0_0_22__contact_entry_aud_fix.sql diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetForm.java index 64d6c708c..dba2e5243 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetForm.java @@ -69,21 +69,29 @@ public abstract class AbstractAssetForm FormSubmissionListener { private static final String ASSET_TITLE = "asset-name"; + private static final String ASSET_NAME = "asset-title"; private final AssetPane assetPane; + private final SingleSelectionModel selectionModel; private BoxPanel showLocalePanel; + private SingleSelect showLocaleSelect; + private Submit showLocaleSubmit; private BoxPanel addLocalePanel; + private SingleSelect addLocaleSelect; + private Submit addLocaleSubmit; private TextField name; + private TextField title; + private SaveCancelSection saveCancelSection; public AbstractAssetForm(final AssetPane assetPane) { @@ -130,13 +138,13 @@ public abstract class AbstractAssetForm final SingleSelect target = (SingleSelect) event .getTarget(); - target.clearOptions();; + target.clearOptions(); final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final AssetL10NManager l10nManager = cdiUtil - .findBean(AssetL10NManager.class); - final List availableLocales = new ArrayList<>( - l10nManager.availableLocales(selectedAsset.get())); + final AbstractAssetFormController controller = cdiUtil + .findBean(AbstractAssetFormController.class); + final List availableLocales = controller + .availableLocales(selectedAsset.get()); availableLocales.sort((locale1, locale2) -> { return locale1.toString().compareTo(locale2 .toString()); @@ -206,10 +214,11 @@ public abstract class AbstractAssetForm target.clearOptions(); final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final AssetL10NManager l10nManager = cdiUtil - .findBean(AssetL10NManager.class); - final List creatableLocales = new ArrayList<>( - l10nManager.creatableLocales(selectedAsset.get())); + final AbstractAssetFormController controller = cdiUtil + .findBean(AbstractAssetFormController.class); + + final List creatableLocales = controller + .creatableLocales(selectedAsset.get()); creatableLocales.sort((locale1, locale2) -> { return locale1 .toString() @@ -298,14 +307,15 @@ public abstract class AbstractAssetForm if (selectedAsset.isPresent()) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final AbstractAssetFormController controller = cdiUtil + .findBean(AbstractAssetFormController.class); showLocaleSelect.setValue(state, getSelectedLocale(state)); title.setValue(state, - selectedAsset - .get() - .getTitle() - .getValue(getSelectedLocale(state))); + controller.getTitle(selectedAsset.get(), + getSelectedLocale(state))); } else { showLocaleSelect.setValue(state, KernelConfig @@ -318,13 +328,13 @@ public abstract class AbstractAssetForm } protected Locale getSelectedLocale(final PageState state) { - + final Object selected = showLocaleSelect.getValue(state); if (selected == null) { return KernelConfig.getConfig().getDefaultLocale(); - } else if(selected instanceof Locale) { + } else if (selected instanceof Locale) { return (Locale) selected; - } else if(selected instanceof String) { + } else if (selected instanceof String) { return new Locale((String) selected); } else { return new Locale(selected.toString()); @@ -345,11 +355,13 @@ public abstract class AbstractAssetForm .get() .getDisplayName()); + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final AbstractAssetFormController controller = cdiUtil + .findBean(AbstractAssetFormController.class); + title.setValue(state, - selectedAsset - .get() - .getTitle() - .getValue(getSelectedLocale(state))); + controller.getTitle(selectedAsset.get(), + getSelectedLocale(state))); showLocale(state); } } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetFormController.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetFormController.java new file mode 100644 index 000000000..66aa3860b --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetFormController.java @@ -0,0 +1,102 @@ +/* + * 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; + +import org.librecms.assets.AssetL10NManager; +import org.librecms.contentsection.Asset; +import org.librecms.contentsection.AssetRepository; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Objects; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.transaction.Transactional; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class AbstractAssetFormController { + + @Inject + private AssetRepository assetRepository; + + @Inject + private AssetL10NManager l10nManager; + + @Transactional(Transactional.TxType.REQUIRED) + public String getTitle(final Asset asset, final Locale locale) { + + Objects.requireNonNull(asset, "Can't get title from asset null."); + Objects.requireNonNull(locale, + "Can't title from asset for locale null"); + + final Asset result = assetRepository + .findById(asset.getObjectId()) + .orElseThrow( + () -> new IllegalArgumentException( + String.format("No asset with ID %d found.", + asset.getObjectId()) + ) + ); + + return result.getTitle().getValue(locale); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List availableLocales(final Asset asset) { + + Objects.requireNonNull(asset, + "Can't get available locales from asset null."); + + final Asset result = assetRepository + .findById(asset.getObjectId()) + .orElseThrow( + () -> new IllegalArgumentException( + String.format("No asset with ID %d found.", + asset.getObjectId()) + ) + ); + + return new ArrayList<>(l10nManager.availableLocales(result)); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List creatableLocales(final Asset asset) { + + Objects.requireNonNull(asset, + "Can't get creatable locales from asset null."); + + final Asset result = assetRepository + .findById(asset.getObjectId()) + .orElseThrow( + () -> new IllegalArgumentException( + String.format("No asset with ID %d found.", + asset.getObjectId()) + ) + ); + + return new ArrayList<>(l10nManager.creatableLocales(result)); + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractContactableEntityForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractContactableEntityForm.java index 67969007a..3c6396d2b 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractContactableEntityForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractContactableEntityForm.java @@ -23,8 +23,14 @@ 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; import com.arsdigita.bebop.event.TableActionEvent; @@ -45,14 +51,20 @@ import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.util.LockableImpl; import org.libreccm.cdi.utils.CdiUtil; -import org.libreccm.configuration.ConfigurationManager; +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.PostalAddress; import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.TooManyListenersException; +import java.util.stream.Collectors; import static org.librecms.CmsConstants.*; @@ -72,6 +84,8 @@ public abstract class AbstractContactableEntityForm private final AssetPane assetPane; + private SimpleContainer contactEntriesContainer; + private Table contactEntriesTable; private SingleSelect contactEntryKeySelect; @@ -94,27 +108,97 @@ public abstract class AbstractContactableEntityForm addPropertyWidgets(); + contactEntriesContainer = new SimpleContainer() { + + @Override + public boolean isVisible(final PageState state) { + return getSelectedAsset(state) != null; + } + + }; + add(contactEntriesContainer); + contactEntriesTable = buildContactEntriesTable(); - add(contactEntriesTable); + contactEntriesContainer.add(contactEntriesTable); contactEntryKeySelect = new SingleSelect(new StringParameter( "contactentry-key")); - add(contactEntryKeySelect); + try { + contactEntryKeySelect + .addPrintListener(new ContactEntryKeySelectPrintListener()); + } catch (TooManyListenersException ex) { + throw new RuntimeException(ex); + } + contactEntriesContainer.add(new Label( + new GlobalizedMessage( + "cms.ui.authoring.assets.contactable.contactentries.key", + CMS_BUNDLE)) + ); + contactEntriesContainer.add(contactEntryKeySelect); + + contactEntryValueField = new TextField("contact-entry-value"); + contactEntriesContainer.add(new Label( + new GlobalizedMessage( + "cms.ui.authoring.assets.contactable.contactentries.value", + CMS_BUNDLE)) + ); + contactEntriesContainer.add(contactEntryValueField); + + addContactEntryLink = new ActionLink( + new GlobalizedMessage( + "cms.ui.authoring.assets.contactable.contactentries.add", + CMS_BUNDLE) + ); + addContactEntryLink + .addActionListener(new AddContactEntryActionListener()); + contactEntriesContainer.add(addContactEntryLink); + + contactEntriesContainer.add(new Label( + new GlobalizedMessage( + "cms.ui.authoring.assets.contactable.postaladdress", + CMS_BUNDLE)) + ); + postalAddressSearchWidget = new AssetSearchWidget( + "contactable-postaladdress", PostalAddress.class + ); + contactEntriesContainer.add(postalAddressSearchWidget); + + } + + @Override + public void init(final FormSectionEvent event) throws FormProcessException { + + super.init(event); + + final PageState state = event.getPageState(); + + final Optional selected = getSelectedAsset(state); + + if (selected.isPresent()) { + // ToDo + } + } + + @Override + public void register(final Page page) { + + super.register(page); + +// page.addComponent(addContactEntryLink); } protected abstract void addPropertyWidgets(); private Table buildContactEntriesTable() { - final Table table = new Table() { - - @Override - public boolean isVisible(final PageState state) { - return getSelectedAsset(state).isPresent(); - } - - }; + final Table table = new Table(); +// { +// @Override +// public boolean isVisible(final PageState state) { +// return getSelectedAsset(state).isPresent(); +// } +// }; final TableColumnModel columnModel = table.getColumnModel(); columnModel.add(new TableColumn( COL_CONTACT_ENTRIES_KEY, @@ -168,8 +252,9 @@ public abstract class AbstractContactableEntityForm final Integer rowKey = (Integer) event.getRowKey(); final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final ContactableEntityFormController controller = cdiUtil - .findBean(ContactableEntityFormController.class); + final AbstractContactableEntityFormController controller + = cdiUtil + .findBean(AbstractContactableEntityFormController.class); final Optional selected = getSelectedAsset(event .getPageState()); if (selected.isPresent()) { @@ -199,8 +284,11 @@ public abstract class AbstractContactableEntityForm .orElseThrow( () -> new IllegalStateException("No asset selected") ); - final List contactEntries = selected - .getContactEntries(); + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final AbstractContactableEntityFormController controller = cdiUtil + .findBean(AbstractContactableEntityFormController.class); + final List contactEntries = controller + .getContactEntries(selected); return new ContactEntriesTableModel(contactEntries); } @@ -293,10 +381,35 @@ public abstract class AbstractContactableEntityForm new Label(new GlobalizedMessage("cms.ui.select_one"), CMS_BUNDLE)) ); - - final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final ContactEntryKeyRepository keyRepo = cdiUtil + .findBean(ContactEntryKeyRepository.class); + final GlobalizationHelper globalizationHelper = cdiUtil + .findBean(GlobalizationHelper.class); + final List keys = keyRepo + .findAll() + .stream() + .sorted(new ContactEntryKeyByLabelComparator(globalizationHelper + .getNegotiatedLocale())) + .collect(Collectors.toList()); + + for (final ContactEntryKey key : keys) { + + final Text label = new Text( + globalizationHelper + .getValueFromLocalizedString(key.getLabel())); + + target.addOption(new Option(key.getEntryKey(), label)); + } + } + + } + + private class AddContactEntryActionListener implements ActionListener { + + @Override + public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ContactableEntityFormController.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractContactableEntityFormController.java similarity index 67% rename from ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ContactableEntityFormController.java rename to ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractContactableEntityFormController.java index 27a639a1b..d513ae1f7 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ContactableEntityFormController.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractContactableEntityFormController.java @@ -21,6 +21,11 @@ package com.arsdigita.cms.ui.assets.forms; import org.librecms.assets.ContactEntry; import org.librecms.assets.ContactableEntity; import org.librecms.assets.ContactableEntityManager; +import org.librecms.assets.ContactableEntityRepository; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; @@ -31,11 +36,36 @@ import javax.transaction.Transactional; * @author Jens Pelzetter */ @RequestScoped -public class ContactableEntityFormController { +public class AbstractContactableEntityFormController { + @Inject + private ContactableEntityRepository contactableEntityRepository; + @Inject private ContactableEntityManager contactableEntityManager; + @Transactional(Transactional.TxType.REQUIRED) + public List getContactEntries( + final ContactableEntity contactable) { + + Objects.requireNonNull(contactable, + "Can't get contact entries from null."); + + final ContactableEntity entity = contactableEntityRepository + .findById(contactable.getObjectId()) + .orElseThrow(() -> new IllegalArgumentException(String.format( + "No ContactEntity with ID %d found.", + contactable.getObjectId()))); + + final List entries = new ArrayList<>(); + for(final ContactEntry entry : entity.getContactEntries()) { + + entries.add(entry); + } + + return entries; + } + @Transactional(Transactional.TxType.REQUIRED) public void addContactEntry(final ContactEntry contactEntry, final ContactableEntity toContactableEntity) { diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/OrganizationForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/OrganizationForm.java index 4ab4c4848..b99438ff9 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/OrganizationForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/OrganizationForm.java @@ -31,6 +31,7 @@ import org.librecms.assets.Organization; import org.librecms.contentsection.Asset; import java.util.Objects; +import java.util.Optional; /** * @@ -45,6 +46,21 @@ public class OrganizationForm extends AbstractContactableEntityForm selected = getSelectedAsset(state); + + if (selected.isPresent()) { + + organizationName.setValue(state, selected.get().getName()); + } + } + @Override protected void addPropertyWidgets() { diff --git a/ccm-cms/src/main/java/org/librecms/assets/ContactEntry.java b/ccm-cms/src/main/java/org/librecms/assets/ContactEntry.java index db75ff7c3..8f09807dc 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/ContactEntry.java +++ b/ccm-cms/src/main/java/org/librecms/assets/ContactEntry.java @@ -28,6 +28,8 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; import javax.persistence.Table; import static org.librecms.CmsConstants.*; @@ -58,8 +60,10 @@ public class ContactEntry implements Serializable { /** * Key used to identify the entry. */ - @Column(name = "ENTRY_KEY", length = 255, nullable = false) - private String key; +// @Column(name = "ENTRY_KEY", length = 255, nullable = false) + @OneToOne + @JoinColumn(name = "CONTACT_ENTRY_KEY_ID") + private ContactEntryKey key; /** * The value of the entry. @@ -89,11 +93,21 @@ public class ContactEntry implements Serializable { this.order = order; } - public String getKey() { +// public String getKey() { +// return key; +// } +// +// public void setKey(final String key) { +// this.key = key; +// } + + public ContactEntryKey getKey() { + return key; } - - public void setKey(final String key) { + + public void setKey(final ContactEntryKey key) { + this.key = key; } @@ -105,6 +119,8 @@ public class ContactEntry implements Serializable { this.value = value; } + + @Override public int hashCode() { int hash = 7; @@ -158,13 +174,13 @@ public class ContactEntry implements Serializable { return String.format("%s{ " + "contactEntryId = %d, " + "order = %d" - + "key = \"%s\", " + + "key = %s, " + "value = \"%s\"%s" + " }", super.toString(), contactEntryId, order, - key, + Objects.toString(key), value, data); } diff --git a/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKey.java b/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKey.java index 2ec3ce2ee..47cc6471e 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKey.java +++ b/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKey.java @@ -18,6 +18,7 @@ */ package org.librecms.assets; +import org.hibernate.envers.Audited; import org.libreccm.l10n.LocalizedString; import java.io.Serializable; @@ -39,8 +40,10 @@ import static org.librecms.CmsConstants.*; * @author Jens Pelzetter */ @Entity -@Table(name = "CONTACT_ENTRY_KEYS") -public class ContactEntryKey implements Serializable { +@Audited +@Table(name = "CONTACT_ENTRY_KEYS", schema = DB_SCHEMA) +public class ContactEntryKey + implements Comparable, Serializable { private static final long serialVersionUID = 1L; @@ -93,6 +96,12 @@ public class ContactEntryKey implements Serializable { this.label = Objects.requireNonNull(label); } + + @Override + public int compareTo(final ContactEntryKey other) { + + return entryKey.compareTo(other.getEntryKey()); + } @Override public int hashCode() { diff --git a/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyByLabelComparator.java b/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyByLabelComparator.java new file mode 100644 index 000000000..5d358d498 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyByLabelComparator.java @@ -0,0 +1,68 @@ +/* + * 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 com.arsdigita.kernel.KernelConfig; + +import org.libreccm.l10n.LocalizedString; + +import java.util.Comparator; +import java.util.Locale; + +/** + * + * @author Jens Pelzetter + */ +public class ContactEntryKeyByLabelComparator + implements Comparator { + + private final Locale locale; + + private final Locale defaultLocale; + + public ContactEntryKeyByLabelComparator(final Locale locale) { + + this.locale = locale; + defaultLocale = KernelConfig.getConfig().getDefaultLocale(); + } + + @Override + public int compare(final ContactEntryKey key1, final ContactEntryKey key2) { + + final LocalizedString label1 = key1.getLabel(); + final LocalizedString label2 = key2.getLabel(); + + final String localized1 = getLocalizedValue(label1); + final String localized2 = getLocalizedValue(label2); + + return localized1.compareTo(localized2); + } + + private String getLocalizedValue(final LocalizedString source) { + + if (source.hasValue(locale)) { + return source.getValue(locale); + } else if (source.hasValue(defaultLocale)) { + return source.getValue(defaultLocale); + } else { + return source.getValue(); + } + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/assets/ContactableEntityRepository.java b/ccm-cms/src/main/java/org/librecms/assets/ContactableEntityRepository.java new file mode 100644 index 000000000..9d46a6623 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/assets/ContactableEntityRepository.java @@ -0,0 +1,57 @@ +/* + * 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 javax.enterprise.context.RequestScoped; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class ContactableEntityRepository + extends AbstractEntityRepository{ + + private static final long serialVersionUID = 1L; + + @Override + public Class getEntityClass() { + return ContactableEntity.class; + } + + @Override + public String getIdAttributeName() { + return "objectId"; + } + + @Override + public Long getIdOfEntity(final ContactableEntity entity) { + + return entity.getObjectId(); + } + + @Override + public boolean isNew(final ContactableEntity entity) { + + return entity.getObjectId() == 0; + } + +} diff --git a/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/h2/V7_0_0_21__contact_entry_keys.sql b/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/h2/V7_0_0_21__contact_entry_keys.sql new file mode 100644 index 000000000..b7b12641b --- /dev/null +++ b/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/h2/V7_0_0_21__contact_entry_keys.sql @@ -0,0 +1,63 @@ +alter table CCM_CMS.CONTACT_ENTRIES add column CONTACT_ENTRY_KEY_ID bigint; + +create table CCM_CMS.CONTACT_ENTRY_KEY_LABELS ( + KEY_ID bigint not null, + LOCALIZED_VALUE varchar(2147483647), + LOCALE varchar(255) not null, + primary key (KEY_ID, LOCALE) + ); + +create table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD ( + REV integer not null, + KEY_ID bigint not null, + LOCALIZED_VALUE varchar(2147483647) not null, + LOCALE varchar(255) not null, + REVTYPE tinyint, + REVEND integer, + primary key (REV, KEY_ID, LOCALIZED_VALUE, LOCALE) +); + +create table CCM_CMS.CONTACT_ENTRY_KEYS ( + KEY_ID bigint not null, + ENTRY_KEY varchar(255), + primary key (KEY_ID) +); + +create table CCM_CMS.CONTACT_ENTRY_KEYS_AUD ( + KEY_ID bigint not null, + REV integer not null, + REVTYPE tinyint, + REVEND integer, + ENTRY_KEY varchar(255), + primary key (KEY_ID, REV) +); + +alter table CCM_CMS.CONTACT_ENTRIES + add constraint FKirtfj8sm4y5myworl5hvs1l78 + foreign key (CONTACT_ENTRY_KEY_ID) + references CCM_CMS.CONTACT_ENTRY_KEYS; + +alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS + add constraint FK243nk3buqm0pskkr5ifjqfxn5 + foreign key (KEY_ID) + references CCM_CMS.CONTACT_ENTRY_KEYS; + +alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD + add constraint FK6n995k5gao6v63gfcga3yaxcw + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + +alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD + add constraint FKdr8ujdpn1ej8l6omlxq8bsxbd + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; + +alter table CCM_CMS.CONTACT_ENTRY_KEYS_AUD + add constraint FKcvn2b1h1d4uvvmtbf4qf81l0y + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + +alter table CCM_CMS.CONTACT_ENTRY_KEYS_AUD + add constraint FKkyy4v3tax8w5htnpkmmt8aec1 + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; \ No newline at end of file diff --git a/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/h2/V7_0_0_22__contact_entry_aud_fix.sql b/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/h2/V7_0_0_22__contact_entry_aud_fix.sql new file mode 100644 index 000000000..f8f429e7b --- /dev/null +++ b/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/h2/V7_0_0_22__contact_entry_aud_fix.sql @@ -0,0 +1 @@ +alter table CCM_CMS.CONTACT_ENTRIES_AUD add column CONTACT_ENTRY_KEY_ID bigint; diff --git a/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/pgsql/V7_0_0_21__contact_entry_keys.sql b/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/pgsql/V7_0_0_21__contact_entry_keys.sql new file mode 100644 index 000000000..3dfe8d531 --- /dev/null +++ b/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/pgsql/V7_0_0_21__contact_entry_keys.sql @@ -0,0 +1,63 @@ +alter table CCM_CMS.CONTACT_ENTRIES add column CONTACT_ENTRY_KEY_ID int8; + +create table CCM_CMS.CONTACT_ENTRY_KEY_LABELS ( + KEY_ID int8 not null, + LOCALIZED_VALUE text, + LOCALE varchar(255) not null, + primary key (KEY_ID, LOCALE) + ); + +create table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD ( + REV int4 not null, + KEY_ID int8 not null, + LOCALIZED_VALUE text not null, + LOCALE varchar(255) not null, + REVTYPE int2, + REVEND int4, + primary key (REV, KEY_ID, LOCALIZED_VALUE, LOCALE) +); + +create table CCM_CMS.CONTACT_ENTRY_KEYS ( + KEY_ID int8 not null, + ENTRY_KEY varchar(255), + primary key (KEY_ID) +); + +create table CCM_CMS.CONTACT_ENTRY_KEYS_AUD ( + KEY_ID int8 not null, + REV int4 not null, + REVTYPE int2, + REVEND int4, + ENTRY_KEY varchar(255), + primary key (KEY_ID, REV) +); + +alter table CCM_CMS.CONTACT_ENTRIES + add constraint FKirtfj8sm4y5myworl5hvs1l78 + foreign key (CONTACT_ENTRY_KEY_ID) + references CCM_CMS.CONTACT_ENTRY_KEYS; + +alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS + add constraint FK243nk3buqm0pskkr5ifjqfxn5 + foreign key (KEY_ID) + references CCM_CMS.CONTACT_ENTRY_KEYS; + +alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD + add constraint FK6n995k5gao6v63gfcga3yaxcw + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + +alter table CCM_CMS.CONTACT_ENTRY_KEY_LABELS_AUD + add constraint FKdr8ujdpn1ej8l6omlxq8bsxbd + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; + +alter table CCM_CMS.CONTACT_ENTRY_KEYS_AUD + add constraint FKcvn2b1h1d4uvvmtbf4qf81l0y + foreign key (REV) + references CCM_CORE.CCM_REVISIONS; + +alter table CCM_CMS.CONTACT_ENTRY_KEYS_AUD + add constraint FKkyy4v3tax8w5htnpkmmt8aec1 + foreign key (REVEND) + references CCM_CORE.CCM_REVISIONS; \ No newline at end of file diff --git a/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/pgsql/V7_0_0_22__contact_entry_aud_fix.sql b/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/pgsql/V7_0_0_22__contact_entry_aud_fix.sql new file mode 100644 index 000000000..880d866c9 --- /dev/null +++ b/ccm-cms/src/main/resources/db/migrations/org/librecms/ccm_cms/pgsql/V7_0_0_22__contact_entry_aud_fix.sql @@ -0,0 +1 @@ +alter table CCM_CMS.CONTACT_ENTRIES_AUD add column CONTACT_ENTRY_KEY_ID int8;