diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImageTableRow.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImageTableRow.java deleted file mode 100644 index 6c95e2968..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImageTableRow.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.images; - -/** - * A container for the data shown in the table of assigned images. - * - * @author Jens Pelzetter - */ -class AssignedImageTableRow { - - private long attachmentId; - private String imageUuid; - private String filename; - private long width; - private long height; - private String type; - private String title; - private String caption; - - public long getAttachmentId() { - return attachmentId; - } - - public void setAttachmentId(final long attachmentId) { - this.attachmentId = attachmentId; - } - - public String getImageUuid() { - return imageUuid; - } - - public void setImageUuid(final String imageUuid) { - this.imageUuid = imageUuid; - } - - public String getFilename() { - return filename; - } - - public void setFilename(final String filename) { - this.filename = filename; - } - - public long getWidth() { - return width; - } - - public void setWidth(final long width) { - this.width = width; - } - - public long getHeight() { - return height; - } - - public void setHeight(final long height) { - this.height = height; - } - - public String getType() { - return type; - } - - public void setType(final String type) { - this.type = type; - } - - public String getTitle() { - return title; - } - - public void setTitle(final String title) { - this.title = title; - } - - public String getCaption() { - return caption; - } - - public void setCaption(final String caption) { - this.caption = caption; - } - - - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTable.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTable.java deleted file mode 100644 index 222830168..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTable.java +++ /dev/null @@ -1,265 +0,0 @@ -/* - * 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.images; - -import com.arsdigita.bebop.BoxPanel; -import com.arsdigita.bebop.Component; -import com.arsdigita.bebop.ControlLink; -import com.arsdigita.bebop.FormProcessException; -import com.arsdigita.bebop.Image; -import com.arsdigita.bebop.Label; -import com.arsdigita.bebop.PageState; -import com.arsdigita.bebop.Table; -import com.arsdigita.bebop.Text; -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.ItemAttachmentSelectionModel; -import com.arsdigita.globalization.GlobalizedMessage; - -import org.libreccm.cdi.utils.CdiUtil; -import org.librecms.CmsConstants; -import org.librecms.contentsection.ItemAttachment; - -/** - * - * @author Jens Pelzetter - */ -class AssignedImagesTable extends Table { - - protected static final int COL_PREVIEW = 0; - protected static final int COL_TITLE = 1; - protected static final int COL_IMAGE_DATA = 2; - protected static final int COL_CAPTION = 3; - protected static final int COL_MOVE = 4; - protected static final int COL_REMOVE = 5; - - private final ItemSelectionModel itemSelectionModel; - private final ItemAttachmentSelectionModel moveAttachmentModel; - private final StringParameter selectedLanguageParam; - - public AssignedImagesTable( - final ItemSelectionModel itemSelectionModel, - final ItemAttachmentSelectionModel moveAttachmentModel, - final StringParameter selectedLanguageParam) { - - super(); - this.itemSelectionModel = itemSelectionModel; - this.moveAttachmentModel = moveAttachmentModel; - this.selectedLanguageParam = selectedLanguageParam; - - final TableColumnModel columnModel = super.getColumnModel(); - columnModel.add(new TableColumn( - COL_PREVIEW, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.preview_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_TITLE, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.title_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_IMAGE_DATA, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.properties_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_CAPTION, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.caption_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_MOVE, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.move_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_REMOVE, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.remove_header", - CmsConstants.CMS_BUNDLE)))); - - super - .setModelBuilder(new AssignedImagesTableModelBuilder( - itemSelectionModel, - moveAttachmentModel, - selectedLanguageParam)); - - super - .getColumn(COL_PREVIEW) - .setCellRenderer(new ThumbnailCellRenderer()); - super - .getColumn(COL_IMAGE_DATA) - .setCellRenderer(new PropertiesCellRenderer()); - super - .getColumn(COL_MOVE) - .setCellRenderer(new MoveCellRenderer()); - super - .getColumn(COL_REMOVE) - .setCellRenderer(new RemoveCellRenderer()); - - 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_MOVE: - if (moveAttachmentModel.getSelectedKey(state) == null) { - - moveAttachmentModel - .setSelectedKey(state, - Long.parseLong((String) event - .getRowKey())); - } else { - final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final ImageStepController controller = cdiUtil - .findBean(ImageStepController.class); - - final ItemAttachment selectedAttachment - = moveAttachmentModel - .getSelectedAttachment(state); - - final Long destId = Long - .parseLong((String) event.getRowKey()); - - controller.moveAfter(selectedAttachment, destId); - moveAttachmentModel.clearSelection(state); - } - break; - case COL_REMOVE: - final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final ImageStepController controller = cdiUtil - .findBean(ImageStepController.class); - controller.deleteAttachment(Long - .parseLong((String) event.getRowKey())); - break; - } - } - - @Override - public void headSelected(final TableActionEvent event) { - //Nothing - } - - }); - - super.setEmptyView(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.none", - CmsConstants.CMS_BUNDLE))); - - } - - private class ThumbnailCellRenderer 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) { - if (value == null) { - return new Text(""); - } else { - final Image image = new Image((String) value, ""); - return image; - } - } - - } - - private class PropertiesCellRenderer 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) { - - @SuppressWarnings("unchecked") - final ImageProperties properties = (ImageProperties) value; - final BoxPanel panel = new BoxPanel(BoxPanel.VERTICAL); - - panel.add(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.properties.filename", - CmsConstants.CMS_BUNDLE, - new Object[]{properties.getFilename()}))); - panel.add(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.properties.size", - CmsConstants.CMS_BUNDLE, - new Object[]{properties.getWidth(), properties.getHeight()}))); - panel.add(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.properties.type", - CmsConstants.CMS_BUNDLE, - new String[]{properties.getType()}))); - - return panel; - } - - } - - 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 RemoveCellRenderer 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); - } - - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTableModel.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTableModel.java deleted file mode 100644 index a22f392e9..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTableModel.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * 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.images; - -import com.arsdigita.bebop.Label; -import com.arsdigita.bebop.PageState; -import com.arsdigita.bebop.table.TableModel; -import com.arsdigita.cms.CMS; -import com.arsdigita.cms.ui.authoring.assets.ItemAttachmentSelectionModel; -import com.arsdigita.globalization.GlobalizedMessage; -import com.arsdigita.web.CCMDispatcherServlet; - -import org.librecms.CmsConstants; - -import java.util.Iterator; -import java.util.List; - -/** - * - * @author Jens Pelzetter - */ -class AssignedImagesTableModel implements TableModel { - - private final PageState state; - private final ItemAttachmentSelectionModel moveAttachmentModel; - - private final Iterator iterator; - private AssignedImageTableRow currentRow; - - public AssignedImagesTableModel( - final List rows, - final PageState state, - final ItemAttachmentSelectionModel moveAttachmentModel) { - - this.iterator = rows.iterator(); - this.state = state; - this.moveAttachmentModel = moveAttachmentModel; - } - - @Override - public int getColumnCount() { - return 4; - } - - @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 AssignedImagesTable.COL_PREVIEW: - return String - .format("%s/content-sections/%s/images/" - + "uuid-%s?width=150&height=100", - CCMDispatcherServlet.getContextPath(), - CMS.getContext().getContentSection().getLabel(), - currentRow.getImageUuid()); - case AssignedImagesTable.COL_IMAGE_DATA: { - final ImageProperties imageProperties = new ImageProperties(); - imageProperties.setFilename(currentRow.getFilename()); - imageProperties.setWidth(currentRow.getWidth()); - imageProperties.setHeight(currentRow.getHeight()); - imageProperties.setType(currentRow.getType()); - - return imageProperties; - } - case AssignedImagesTable.COL_TITLE: - return currentRow.getTitle(); - case AssignedImagesTable.COL_CAPTION: - return currentRow.getCaption(); - case AssignedImagesTable.COL_MOVE: - if (moveAttachmentModel.getSelectedKey(state) == null) { - return new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_image.link_move", - CmsConstants.CMS_BUNDLE)); - } else { - return new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_image.link_move_below_here", - CmsConstants.CMS_BUNDLE)); - } - case AssignedImagesTable.COL_REMOVE: - return new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.remove", - CmsConstants.CMS_BUNDLE)); - default: - throw new IllegalArgumentException(String.format( - "Illegal column index %d.", columnIndex)); - } - } - - @Override - public Object getKeyAt(final int columnIndex) { - return currentRow.getAttachmentId(); - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTableModelBuilder.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTableModelBuilder.java deleted file mode 100644 index b95ed6352..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AssignedImagesTableModelBuilder.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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.images; - -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.ItemAttachmentSelectionModel; -import com.arsdigita.util.LockableImpl; - -import org.libreccm.cdi.utils.CdiUtil; -import org.librecms.contentsection.ContentItem; - -import java.util.List; -import java.util.Locale; - -/** - * Creates the {@link AssignedImagesTableModel} which is used in - * {@link ImageStep} for the table of assigned images. This - * {@link TableModelBuilder} takes the selected item and retrieves all retrieved - * images from the item. For each assigned image an instance - * {@link AssignedImageTableRow} is created. The resulting list (which might be - * empty) is than used to create the image model. This also means that all - * interaction with the database is done by this {@link TableModelBuilder}. - * - * @author Jens Pelzetter - */ -class AssignedImagesTableModelBuilder extends LockableImpl implements - TableModelBuilder { - - private final ItemSelectionModel itemSelectionModel; - private final ItemAttachmentSelectionModel moveAttachmentModel; - private final StringParameter selectedLanguageParam; - - /** - * Constructor for this {@link TableModelBuilder}. - * - * @param itemSelectionModel The model used to determine the selected - * {@link ContentItem}. - * @param selectedLanguageParam Parameter used to determine the selected - * language variant of the selected - * {@link ContentItem}. - */ - protected AssignedImagesTableModelBuilder( - final ItemSelectionModel itemSelectionModel, - final ItemAttachmentSelectionModel moveAttachmentModel, - final StringParameter selectedLanguageParam) { - - this.itemSelectionModel = itemSelectionModel; - this.moveAttachmentModel = moveAttachmentModel; - 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 ImageStepController controller = cdiUtil - .findBean(ImageStepController.class); - - final List rows = controller - .retrieveAssignedImagesRows(selectedItem, selectedLocale); - - return new AssignedImagesTableModel(rows, - state, - moveAttachmentModel); - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImageTableRow.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImageTableRow.java deleted file mode 100644 index ca1411e7f..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImageTableRow.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * 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.images; - -/** - * A container for the data shown in the table of assigned images. - * - * @author Jens Pelzetter - */ -class AvailableImageTableRow { - - private long imageId; - private String imageUuid; - private String title; - private String filename; - private long width; - private long height; - private String type; - private String caption; - - public long getImageId() { - return imageId; - } - - public void setImageId(final long imageId) { - this.imageId = imageId; - } - - public String getImageUuid() { - return imageUuid; - } - - public void setImageUuid(final String imageUuid) { - this.imageUuid = imageUuid; - } - - public String getTitle() { - return title; - } - - public void setTitle(final String title) { - this.title = title; - } - - public String getFilename() { - return filename; - } - - public void setFilename(final String filename) { - this.filename = filename; - } - - public long getWidth() { - return width; - } - - public void setWidth(final long width) { - this.width = width; - } - - public long getHeight() { - return height; - } - - public void setHeight(final long height) { - this.height = height; - } - - public String getType() { - return type; - } - - public void setType(final String type) { - this.type = type; - } - - public String getCaption() { - return caption; - } - - public void setCaption(final String caption) { - this.caption = caption; - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImages.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImages.java deleted file mode 100644 index 8b70dbeff..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImages.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * 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.images; - -import com.arsdigita.bebop.BoxPanel; -import com.arsdigita.bebop.Component; -import com.arsdigita.bebop.ControlLink; -import com.arsdigita.bebop.Form; -import com.arsdigita.bebop.FormProcessException; -import com.arsdigita.bebop.Label; -import com.arsdigita.bebop.PageState; -import com.arsdigita.bebop.PaginationModelBuilder; -import com.arsdigita.bebop.Paginator; -import com.arsdigita.bebop.Table; -import com.arsdigita.bebop.Text; -import com.arsdigita.bebop.event.TableActionEvent; -import com.arsdigita.bebop.event.TableActionListener; -import com.arsdigita.bebop.form.Submit; -import com.arsdigita.bebop.form.TextField; -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.globalization.GlobalizedMessage; - -import org.libreccm.cdi.utils.CdiUtil; -import org.librecms.CmsConstants; - -/** - * - * @author Jens Pelzetter - */ -class AvailableImages extends BoxPanel { - - protected static final int COL_PREVIEW = 0; - protected static final int COL_TITLE = 1; - protected static final int COL_PROPERTIES = 2; - protected static final int COL_CAPTION = 3; - protected static final int COL_ADD = 4; - - public AvailableImages(final ImageStep imageStep, - final ItemSelectionModel itemSelectionModel, - final StringParameter selectedLanguageParam) { - - super(BoxPanel.VERTICAL); - - final Form filterForm = new Form("filter_available_images_form", - new BoxPanel(BoxPanel.HORIZONTAL)); - final TextField filterField = new TextField("filter_available_images"); - filterField.setLabel(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.filter_label", - CmsConstants.CMS_BUNDLE)); - final Submit submitFilter = new Submit(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.submit_filter", - CmsConstants.CMS_BUNDLE)); - filterForm.add(filterField); - filterForm.add(submitFilter); - - super.add(filterForm); - - final Paginator paginator = new Paginator(new PaginationModelBuilder() { - - @Override - public int getTotalSize(final Paginator paginator, - final PageState state) { - final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final ImageStepController controller = cdiUtil - .findBean(ImageStepController.class); - - return (int) controller - .getNumberOfAvailableImages( - itemSelectionModel.getSelectedItem(state), - (String) filterField.getValue(state)); - } - - @Override - public boolean isVisible(final PageState state) { - return true; - } - - }, - 30); - - super.add(paginator); - - final Table table = new Table(); - final TableColumnModel columnModel = table.getColumnModel(); - columnModel.add(new TableColumn( - COL_PREVIEW, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.preview_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_TITLE, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.title_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_PROPERTIES, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.properties_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_CAPTION, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.caption_header", - CmsConstants.CMS_BUNDLE)))); - columnModel.add(new TableColumn( - COL_ADD, - new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.select_header", - CmsConstants.CMS_BUNDLE)))); - - table.setModelBuilder(new AvailableImagesTableModelBuilder( - itemSelectionModel, selectedLanguageParam, filterField, paginator)); - - table - .getColumn(COL_PREVIEW) - .setCellRenderer(new ThumbnailCellRenderer()); - table - .getColumn(COL_PROPERTIES) - .setCellRenderer(new PropertiesCellRenderer()); - table - .getColumn(COL_ADD) - .setCellRenderer(new AddCellRenderer()); - - table.setEmptyView(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.none", - CmsConstants.CMS_BUNDLE))); - - table.addTableActionListener(new TableActionListener() { - - @Override - public void cellSelected(final TableActionEvent event) - throws FormProcessException { - - final Object rowKey = event.getRowKey(); - final long imageId; - if (rowKey instanceof Long) { - imageId = (Long) event.getRowKey(); - } else if(rowKey instanceof String) { - imageId = Long.parseLong((String) rowKey); - } else { - imageId = Long.parseLong(rowKey.toString()); - } - - final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final ImageStepController controller = cdiUtil - .findBean(ImageStepController.class); - controller - .attachImage(itemSelectionModel - .getSelectedItem(event.getPageState()), - imageId); - - imageStep.showAssignedImages(event.getPageState()); - } - - @Override - public void headSelected(final TableActionEvent event) { - //Nothing - } - - }); - - super.add(table); - } - - private class ThumbnailCellRenderer 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) { - if (value == null) { - return new Text(""); - } else { - final com.arsdigita.bebop.Image image - = new com.arsdigita.bebop.Image( - (String) value, ""); - return image; - } - } - - } - - private class PropertiesCellRenderer 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) { - - @SuppressWarnings("unchecked") - final ImageProperties properties = (ImageProperties) value; - final BoxPanel panel = new BoxPanel(BoxPanel.VERTICAL); - - panel.add(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.properties.filename", - CmsConstants.CMS_BUNDLE, - new Object[]{properties.getFilename()}))); - panel.add(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.properties.size", - CmsConstants.CMS_BUNDLE, - new Object[]{properties.getWidth(), properties.getHeight()}))); - panel.add(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.properties.type", - CmsConstants.CMS_BUNDLE, - new String[]{properties.getType()}))); - - return panel; - } - - } - - private class AddCellRenderer 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); - } - - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImagesTableModel.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImagesTableModel.java deleted file mode 100644 index acc3eb2ff..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImagesTableModel.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.images; - -import com.arsdigita.bebop.Label; -import com.arsdigita.bebop.table.TableModel; -import com.arsdigita.cms.CMS; -import com.arsdigita.globalization.GlobalizedMessage; -import com.arsdigita.web.CCMDispatcherServlet; - -import org.librecms.CmsConstants; - -import java.util.Iterator; -import java.util.List; - -/** - * - * @author Jens Pelzetter - */ -class AvailableImagesTableModel implements TableModel { - - private final Iterator iterator; - private AvailableImageTableRow currentRow; - - public AvailableImagesTableModel(final List rows) { - iterator = rows.iterator(); - } - - @Override - public int getColumnCount() { - return 4; - } - - @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 AvailableImages.COL_PREVIEW: - return String - .format("%s/content-sections/%s/images/" - + "uuid-%s?width=150&height=100", - CCMDispatcherServlet.getContextPath(), - CMS.getContext().getContentSection().getLabel(), - currentRow.getImageUuid()); - case AvailableImages.COL_TITLE: - return currentRow.getTitle(); - case AvailableImages.COL_PROPERTIES: - final ImageProperties imageProperties = new ImageProperties(); - imageProperties.setFilename(currentRow.getFilename()); - imageProperties.setWidth(currentRow.getWidth()); - imageProperties.setHeight(currentRow.getHeight()); - imageProperties.setType(currentRow.getType()); - - return imageProperties; - case AvailableImages.COL_CAPTION: - return currentRow.getCaption(); - case AvailableImages.COL_ADD: - return new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.available_images.add", - CmsConstants.CMS_BUNDLE)); - default: - throw new IllegalArgumentException(String.format( - "Illegal column index %d.", columnIndex)); - } - - } - - @Override - public Object getKeyAt(final int columnIndex) { - return currentRow.getImageId(); - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImagesTableModelBuilder.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImagesTableModelBuilder.java deleted file mode 100644 index a2e98a427..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/AvailableImagesTableModelBuilder.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.images; - -import com.arsdigita.bebop.PageState; -import com.arsdigita.bebop.Paginator; -import com.arsdigita.bebop.Table; -import com.arsdigita.bebop.form.TextField; -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.util.LockableImpl; - -import org.libreccm.cdi.utils.CdiUtil; -import org.librecms.assets.Image; -import org.librecms.contentsection.ContentItem; -import org.librecms.contentsection.ItemAttachment; - -import java.util.List; -import java.util.Locale; -import java.util.stream.Collectors; - -/** - * - * @author Jens Pelzetter - */ -public class AvailableImagesTableModelBuilder - extends LockableImpl - implements TableModelBuilder { - - private final ItemSelectionModel itemSelectionModel; - private final StringParameter selectedLanguageParam; - private final TextField filterField; - private final Paginator paginator; - - public AvailableImagesTableModelBuilder( - final ItemSelectionModel itemSelectionModel, - final StringParameter selectedLanguageParam, - final TextField filterField, - final Paginator paginator) { - - this.itemSelectionModel = itemSelectionModel; - this.selectedLanguageParam = selectedLanguageParam; - this.filterField = filterField; - this.paginator = paginator; - } - - @Override - public TableModel makeModel(final Table table, - final PageState state) { - - final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final ImageStepController controller = cdiUtil - .findBean(ImageStepController.class); - - final ContentItem selectedItem = itemSelectionModel - .getSelectedItem(state); - - final List> imageAttachments = controller - .retrieveAssignedImages(selectedItem); - - final List excludedImages = imageAttachments - .stream() - .map(attachment -> attachment.getAsset()) - .collect(Collectors.toList()); - - //Paginator count from 1, JPA from 0 - final int firstResult = paginator.getFirst(state) - 1; - final int maxResults = paginator.getPageSize(state); - - final Locale selectedLocale = new Locale((String) state.getValue(selectedLanguageParam)); - - final List rows = controller - .getAvailableImageRows(excludedImages, - selectedLocale, - (String) filterField.getValue(state), - firstResult, - maxResults); - - return new AvailableImagesTableModel(rows); - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageProperties.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageProperties.java deleted file mode 100644 index a8b8581ea..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageProperties.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.images; - -/** - * - * @author Jens Pelzetter - */ -class ImageProperties { - - protected static final String IMAGE_DATA_FILE_NAME = "filename"; - protected static final String IMAGE_DATA_WIDTH = "width"; - protected static final String IMAGE_DATA_HEIGHT = "height"; - protected static final String IMAGE_DATA_TYPE = "type"; - - private String filename; - - private long width; - - private long height; - - private String type; - - public String getFilename() { - return filename; - } - - public void setFilename(final String filename) { - this.filename = filename; - } - - public long getWidth() { - return width; - } - - public void setWidth(final long width) { - this.width = width; - } - - public long getHeight() { - return height; - } - - public void setHeight(final long height) { - this.height = height; - } - - public String getType() { - return type; - } - - public void setType(final String type) { - this.type = type; - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageStep.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageStep.java deleted file mode 100644 index e045f90ac..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageStep.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * 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.images; - -import com.arsdigita.bebop.ActionLink; -import com.arsdigita.bebop.BoxPanel; -import com.arsdigita.bebop.ControlLink; -import com.arsdigita.bebop.Label; -import com.arsdigita.bebop.Page; -import com.arsdigita.bebop.PageState; -import com.arsdigita.bebop.Table; -import com.arsdigita.bebop.parameters.LongParameter; -import com.arsdigita.bebop.parameters.StringParameter; -import com.arsdigita.cms.ItemSelectionModel; -import com.arsdigita.cms.ui.authoring.AuthoringKitWizard; -import com.arsdigita.cms.ui.authoring.ResettableContainer; -import com.arsdigita.cms.ui.authoring.assets.ItemAttachmentSelectionModel; -import com.arsdigita.globalization.GlobalizedMessage; -import com.arsdigita.kernel.KernelConfig; - -import org.libreccm.cdi.utils.CdiUtil; -import org.librecms.CmsConstants; -import org.librecms.contentsection.ContentItem; -import org.librecms.contentsection.ItemAttachment; -import org.librecms.ui.authoring.ContentItemAuthoringStep; - -import java.util.Locale; - -/** - * - * @author Jens Pelzetter - */ -@ContentItemAuthoringStep( - labelBundle = CmsConstants.CMS_BUNDLE, - labelKey = "image_step.label", - descriptionBundle = CmsConstants.CMS_BUNDLE, - descriptionKey = "image_step.description") -public class ImageStep extends ResettableContainer { - - public static final String IMAGES_ATTACHMENT_LIST = ".images"; - - private final LongParameter moveAttachmentParam; - private final ItemAttachmentSelectionModel moveAttachmentModel; - - private final Label assignedImagesHeader; - private final ControlLink addImageLink; - private final ActionLink beginLink; - private final Table assignedImagesTable; - private final Label addImageHeader; - private final AvailableImages availableImages; - private final ControlLink cancelAddImage; - - public ImageStep(final ItemSelectionModel itemSelectionModel, - final AuthoringKitWizard authoringKitWizard, - final StringParameter selectedLanguageParam) { - - super(); - - moveAttachmentParam = new LongParameter("moveAttachment"); - moveAttachmentModel = new ItemAttachmentSelectionModel( - moveAttachmentParam); - - assignedImagesHeader = new Label(event -> { - final PageState state = event.getPageState(); - final Label target = (Label) event.getTarget(); - - final ContentItem selectedItem = itemSelectionModel - .getSelectedItem(state); - final String selectedLanguage = (String) state - .getValue(selectedLanguageParam); - final Locale selectedLocale = new Locale(selectedLanguage); - final String title; - if (selectedItem.getTitle().hasValue(selectedLocale)) { - title = selectedItem.getTitle().getValue(selectedLocale); - } else { - title = selectedItem.getTitle().getValue(KernelConfig - .getConfig() - .getDefaultLocale()); - } - - target.setLabel(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.heading", - CmsConstants.CMS_BUNDLE, - new String[]{title})); - }); - assignedImagesHeader.setClassAttr(""); - - addImageHeader = new Label(event -> { - final PageState state = event.getPageState(); - final Label target = (Label) event.getTarget(); - - final ContentItem selectedItem = itemSelectionModel - .getSelectedItem(state); - final String selectedLanguage = (String) state - .getValue(selectedLanguageParam); - final Locale selectedLocale = new Locale(selectedLanguage); - final String title; - if (selectedItem.getTitle().hasValue(selectedLocale)) { - title = selectedItem.getTitle().getValue(selectedLocale); - } else { - title = selectedItem.getTitle().getValue(KernelConfig - .getConfig() - .getDefaultLocale()); - } - - target.setLabel(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.add_heading", - CmsConstants.CMS_BUNDLE, - new String[]{title})); - }); - - addImageLink = new ActionLink(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.add_image", - CmsConstants.CMS_BUNDLE))); - addImageLink.addActionListener(event -> { - showAvailableImages(event.getPageState()); - }); - - beginLink = new ActionLink(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.move_to_beginning", - CmsConstants.CMS_BUNDLE)); - - beginLink.addActionListener(event -> { - final PageState state = event.getPageState(); - final ItemAttachment toMove = moveAttachmentModel - .getSelectedAttachment(state); - - final ImageStepController controller = CdiUtil - .createCdiUtil() - .findBean(ImageStepController.class); - - controller.moveToFirst(toMove); - - moveAttachmentModel.clearSelection(state); - }); - - assignedImagesTable = new AssignedImagesTable(itemSelectionModel, - moveAttachmentModel, - selectedLanguageParam); - - cancelAddImage = new ControlLink(new Label(new GlobalizedMessage( - "cms.ui.authoring.assets.imagestep.assigned_images.cancel_add_image", - CmsConstants.CMS_BUNDLE))); - cancelAddImage.addActionListener(event -> { - showAssignedImages(event.getPageState()); - }); - - availableImages = new AvailableImages(this, - itemSelectionModel, - selectedLanguageParam); - - final BoxPanel panel = new BoxPanel(BoxPanel.VERTICAL); - panel.add(assignedImagesHeader); - panel.add(addImageLink); - panel.add(beginLink); - panel.add(assignedImagesTable); - panel.add(addImageHeader); - panel.add(availableImages); - super.add(panel); - - moveAttachmentModel.addChangeListener(event -> { - - final PageState state = event.getPageState(); - - if (moveAttachmentModel.getSelectedKey(state) == null) { - addImageLink.setVisible(state, true); - beginLink.setVisible(state, false); - } else { - addImageLink.setVisible(state, false); - beginLink.setVisible(state, true); - } - }); - } - - @Override - public void register(final Page page) { - super.register(page); - - page.setVisibleDefault(assignedImagesHeader, true); - page.setVisibleDefault(addImageLink, true); - page.setVisibleDefault(beginLink, false); - page.setVisibleDefault(assignedImagesTable, true); - page.setVisibleDefault(addImageHeader, false); - page.setVisibleDefault(cancelAddImage, false); - page.setVisibleDefault(availableImages, false); - - page.addComponentStateParam(assignedImagesTable, moveAttachmentParam); - } - - protected void showAssignedImages(final PageState state) { - assignedImagesHeader.setVisible(state, true); - addImageLink.setVisible(state, true); - assignedImagesTable.setVisible(state, true); - addImageHeader.setVisible(state, false); - - availableImages.setVisible(state, false); - } - - protected void showAvailableImages(final PageState state) { - assignedImagesHeader.setVisible(state, false); - addImageLink.setVisible(state, false); - assignedImagesTable.setVisible(state, false); - addImageHeader.setVisible(state, true); - cancelAddImage.setVisible(state, true); - availableImages.setVisible(state, true); - } - -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageStepController.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageStepController.java deleted file mode 100644 index c20818a36..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/assets/images/ImageStepController.java +++ /dev/null @@ -1,474 +0,0 @@ -/* - * 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.images; - -import com.arsdigita.kernel.KernelConfig; - -import org.libreccm.configuration.ConfigurationManager; -import org.libreccm.l10n.LocalizedString; -import org.librecms.assets.Image; -import org.librecms.contentsection.AssetRepository; -import org.librecms.contentsection.AttachmentList; -import org.librecms.contentsection.AttachmentListManager; -import org.librecms.contentsection.ContentItem; -import org.librecms.contentsection.ContentItemRepository; -import org.librecms.contentsection.ItemAttachment; -import org.librecms.contentsection.ItemAttachmentManager; - -import java.util.ArrayList; -import java.util.Collections; -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.inject.Inject; -import javax.persistence.EntityManager; -import javax.persistence.TypedQuery; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Join; -import javax.persistence.criteria.Root; -import javax.transaction.Transactional; - -/** - * Encapsulates all interaction between the {@link ImageStep} and associated - * classes like the {@link ImageStepTableModelBuilder} and CDI beans. - * - * @author Jens Pelzetter - */ -@RequestScoped -class ImageStepController { - - @Inject - private AssetRepository assetRepo; - - @Inject - private AttachmentListManager attachmentListManager; - - @Inject - private ItemAttachmentManager attachmentManager; - - @Inject - private ConfigurationManager confManager; - - @Inject - private EntityManager entityManager; - - @Inject - private ContentItemRepository itemRepo; - - private Locale defaultLocale; - - @PostConstruct - private void init() { - - final KernelConfig kernelConfig = confManager - .findConfiguration(KernelConfig.class); - defaultLocale = kernelConfig.getDefaultLocale(); - } - - @Transactional(Transactional.TxType.REQUIRED) - protected List> retrieveAssignedImages( - final ContentItem fromContentItem) { - - Objects.requireNonNull(fromContentItem); - - final ContentItem item = itemRepo - .findById(fromContentItem.getObjectId()) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No ContentItem with id %d in the database.", - fromContentItem.getObjectId()))); - - final List imageLists = attachmentListManager - .getAttachmentList(item, ImageStep.IMAGES_ATTACHMENT_LIST); - - if (imageLists.isEmpty()) { - return Collections.emptyList(); - } - - final List> attachments = new ArrayList<>(); - for (final AttachmentList imageList : imageLists) { - for (final ItemAttachment attachment : imageList.getAttachments()) { - attachments.add(attachment); - } - } - - @SuppressWarnings("unchecked") - final List> imageAttachments = attachments - .stream() - .sorted((attachment1, attachment2) -> { - return attachment1.compareTo(attachment2); - }) - .filter(attachment -> attachment.getAsset() instanceof Image) - .map(attachment -> (ItemAttachment) attachment) - .collect(Collectors.toList()); - - return imageAttachments; - } - - @Transactional(Transactional.TxType.REQUIRED) - protected List retrieveAssignedImagesRows( - final ContentItem fromContentItem, final Locale selectedLocale) { - -// Objects.requireNonNull(fromContentItem); -// Objects.requireNonNull(selectedLocale); -// -// final ContentItem item = itemRepo -// .findById(fromContentItem.getObjectId()) -// .orElseThrow(() -> new IllegalArgumentException(String -// .format("No ContentItem with id %d in the database.", -// fromContentItem.getObjectId()))); -// -// final List imageLists = attachmentListManager -// .getAttachmentList(item, ImageStep.IMAGES_ATTACHMENT_LIST); -// -// if (imageLists.isEmpty()) { -// return Collections.emptyList(); -// } -// -// final List> attachments = new ArrayList<>(); -// for (final AttachmentList imageList : imageLists) { -// for (final ItemAttachment attachment : imageList.getAttachments()) { -// attachments.add(attachment); -// } -// } - final List> imageAttachments - = retrieveAssignedImages( - fromContentItem); - -// @SuppressWarnings("unchecked") -// final List rows = attachments -// .stream() -// .sorted((attachment1, attachment2) -> { -// return attachment1.compareTo(attachment2); -// }) -// .filter(attachment -> attachment.getAsset() instanceof Image) -// .map(attachment -> (ItemAttachment) attachment) -// .map(imageAttachment -> buildAssignedImageTableRow(imageAttachment, -// selectedLocale)) -// .collect(Collectors.toList()); -// -// return rows; - return imageAttachments - .stream() - .map(imageAttachment -> buildAssignedImageTableRow(imageAttachment, - selectedLocale)) - .collect(Collectors.toList()); - } - - @Transactional(Transactional.TxType.REQUIRED) - protected void moveToFirst(final ItemAttachment attachmentToMove) { - - Objects.requireNonNull(attachmentToMove); - - final ItemAttachment toMove = attachmentManager - .findById(attachmentToMove.getAttachmentId()) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No ItemAttachment with ID %d in the database.", - attachmentToMove.getAttachmentId()))); - - final AttachmentList attachmentList = toMove.getAttachmentList(); - final List> attachments = attachmentList - .getAttachments() - .stream() - .sorted((attachment1, attachment2) -> { - return attachment1.compareTo(attachment2); - }) - .collect(Collectors.toList()); - - toMove.setSortKey(0); - attachments - .stream() - .filter(current -> !current.equals(toMove)) - .forEach(current -> current.setSortKey(current.getSortKey() + 1)); - - attachments - .forEach(current -> entityManager.merge(current)); - } - - @Transactional(Transactional.TxType.REQUIRED) - protected void moveAfter(final ItemAttachment attachmentToMove, - final long destId) { - - Objects.requireNonNull(attachmentToMove); - - final ItemAttachment toMove = attachmentManager - .findById(attachmentToMove.getAttachmentId()) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No ItemAttachment with ID %d in the database.", - attachmentToMove.getAttachmentId()))); - - final ItemAttachment after = attachmentManager - .findById(destId) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No ItemAttachment with ID %d in the database.", - attachmentToMove.getAttachmentId()))); - - final AttachmentList attachmentList = toMove.getAttachmentList(); - final List> attachments = attachmentList - .getAttachments() - .stream() - .sorted((attachment1, attachment2) -> { - return attachment1.compareTo(attachment2); - }) - .collect(Collectors.toList()); - - if (!attachments.contains(toMove)) { - throw new IllegalArgumentException(String.format( - "ItemAttachment %d is not part of AttachmentList %d.", - toMove.getAttachmentId(), - attachmentList.getListId())); - } - - if (!attachments.contains(after)) { - throw new IllegalArgumentException(String.format( - "ItemAttachment %d is not part of AttachmentList %d.", - after.getAttachmentId(), - attachmentList.getListId())); - } - - final int afterIndex = attachments.indexOf(after); - for (int i = afterIndex + 1; i < attachments.size(); i++) { - final ItemAttachment current = attachments.get(i); - current.setSortKey(current.getSortKey() + 1); - entityManager.merge(current); - } - - toMove.setSortKey(afterIndex + 1); - entityManager.merge(toMove); - } - - @Transactional(Transactional.TxType.REQUIRED) - protected void deleteAttachment(final long attachmentId) { - - final ItemAttachment attachment = attachmentManager - .findById(attachmentId) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No ItemAttachment with ID %d in the database.", - attachmentId))); - - deleteAttachment(attachment); - } - - @Transactional(Transactional.TxType.REQUIRED) - protected void deleteAttachment(final ItemAttachment attachment) { - - Objects.requireNonNull(attachment); - - final ItemAttachment toRemove = attachmentManager - .findById(attachment.getAttachmentId()) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No ItemAttachment with ID %d in the database.", - attachment.getAttachmentId()))); - - entityManager.remove(toRemove); - } - - @Transactional(Transactional.TxType.REQUIRED) - protected long getNumberOfAvailableImages(final ContentItem selectedItem, - final String filter) { - - Objects.requireNonNull(selectedItem); - - final ContentItem item = itemRepo - .findById(selectedItem.getObjectId()) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No ContentItem with ID %d in the database.", - selectedItem.getObjectId()))); - - final List> imageAttachments - = retrieveAssignedImages(item); - final List excluededImages = imageAttachments - .stream() - .map(imageAttachment -> imageAttachment.getAsset()) - .collect(Collectors.toList()); - - final CriteriaBuilder criteriaBuilder = entityManager - .getCriteriaBuilder(); - - final CriteriaQuery query = criteriaBuilder - .createQuery(Long.class); - final Root from = query.from(Image.class); - final Join titleJoin = from.join("title"); - final Join titleValuesJoin = titleJoin.join("values"); - - query - .select(criteriaBuilder.count(from)); - - if (filter == null || filter.trim().isEmpty()) { - if (excluededImages != null && !excluededImages.isEmpty()) { - query.where(criteriaBuilder.not(from.in(excluededImages))); - } - } else { - if (excluededImages == null || excluededImages.isEmpty()) { - query.where(criteriaBuilder.like(titleValuesJoin, - String.format("%s%%", filter))); - } else { - query.where(criteriaBuilder.and( - criteriaBuilder.not(from.in(excluededImages)), - criteriaBuilder.like(titleValuesJoin, - String.format("%s%%", filter)))); - } - } - - final long result = entityManager.createQuery(query) - .getSingleResult(); - return result; - } - - @Transactional(Transactional.TxType.REQUIRED) - protected List getAvailableImages(final List excludedImages, - final String filter, - final long firstImage, - final long maxImages) { - - final CriteriaBuilder criteriaBuilder = entityManager - .getCriteriaBuilder(); - - final CriteriaQuery criteriaQuery = criteriaBuilder - .createQuery(Image.class); - final Root from = criteriaQuery.from(Image.class); - final Join titleJoin = from.join("title"); - final Join titleValuesJoin = titleJoin.join("values"); - - if (filter == null || filter.trim().isEmpty()) { - if (excludedImages != null && !excludedImages.isEmpty()) { - criteriaQuery.where(criteriaBuilder.not(from.in( - excludedImages))); - } - } else { - if (excludedImages == null || excludedImages.isEmpty()) { - criteriaQuery - .where(criteriaBuilder.like(titleValuesJoin, - String.format("%s%%", filter))); - } else { - criteriaQuery.where(criteriaBuilder.and( - criteriaBuilder.not(from.in(excludedImages)), - criteriaBuilder.like(titleValuesJoin, - String.format("%s%%", filter)))); - } - } - - final TypedQuery query = entityManager - .createQuery(criteriaQuery); - query.setFirstResult((int) firstImage); - query.setMaxResults((int) maxImages); - - final List result = query.getResultList(); - return result; - - } - - @Transactional(Transactional.TxType.REQUIRED) - protected List getAvailableImageRows( - final List excludedImages, - final Locale selectedLocale, - final String filter, - final long firstImage, - final long lastImage - ) { - - return getAvailableImages(excludedImages, filter, firstImage, - lastImage) - .stream() - .map(image -> buildAvailableImageRow(image, selectedLocale)) - .collect(Collectors.toList()); - - } - - private AvailableImageTableRow buildAvailableImageRow( - final Image image, final Locale selectedLocale) { - final AvailableImageTableRow row = new AvailableImageTableRow(); - row.setImageId(image.getObjectId()); - row.setImageUuid(image.getUuid()); - if (image.getTitle().hasValue(selectedLocale)) { - row.setTitle(image.getTitle().getValue(selectedLocale)); - } else if (image.getTitle().hasValue(defaultLocale)) { - row.setTitle(image.getTitle().getValue(defaultLocale)); - } else { - row.setTitle(image.getTitle().getValue()); - } - row.setFilename(image.getFileName()); - row.setWidth(image.getWidth()); - row.setHeight(image.getHeight()); - row.setType(image.getMimeType().toString()); - - return row; - } - - @Transactional(Transactional.TxType.REQUIRED) - protected void attachImage(final ContentItem contentItem, - final long imageId) { - - final ContentItem item = itemRepo - .findById(contentItem.getObjectId()) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No ContentItem with ID %d in the database.", - contentItem.getObjectId()))); - - final List list = attachmentListManager - .getAttachmentList(item, ImageStep.IMAGES_ATTACHMENT_LIST); - - final AttachmentList addTo; - if (list == null || list.isEmpty()) { - addTo = attachmentListManager - .createAttachmentList(item, ImageStep.IMAGES_ATTACHMENT_LIST); - } else { - addTo = list.get(0); - } - - final Image image = assetRepo - .findById(imageId, Image.class) - .orElseThrow(() -> new IllegalArgumentException(String - .format("No Image with ID %d in the database.", - imageId))); - - attachmentManager.attachAsset(image, addTo); - } - - private AssignedImageTableRow buildAssignedImageTableRow( - final ItemAttachment imageAttachment, - final Locale selectedLocale) { - - final AssignedImageTableRow row = new AssignedImageTableRow(); - - row.setAttachmentId(imageAttachment.getAttachmentId()); - - final Image image = imageAttachment.getAsset(); - row.setImageUuid(image.getUuid()); - row.setFilename(image.getFileName()); - row.setWidth(image.getWidth()); - row.setHeight(image.getHeight()); - row.setType(image.getMimeType().toString()); - if (image.getTitle().hasValue(selectedLocale)) { - row.setTitle(image.getTitle().getValue(selectedLocale)); - } else if (image.getTitle().hasValue(defaultLocale)) { - row.setTitle(image.getTitle().getValue(defaultLocale)); - } else { - row.setTitle(image.getTitle().getValue()); - } - row.setCaption(image.getDescription().getValue(selectedLocale)); - - return row; - } - -}