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
jensp 2017-08-17 12:52:57 +00:00
parent 2021628b8e
commit 27cb48b316
3 changed files with 232 additions and 2 deletions

View File

@ -53,6 +53,7 @@ public class AttachmentListSelectionModel implements
@Override @Override
public Long getSelectedKey(final PageState state) { public Long getSelectedKey(final PageState state) {
final Object key = model.getSelectedKey(state); final Object key = model.getSelectedKey(state);
if (key == null) { if (key == null) {
return null; return null;
@ -66,8 +67,7 @@ public class AttachmentListSelectionModel implements
} }
@Override @Override
public void setSelectedKey(final PageState state, public void setSelectedKey(final PageState state, final Long key) {
final Long key) {
model.setSelectedKey(state, key); model.setSelectedKey(state, key);
} }

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class AttachmentSelectionModel implements SingleSelectionModel<Long> {
private final SingleSelectionModel<Long> 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();
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
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);
}
}