CCM NG/ccm-cms: Next part of RelatedInfoStep

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4910 8810af33-2d31-482b-a856-94f89814c4df
jensp 2017-08-18 19:26:56 +00:00
parent 27cb48b316
commit 28712593f2
11 changed files with 807 additions and 21 deletions

View File

@ -132,8 +132,7 @@ class AssignedImagesTable extends Table {
switch (column.getModelIndex()) { switch (column.getModelIndex()) {
case COL_MOVE: case COL_MOVE:
if (moveAttachmentModel if (moveAttachmentModel.getSelectedKey(state) == null) {
.getSelectedKey(state) == null) {
moveAttachmentModel moveAttachmentModel
.setSelectedKey(state, .setSelectedKey(state,
@ -152,7 +151,7 @@ class AssignedImagesTable extends Table {
.parseLong((String) event.getRowKey()); .parseLong((String) event.getRowKey());
controller.moveAfter(selectedAttachment, destId); controller.moveAfter(selectedAttachment, destId);
moveAttachmentModel.setSelectedKey(state, null); moveAttachmentModel.clearSelection(state);
} }
break; break;
case COL_REMOVE: case COL_REMOVE:

View File

@ -147,7 +147,7 @@ public class ImageStep extends ResettableContainer {
controller.moveToFirst(toMove); controller.moveToFirst(toMove);
moveAttachmentModel.setSelectedKey(state, null); moveAttachmentModel.clearSelection(state);
}); });
assignedImagesTable = new AssignedImagesTable(itemSelectionModel, assignedImagesTable = new AssignedImagesTable(itemSelectionModel,

View File

@ -65,16 +65,16 @@ class ImageStepController {
private AttachmentListManager attachmentListManager; private AttachmentListManager attachmentListManager;
@Inject @Inject
private ConfigurationManager confManager; private ItemAttachmentManager attachmentManager;
@Inject @Inject
private ContentItemRepository itemRepo; private ConfigurationManager confManager;
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
@Inject @Inject
private ItemAttachmentManager attachmentManager; private ContentItemRepository itemRepo;
private Locale defaultLocale; private Locale defaultLocale;

View File

@ -0,0 +1,66 @@
/*
* 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;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class AttachmentListTableRow {
private long listId;
private String name;
private String title;
private String description;
public long getListId() {
return listId;
}
public void setListId(final long listId) {
this.listId = listId;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
}

View File

@ -109,7 +109,7 @@ class RelatedInfoAttachAssetForm
attachmentManager.attachAsset(asset, list); attachmentManager.attachAsset(asset, list);
} }
relatedInfoStep.showAttachmentList(state); relatedInfoStep.showAttachmentListTable(state);
} }
@Override @Override
@ -119,7 +119,7 @@ class RelatedInfoAttachAssetForm
final PageState state = event.getPageState(); final PageState state = event.getPageState();
listSelectionModel.clearSelection(state); listSelectionModel.clearSelection(state);
relatedInfoStep.showAttachmentList(state); relatedInfoStep.showAttachmentListTable(state);
} }
} }

View File

@ -370,7 +370,7 @@ class RelatedInfoListForm
if (saveCancelSection.getCancelButton().isSelected(state)) { if (saveCancelSection.getCancelButton().isSelected(state)) {
listSelectionModel.clearSelection(state); listSelectionModel.clearSelection(state);
relatedInfoStep.showAttachmentList(state); relatedInfoStep.showAttachmentListTable(state);
} }
} }

View File

@ -0,0 +1,237 @@
/*
* 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.Component;
import com.arsdigita.bebop.ControlLink;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.Table;
import com.arsdigita.bebop.event.TableActionEvent;
import com.arsdigita.bebop.event.TableActionListener;
import com.arsdigita.bebop.parameters.StringParameter;
import com.arsdigita.bebop.table.TableCellRenderer;
import com.arsdigita.bebop.table.TableColumn;
import com.arsdigita.bebop.table.TableColumnModel;
import com.arsdigita.cms.ItemSelectionModel;
import com.arsdigita.cms.ui.authoring.assets.AttachmentListSelectionModel;
import com.arsdigita.globalization.GlobalizedMessage;
import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.CmsConstants;
import org.librecms.contentsection.AttachmentList;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class RelatedInfoListTable extends Table {
protected static final int COL_NAME = 0;
protected static final int COL_TITLE = 1;
protected static final int COL_DESC = 2;
protected static final int COL_EDIT = 3;
protected static final int COL_MOVE = 4;
protected static final int COL_DELETE = 5;
private final RelatedInfoStep relatedInfoStep;
private final ItemSelectionModel itemSelectionModel;
private final AttachmentListSelectionModel selectedListModel;
private final AttachmentListSelectionModel moveListModel;
private final StringParameter selectedLanguageParam;
protected RelatedInfoListTable(
final RelatedInfoStep relatedInfoStep,
final ItemSelectionModel itemSelectionModel,
final AttachmentListSelectionModel selectedListModel,
final AttachmentListSelectionModel moveListModel,
final StringParameter selectedLanguageParam) {
super();
this.relatedInfoStep = relatedInfoStep;
this.itemSelectionModel = itemSelectionModel;
this.selectedListModel = selectedListModel;
this.moveListModel = moveListModel;
this.selectedLanguageParam = selectedLanguageParam;
final TableColumnModel columnModel = super.getColumnModel();
columnModel.add(new TableColumn(
COL_NAME,
new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.name",
CmsConstants.CMS_BUNDLE))));
columnModel.add(new TableColumn(
COL_TITLE,
new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.title",
CmsConstants.CMS_BUNDLE))));
columnModel.add(new TableColumn(
COL_DESC,
new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.description",
CmsConstants.CMS_BUNDLE))));
columnModel.add(new TableColumn(
COL_EDIT,
new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.edit",
CmsConstants.CMS_BUNDLE))));
columnModel.add(new TableColumn(
COL_MOVE,
new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.move",
CmsConstants.CMS_BUNDLE))));
columnModel.add(new TableColumn(
COL_DELETE,
new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.delete",
CmsConstants.CMS_BUNDLE))));
super
.setModelBuilder(new RelatedInfoListTableModelBuilder(
itemSelectionModel,
moveListModel,
selectedLanguageParam));
super
.getColumn(COL_EDIT)
.setCellRenderer(new EditCellRenderer());
super
.getColumn(COL_MOVE)
.setCellRenderer(new MoveCellRenderer());
super
.getColumn(COL_DELETE)
.setCellRenderer(new DeleteCellRenderer());
super
.addTableActionListener(new TableActionListener() {
@Override
public void cellSelected(final TableActionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
final TableColumn column = getColumnModel()
.get(event.getColumn());
switch (column.getModelIndex()) {
case COL_EDIT:
selectedListModel
.setSelectedKey(state,
Long.parseLong((String) event
.getRowKey()));
relatedInfoStep.showListEditForm(state);
break;
case COL_MOVE:
if (moveListModel.getSelectedKey(state) == null) {
moveListModel
.setSelectedKey(state,
Long.parseLong(
(String) event
.getRowKey()));
} else {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final RelatedInfoStepController controller
= cdiUtil
.findBean(
RelatedInfoStepController.class);
final AttachmentList selectedList
= moveListModel
.getSelectedAttachmentList(state);
final Long destId = Long
.parseLong((String) event.getRowKey());
controller.moveAfter(itemSelectionModel
.getSelectedItem(state),
selectedList,
destId);
moveListModel.clearSelection(state);
}
break;
case COL_DELETE:
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final RelatedInfoStepController controller = cdiUtil
.findBean(RelatedInfoStepController.class);
controller.deleteList(Long
.parseLong((String) event.getRowKey()));
}
}
@Override
public void headSelected(final TableActionEvent event) {
//Nothing
}
});
}
private class MoveCellRenderer implements TableCellRenderer {
@Override
public Component getComponent(final Table table,
final PageState state,
final Object value,
final boolean isSelected,
final Object key,
final int row,
final int column) {
return new ControlLink((Component) value);
}
}
private class EditCellRenderer implements TableCellRenderer {
@Override
public Component getComponent(final Table table,
final PageState state,
final Object value,
final boolean isSelected,
final Object key,
final int row,
final int column) {
return new ControlLink((Component) value);
}
}
private class DeleteCellRenderer implements TableCellRenderer {
@Override
public Component getComponent(final Table table,
final PageState state,
final Object value,
final boolean isSelected,
final Object key,
final int row,
final int column) {
return new ControlLink((Component) value);
}
}
}

View File

@ -0,0 +1,106 @@
/*
* 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.Label;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.table.TableModel;
import com.arsdigita.cms.ui.authoring.assets.AttachmentListSelectionModel;
import com.arsdigita.globalization.GlobalizedMessage;
import org.librecms.CmsConstants;
import java.util.Iterator;
import java.util.List;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class RelatedInfoListTableModel implements TableModel {
private final PageState state;
private final AttachmentListSelectionModel moveListModel;
private final Iterator<AttachmentListTableRow> iterator;
private AttachmentListTableRow currentRow;
RelatedInfoListTableModel(
final List<AttachmentListTableRow> rows,
final PageState state,
final AttachmentListSelectionModel moveListModel) {
this.iterator = rows.iterator();
this.state = state;
this.moveListModel = moveListModel;
}
@Override
public int getColumnCount() {
return 6;
}
@Override
public boolean nextRow() {
if (iterator.hasNext()) {
currentRow = iterator.next();
return true;
} else {
return false;
}
}
@Override
public Object getElementAt(final int columnIndex) {
switch (columnIndex) {
case RelatedInfoListTable.COL_NAME:
return currentRow.getName();
case RelatedInfoListTable.COL_TITLE:
return currentRow.getTitle();
case RelatedInfoListTable.COL_DESC:
return currentRow.getDescription();
case RelatedInfoListTable.COL_EDIT:
return new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.edit"));
case RelatedInfoListTable.COL_MOVE:
if (moveListModel.getSelectedAttachmentList(state) == null) {
return new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.move",
CmsConstants.CMS_BUNDLE));
} else {
return new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.move_here",
CmsConstants.CMS_BUNDLE));
}
case RelatedInfoListTable.COL_DELETE:
return new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.list.delete"));
default:
throw new IllegalArgumentException(String.format(
"Illegal column index %d.", columnIndex));
}
}
@Override
public Object getKeyAt(final int columnIndex) {
return currentRow.getListId();
}
}

View File

@ -0,0 +1,80 @@
/*
* 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.PageState;
import com.arsdigita.bebop.Table;
import com.arsdigita.bebop.parameters.StringParameter;
import com.arsdigita.bebop.table.TableModel;
import com.arsdigita.bebop.table.TableModelBuilder;
import com.arsdigita.cms.ItemSelectionModel;
import com.arsdigita.cms.ui.authoring.assets.AttachmentListSelectionModel;
import com.arsdigita.util.LockableImpl;
import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.contentsection.ContentItem;
import java.util.List;
import java.util.Locale;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class RelatedInfoListTableModelBuilder
extends LockableImpl
implements TableModelBuilder {
private final ItemSelectionModel itemSelectionModel;
private final AttachmentListSelectionModel moveListModel;
private final StringParameter selectedLanguageParam;
protected RelatedInfoListTableModelBuilder(
final ItemSelectionModel itemSelectionModel,
final AttachmentListSelectionModel moveListModel,
final StringParameter selectedLanguageParam) {
super();
this.itemSelectionModel = itemSelectionModel;
this.moveListModel = moveListModel;
this.selectedLanguageParam = selectedLanguageParam;
}
@Override
public TableModel makeModel(final Table table,
final PageState state) {
final ContentItem selectedItem = itemSelectionModel
.getSelectedItem(state);
final String selectedLanguage = (String) state
.getValue(selectedLanguageParam);
final Locale selectedLocale = new Locale(selectedLanguage);
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final RelatedInfoStepController controller = cdiUtil
.findBean(RelatedInfoStepController.class);
final List<AttachmentListTableRow> rows = controller
.retrieveAttachmentLists(selectedItem, selectedLocale);
return new RelatedInfoListTableModel(rows, state, moveListModel);
}
}

View File

@ -18,14 +18,20 @@
*/ */
package com.arsdigita.cms.ui.authoring.assets.relatedinfo; package com.arsdigita.cms.ui.authoring.assets.relatedinfo;
import com.arsdigita.bebop.ActionLink;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.Page;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.Text;
import com.arsdigita.bebop.parameters.StringParameter; import com.arsdigita.bebop.parameters.StringParameter;
import com.arsdigita.cms.ItemSelectionModel; import com.arsdigita.cms.ItemSelectionModel;
import com.arsdigita.cms.ui.authoring.AuthoringKitWizard; import com.arsdigita.cms.ui.authoring.AuthoringKitWizard;
import com.arsdigita.cms.ui.authoring.ResettableContainer; import com.arsdigita.cms.ui.authoring.ResettableContainer;
import com.arsdigita.cms.ui.authoring.assets.AttachmentListSelectionModel;
import com.arsdigita.globalization.GlobalizedMessage;
import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.CmsConstants; import org.librecms.CmsConstants;
import org.librecms.contentsection.AttachmentList;
import org.librecms.ui.authoring.ContentItemAuthoringStep; import org.librecms.ui.authoring.ContentItemAuthoringStep;
/** /**
@ -39,17 +45,110 @@ import org.librecms.ui.authoring.ContentItemAuthoringStep;
descriptionKey = "related_info_step.description") descriptionKey = "related_info_step.description")
public class RelatedInfoStep extends ResettableContainer { public class RelatedInfoStep extends ResettableContainer {
private final ItemSelectionModel itemSelectionModel;
private final AttachmentListSelectionModel selectedListModel;
private final AttachmentListSelectionModel moveListModel;
private final AuthoringKitWizard authoringKitWizard;
private final StringParameter selectedLanguageParam;
private final RelatedInfoListTable listTable;
private final RelatedInfoListForm listForm;
private final ActionLink addListLink;
private final ActionLink listToFirstLink;
public RelatedInfoStep(final ItemSelectionModel itemSelectionModel, public RelatedInfoStep(final ItemSelectionModel itemSelectionModel,
final AuthoringKitWizard authoringKitWizard, final AuthoringKitWizard authoringKitWizard,
final StringParameter selectedLanguage) { final StringParameter selectedLanguage) {
super(); super();
super.add(new Text("Related Info Step placeholder")); this.itemSelectionModel = itemSelectionModel;
this.authoringKitWizard = authoringKitWizard;
this.selectedLanguageParam = selectedLanguage;
selectedListModel = new AttachmentListSelectionModel(
"selected-attachment-list");
moveListModel = new AttachmentListSelectionModel("move-attachment-list");
listTable = new RelatedInfoListTable(this,
itemSelectionModel,
selectedListModel, moveListModel,
selectedLanguageParam);
listForm = new RelatedInfoListForm(this,
itemSelectionModel,
selectedListModel,
selectedLanguageParam);
super.add(listTable);
super.add(listForm);
addListLink = new ActionLink(new Label(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.add_list",
CmsConstants.CMS_BUNDLE)));
addListLink.addActionListener(event -> {
showListEditForm(event.getPageState());
});
listToFirstLink = new ActionLink(new GlobalizedMessage(
"cms.ui.authoring.assets.related_info_step.move_to_beginning",
CmsConstants.CMS_BUNDLE));
listToFirstLink.addActionListener(event -> {
final PageState state = event.getPageState();
final AttachmentList toMove = moveListModel
.getSelectedAttachmentList(state);
final RelatedInfoStepController controller = CdiUtil
.createCdiUtil()
.findBean(RelatedInfoStepController.class);
controller.moveToFirst(itemSelectionModel.getSelectedItem(state),
toMove);
moveListModel.clearSelection(state);
});
moveListModel.addChangeListener(event -> {
final PageState state = event.getPageState();
if (moveListModel.getSelectedKey(state) == null) {
addListLink.setVisible(state, true);
listToFirstLink.setVisible(state, false);
} else {
addListLink.setVisible(state, false);
listToFirstLink.setVisible(state, true);
}
});
} }
protected void showAttachmentList(final PageState state) { @Override
public void register(final Page page) {
super.register(page);
page.addComponentStateParam(this, selectedListModel.getStateParameter());
page.addComponentStateParam(this, moveListModel.getStateParameter());
page.setVisibleDefault(listTable, true);
page.setVisibleDefault(listForm, false);
page.setVisibleDefault(addListLink, true);
page.setVisibleDefault(listToFirstLink, false);
}
protected void showAttachmentListTable(final PageState state) {
listTable.setVisible(state, true);
addListLink.setVisible(state, true);
listForm.setVisible(state, false);
listToFirstLink.setVisible(state, false);
}
void showListEditForm(final PageState state) {
listTable.setVisible(state, false);
listForm.setVisible(state, true);
addListLink.setVisible(state, false);
listToFirstLink.setVisible(state, false);
} }
} }

View File

@ -18,12 +18,24 @@
*/ */
package com.arsdigita.cms.ui.authoring.assets.relatedinfo; package com.arsdigita.cms.ui.authoring.assets.relatedinfo;
import com.arsdigita.kernel.KernelConfig;
import org.libreccm.configuration.ConfigurationManager;
import org.librecms.contentsection.AttachmentList; import org.librecms.contentsection.AttachmentList;
import org.librecms.contentsection.AttachmentListManager; import org.librecms.contentsection.AttachmentListManager;
import org.librecms.contentsection.ContentItem;
import org.librecms.contentsection.ContentItemRepository;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.transaction.Transactional;
/** /**
* *
@ -35,9 +47,27 @@ class RelatedInfoStepController {
@Inject @Inject
private AttachmentListManager attachmentListManager; private AttachmentListManager attachmentListManager;
@Inject
private ConfigurationManager confManager;
@Inject
private ContentItemRepository itemRepo;
@Inject @Inject
private EntityManager entityManager; private EntityManager entityManager;
private Locale defaultLocale;
@PostConstruct
private void init() {
final KernelConfig kernelConfig = confManager
.findConfiguration(KernelConfig.class);
defaultLocale = kernelConfig.getDefaultLocale();
}
@Transactional(Transactional.TxType.REQUIRED)
protected void saveAttachmentList(final AttachmentList attachmentList) { protected void saveAttachmentList(final AttachmentList attachmentList) {
if (attachmentList.getListId() == 0) { if (attachmentList.getListId() == 0) {
@ -46,4 +76,173 @@ class RelatedInfoStepController {
entityManager.merge(attachmentList); entityManager.merge(attachmentList);
} }
} }
@Transactional(Transactional.TxType.REQUIRED)
void moveToFirst(final ContentItem selectedItem,
final AttachmentList listToMove) {
Objects.requireNonNull(selectedItem);
Objects.requireNonNull(listToMove);
final ContentItem item = itemRepo
.findById(selectedItem.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No ContentItem with ID %d in the database.",
selectedItem.getObjectId())));
final AttachmentList toMove = attachmentListManager
.getAttachmentList(listToMove.getListId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No AttachmentList with ID %d in the database.",
listToMove.getListId())));
final List<AttachmentList> lists = item
.getAttachments()
.stream()
.sorted((list1, list2) -> list1.compareTo(list2))
.collect(Collectors.toList());
toMove.setOrder(0);
lists
.stream()
.filter(current -> !current.equals(toMove))
.forEach(current -> current.setOrder(current.getOrder() + 1));
lists.forEach(entityManager::merge);
}
@Transactional(Transactional.TxType.REQUIRED)
protected void moveAfter(final ContentItem selectedItem,
final AttachmentList listToMove,
final Long destId) {
Objects.requireNonNull(selectedItem);
Objects.requireNonNull(listToMove);
final ContentItem item = itemRepo
.findById(selectedItem.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No ContentItem with ID %d in the database.",
selectedItem.getObjectId())));
final AttachmentList toMove = attachmentListManager
.getAttachmentList(listToMove.getListId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No AttachmentList with ID %d in the database.",
listToMove.getListId())));
final AttachmentList after = attachmentListManager
.getAttachmentList(destId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No AttachmentList with ID %d in the database.", destId)));
final List<AttachmentList> lists = item
.getAttachments()
.stream()
.sorted((list1, list2) -> list1.compareTo(list2))
.collect(Collectors.toList());
if (!lists.contains(toMove)) {
throw new IllegalArgumentException(String
.format("AttachmentList %d is not part of ContentItem %d.",
toMove.getListId(),
item.getObjectId()));
}
if (!lists.contains(after)) {
throw new IllegalArgumentException(String
.format("AttachmentList %d is not part of ContentItem %d.",
after.getListId(),
item.getObjectId()));
}
final int afterIndex = lists.indexOf(after);
for (int i = afterIndex + 1; i < lists.size(); i++) {
final AttachmentList current = lists.get(i);
current.setOrder(current.getOrder() + 1);
entityManager.merge(current);
}
toMove.setOrder(afterIndex + 1);
entityManager.merge(toMove);
}
protected void deleteList(final Long listId) {
final AttachmentList list = attachmentListManager
.getAttachmentList(listId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No AttachmentList with ID %d in the database.",
listId)));
entityManager.remove(list);
}
@Transactional(Transactional.TxType.REQUIRED)
List<AttachmentListTableRow> retrieveAttachmentLists(
final ContentItem forContentItem,
final Locale selectedLocale) {
final ContentItem item = itemRepo
.findById(forContentItem.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No ContentItem with ID %d in the database.",
forContentItem.getObjectId())));
return item.getAttachments()
.stream()
.filter(list -> !list.getName().startsWith("."))
.map(list -> buildAttachmentListTableRow(list, selectedLocale))
.collect(Collectors.toList());
}
private AttachmentListTableRow buildAttachmentListTableRow(
final AttachmentList attachmentList,
final Locale selectedLocale) {
final AttachmentListTableRow row = new AttachmentListTableRow();
row.setListId(attachmentList.getListId());
row.setName(attachmentList.getName());
if (attachmentList.getTitle().hasValue(selectedLocale)) {
row.setTitle(attachmentList.getTitle().getValue(selectedLocale));
} else if (attachmentList.getTitle().hasValue(defaultLocale)) {
row.setTitle(attachmentList.getTitle().getValue(defaultLocale));
} else {
row.setTitle(attachmentList.getTitle().getValue());
}
if (attachmentList.getDescription().hasValue(selectedLocale)) {
row.setTitle(shortenDescription(attachmentList
.getDescription()
.getValue(selectedLocale)));
} else if (attachmentList.getDescription().hasValue(defaultLocale)) {
row.setTitle(shortenDescription(attachmentList
.getDescription()
.getValue(defaultLocale)));
} else {
row.setTitle(shortenDescription(attachmentList
.getDescription()
.getValue()));
}
return row;
}
private String shortenDescription(final String description) {
if (description.trim().length() < 140) {
return description.trim();
} else {
final String tmp = description.trim().substring(0, 140);
return String
.format("%s...",
tmp.substring(0, tmp.lastIndexOf(" ")));
}
}
} }