From e175d00862689a10788906cd56dbe3de11c48073 Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 17 Aug 2017 12:52:57 +0000 Subject: [PATCH] CCM NG/ccm-cms: Form for adding assets to an attachment list git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4907 8810af33-2d31-482b-a856-94f89814c4df Former-commit-id: 27cb48b31640dc76c7c9ce968d38458bb48dc6db --- .../assets/AttachmentListSelectionModel.java | 4 +- .../assets/AttachmentSelectionModel.java | 105 +++++++++++++++ .../RelatedInfoAttachAssetForm.java | 125 ++++++++++++++++++ 3 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/AttachmentSelectionModel.java create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/relatedinfo/RelatedInfoAttachAssetForm.java diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/AttachmentListSelectionModel.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/AttachmentListSelectionModel.java index 8a576d784..b47f0a79d 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/AttachmentListSelectionModel.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/AttachmentListSelectionModel.java @@ -53,6 +53,7 @@ public class AttachmentListSelectionModel implements @Override public Long getSelectedKey(final PageState state) { + final Object key = model.getSelectedKey(state); if (key == null) { return null; @@ -66,8 +67,7 @@ public class AttachmentListSelectionModel implements } @Override - public void setSelectedKey(final PageState state, - final Long key) { + public void setSelectedKey(final PageState state, final Long key) { model.setSelectedKey(state, key); } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/AttachmentSelectionModel.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/AttachmentSelectionModel.java new file mode 100644 index 000000000..ca120633a --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/AttachmentSelectionModel.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2017 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.authoring.assets; + +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.ParameterSingleSelectionModel; +import com.arsdigita.bebop.SingleSelectionModel; +import com.arsdigita.bebop.event.ChangeListener; +import com.arsdigita.bebop.parameters.LongParameter; +import com.arsdigita.bebop.parameters.ParameterModel; + +import org.libreccm.cdi.utils.CdiUtil; +import org.librecms.contentsection.ItemAttachment; +import org.librecms.contentsection.ItemAttachmentManager; + +/** + * + * @author Jens Pelzetter + */ +public class AttachmentSelectionModel implements SingleSelectionModel { + + private final SingleSelectionModel model; + + public AttachmentSelectionModel(final LongParameter parameter) { + this.model = new ParameterSingleSelectionModel<>(parameter); + } + + public AttachmentSelectionModel(final String parameterName) { + this(new LongParameter(parameterName)); + } + + @Override + public boolean isSelected(final PageState state) { + return model.isSelected(state); + } + + @Override + public Long getSelectedKey(final PageState state) { + + final Object key = model.getSelectedKey(state); + if (key == null) { + return null; + } else if (key instanceof Long) { + return (Long) key; + } else if (key instanceof String) { + return Long.parseLong((String) key); + } else { + return Long.parseLong(key.toString()); + } + } + + @Override + public void setSelectedKey(final PageState state, final Long key) { + model.setSelectedKey(state, key); + } + + public ItemAttachment getSelectedAttachment(final PageState state) { + final Long key = getSelectedKey(state); + if (key == null) { + return null; + } else { + final ItemAttachmentManager manager = CdiUtil + .createCdiUtil() + .findBean(ItemAttachmentManager.class); + return manager.findById(key).get(); + } + } + + @Override + public void clearSelection(final PageState state) { + model.clearSelection(state); + } + + @Override + public void addChangeListener(final ChangeListener changeListener) { + model.addChangeListener(changeListener); + } + + @Override + public void removeChangeListener(final ChangeListener changeListener) { + model.removeChangeListener(changeListener); + } + + @Override + public ParameterModel getStateParameter() { + return model.getStateParameter(); + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/relatedinfo/RelatedInfoAttachAssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/relatedinfo/RelatedInfoAttachAssetForm.java new file mode 100644 index 000000000..199b614c3 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/relatedinfo/RelatedInfoAttachAssetForm.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2017 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.authoring.assets.relatedinfo; + +import com.arsdigita.bebop.Form; +import com.arsdigita.bebop.FormProcessException; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.SaveCancelSection; +import com.arsdigita.bebop.event.FormInitListener; +import com.arsdigita.bebop.event.FormProcessListener; +import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.bebop.event.FormSubmissionListener; +import com.arsdigita.bebop.parameters.StringParameter; +import com.arsdigita.cms.ItemSelectionModel; +import com.arsdigita.cms.ui.assets.AssetSearchWidget; +import com.arsdigita.cms.ui.authoring.assets.AttachmentListSelectionModel; + +import org.libreccm.cdi.utils.CdiUtil; +import org.libreccm.core.UnexpectedErrorException; +import org.librecms.contentsection.Asset; +import org.librecms.contentsection.AssetRepository; +import org.librecms.contentsection.AttachmentList; +import org.librecms.contentsection.ContentItem; +import org.librecms.contentsection.ItemAttachmentManager; + +/** + * + * @author Jens Pelzetter + */ +class RelatedInfoAttachAssetForm + extends Form + implements FormInitListener, + FormProcessListener, + FormSubmissionListener { + + private final RelatedInfoStep relatedInfoStep; + private final ItemSelectionModel itemSelectionModel; + private final AttachmentListSelectionModel listSelectionModel; + private final StringParameter selectedLanguageParameter; + + private final AssetSearchWidget searchWidget; + private final SaveCancelSection saveCancelSection; + + public RelatedInfoAttachAssetForm( + final RelatedInfoStep relatedInfoStep, + final ItemSelectionModel itemSelectionModel, + final AttachmentListSelectionModel listSelectionModel, + final StringParameter selectedLangugeParam) { + + super("relatedinfo-attachasset-form"); + + this.relatedInfoStep = relatedInfoStep; + this.itemSelectionModel = itemSelectionModel; + this.listSelectionModel = listSelectionModel; + this.selectedLanguageParameter = selectedLangugeParam; + + searchWidget = new AssetSearchWidget("asset-search-widget"); + super.add(searchWidget); + saveCancelSection = new SaveCancelSection(); + super.add(saveCancelSection); + + super.addInitListener(this); + super.addProcessListener(this); + super.addSubmissionListener(this); + } + + @Override + public void init(final FormSectionEvent event) throws FormProcessException { + // Nothing yet + } + + @Override + public void process(final FormSectionEvent event) throws + FormProcessException { + + final PageState state = event.getPageState(); + + final Object value = searchWidget.getValue(state); + if (value != null) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final ItemAttachmentManager attachmentManager = cdiUtil + .findBean(ItemAttachmentManager.class); + final AssetRepository assetRepo = cdiUtil + .findBean(AssetRepository.class); + final Asset asset = assetRepo + .findById((long) value) + .orElseThrow(() -> new UnexpectedErrorException(String + .format("No Asset with ID %d in the database.", value))); + + final AttachmentList list = listSelectionModel + .getSelectedAttachmentList(state); + + attachmentManager.attachAsset(asset, list); + } + + relatedInfoStep.showAttachmentList(state); + } + + @Override + public void submitted(final FormSectionEvent event) throws + FormProcessException { + + final PageState state = event.getPageState(); + + listSelectionModel.clearSelection(state); + relatedInfoStep.showAttachmentList(state); + } + +}