Removed depcrecated package com.arsdigita.ui.admin.pagemodels from ccm-core

pull/28/head
Jens Pelzetter 2022-03-19 17:45:44 +01:00
parent c9f48f042b
commit 8d7cbea03b
10 changed files with 0 additions and 2421 deletions

View File

@ -1,391 +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.ui.admin.pagemodels;
import com.arsdigita.bebop.Form;
import com.arsdigita.bebop.FormData;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.ParameterSingleSelectionModel;
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.FormValidationListener;
import com.arsdigita.bebop.form.TextField;
import com.arsdigita.bebop.parameters.LongParameter;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.ui.admin.AdminUiConstants;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ComponentModelRepository;
import org.libreccm.pagemodel.PageModel;
/**
* Base form for creating forms for editing/creating components of a
* {@link PageModel}.
*
* Subclasses must provided a constructor with the following signature:
* {@code SomeComponentModelForm(PageModelTab, ParameterSingleSelectionModel, ParameterSingleSelectionModel)}.
*
* This constructor has to call
* {@link #AbstractComponentModelForm(java.lang.String, com.arsdigita.ui.admin.pagemodels.PageModelsTab, com.arsdigita.bebop.ParameterSingleSelectionModel, com.arsdigita.bebop.ParameterSingleSelectionModel)}
*
* with the provided parameters and a unique name for the form. Usually this be
* the name of the component model which is associated with the form and the
* suffix {@code Form}.
*
* The constructor is called is using reflection. The parameters passed to the
* constructor are:
* <ol>
* <li>The {@link PageModelsTab} in which the form is displayed.</li>
* <li>The {@link ParameterSingleSelectionModel} which holds the ID of the
* currently selected {@link PageModel}.</li>
* <li>The {@link ParameterSingleSelectionModel} which holds the ID of the
* currently selected {@link ComponentModel}. The selected key of the selection
* model might be null if a new component model is created.</li>
* </ol>
*
* @param <T>
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public abstract class AbstractComponentModelForm<T extends ComponentModel>
extends Form
implements FormInitListener,
FormValidationListener,
FormProcessListener {
/**
* Constant for identifying the key text field.
*/
private static final String COMPONENT_KEY = "componentKey";
/**
* The {@link PageModelsTab} in which the form is used
*/
private final PageModelsTab pageModelTab;
/**
* ID of the selected {@link PageModel}.
*/
private final ParameterSingleSelectionModel<String> selectedModelId;
/**
* ID of the selected {@link ComponentModel}. {@code null} of empty if a new
* component is added.
*/
private final ParameterSingleSelectionModel<String> selectedComponentId;
/**
* Text field for the component's key in the page model.
*/
private TextField keyField;
private SaveCancelSection saveCancelSection;
/**
* The selected component model.
*/
private T componentModel;
public AbstractComponentModelForm(
final String name,
final PageModelsTab pageModelTab,
final ParameterSingleSelectionModel<String> selectedModelId,
final ParameterSingleSelectionModel<String> selectedComponentId) {
super(name);
this.pageModelTab = pageModelTab;
this.selectedModelId = selectedModelId;
this.selectedComponentId = selectedComponentId;
createWidgets();
super.addInitListener(this);
super.addValidationListener(this);
super.addProcessListener(this);
}
/**
* Helper method called by the constructor to create the widgets of the
* form. The method also calls the {@link #addWidgets()} after the basic
* widgets have been created and adds the {@link SaveCancelSection} at the
* end.
*/
private void createWidgets() {
keyField = new TextField(COMPONENT_KEY);
keyField.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.components.key.label",
AdminUiConstants.ADMIN_BUNDLE));
super.add(keyField);
addWidgets();
saveCancelSection = new SaveCancelSection();
super.add(saveCancelSection);
}
/**
* Provides access to the {@link PageModelsTab}.
*
* @return
*/
protected final PageModelsTab getPageModelTab() {
return pageModelTab;
}
/**
* Provides access the {@link ParameterSingleSelectionModel} holding the ID
* of the currently selected {@link ComponentModel}. The selected key of the
* selection model is {@code null} if a new {@link ComponentModel} is
* created.
*
* @return
*/
protected final ParameterSingleSelectionModel<String> getSelectedComponentId() {
return selectedComponentId;
}
/**
* Provides access to the {@link ParameterSingleSelectionModel} holding the
* ID of the currently selected {@link PageModel}.
*
* @return
*/
protected final ParameterSingleSelectionModel<String> getSelectedModelId() {
return selectedModelId;
}
/**
* Provides access to the {@link SaveCancelSection} of the form allowing
* subclasses to check if the <em>Save</em> button of the
* {@link SaveCancelSection} has been pressed.
*
* @return
*/
protected final SaveCancelSection getSaveCancelSection() {
return saveCancelSection;
}
/**
* Provides access to the currently selected {@link PageModel}. The
* implementation for the init and validation listeners
* ({@link #init(com.arsdigita.bebop.event.FormSectionEvent)} and
* {@link #validate(com.arsdigita.bebop.event.FormSectionEvent)} initialise
* this field.
*
* @return
*/
protected final T getComponentModel() {
return componentModel;
}
/**
* Subclasses have to override this method to add the widgets specific for a
* component model.
*/
protected abstract void addWidgets();
/**
* Creates a new {@link ComponentModel} of a specific type. This method is
* only a wrapper around the constructor. An implementation should not add
* the component to a {@link PageModel} or save the {@link ComponentModel}
* in the database. This class takes care of that.
*
* @return A new {@link ComponentModel}.
*/
protected abstract T createComponentModel();
/**
* Updates the current component model with data from the form.
*
* @param componentModel
* @param state
* @param data
*/
protected abstract void updateComponentModel(T componentModel,
PageState state,
FormData data);
/**
* Init listener for the component form. Subclasses should override this
* method to initialise their fields. If this method is overridden the
* overriding method <strong>must</strong> call {@code super.init(event)}.
* Otherwise the {@link #keyField} will not be initialised properly. Also
* the method loads the selected current component model from the database
* and stores it in the {@link #componentModel} field. Overriding methods
* can access the field using the {@link #getComponentModel()} method. If
* {@link super.init(event)} is not called the {@link #componentModel} field
* will not be initialised.
*
* @param event The event which caused the listener to be invoked.
*
* @throws FormProcessException
*/
@Override
@SuppressWarnings("unchecked")
public void init(final FormSectionEvent event) throws FormProcessException {
final PageState state = event.getPageState();
final String selectedComponentIdStr = selectedComponentId
.getSelectedKey(state);
if (selectedComponentIdStr != null
&& !selectedComponentIdStr.isEmpty()) {
componentModel = loadSelectedComponent(
Long.parseLong(selectedComponentIdStr));
keyField.setValue(state, componentModel.getKey());
}
}
@SuppressWarnings("unchecked")
protected T loadSelectedComponent(final long componentId) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final ComponentModelRepository componentModelRepo = cdiUtil
.findBean(ComponentModelRepository.class);
return (T) componentModelRepo
.findById(componentId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No ComponentModel with ID %d in the database.",
componentId)));
}
/**
* Validation listener for the component form. Subclasses should override
* this method to validate their fields if necessary.. If this method is
* overridden the overriding method <strong>must</strong> call
* {@code super.validate(event)}. Otherwise the {@link #keyField} will not
* be validated properly.
*
* @param event The event which caused the listener to be invoked.
*
* @throws FormProcessException
*/
@Override
public void validate(final FormSectionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
if (saveCancelSection.getSaveButton().isSelected(state)) {
final FormData data = event.getFormData();
final String keyValue = data.getString(COMPONENT_KEY);
if (keyValue == null
|| keyValue.isEmpty()
|| keyValue.matches("\\s*")) {
data.addError(COMPONENT_KEY,
new GlobalizedMessage(
"ui.admin.pagemodels.components.key.error.not_empty",
AdminUiConstants.ADMIN_BUNDLE));
}
}
}
/**
* Process listener for the component form. This method can't be overridden.
* Instead subclasses have to implement
* {@link #updateComponentModel(org.libreccm.pagemodel.ComponentModel, com.arsdigita.bebop.PageState, com.arsdigita.bebop.FormData)}
* to set their specific values on the current component model. The
* implementation of that method is called by the this method.
*
* @param event The event which caused the listener to be invoked.
*
* @throws FormProcessException
*/
@Override
public final void process(final FormSectionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
if (saveCancelSection.getSaveButton().isSelected(state)) {
final String selectedModelIdStr = selectedModelId
.getSelectedKey(state);
final String selectedComponentIdStr = selectedComponentId
.getSelectedKey(state);
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelsController controller = cdiUtil
.findBean(PageModelsController.class);
final FormData data = event.getFormData();
final String keyValue = data.getString(COMPONENT_KEY);
if (selectedComponentIdStr == null
|| selectedComponentIdStr.isEmpty()) {
componentModel = createComponentModel();
componentModel.setKey(keyValue);
updateComponentModel(componentModel, state, data);
controller.addComponentModel(Long.parseLong(selectedModelIdStr),
componentModel);
} else {
componentModel = retrieveComponentModel(selectedComponentIdStr);
componentModel.setKey(keyValue);
updateComponentModel(componentModel, state, data);
final ComponentModelRepository componentModelRepo = cdiUtil
.findBean(ComponentModelRepository.class);
componentModelRepo.save(componentModel);
}
}
selectedComponentId.clearSelection(state);
pageModelTab.showPageModelDetails(state);
}
/**
* Helper method for retrieving the component model from the database.
*
* @param componentModelId The ID of the component model to retrieve.
*
* @return The component model.
*/
@SuppressWarnings("unchecked")
private T retrieveComponentModel(final String componentModelId) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final ComponentModelRepository componentModelRepo = cdiUtil
.findBean(ComponentModelRepository.class);
return (T) componentModelRepo
.findById(Long.parseLong(componentModelId))
.orElseThrow(() -> new IllegalArgumentException(String
.format("No ComponentModel with ID %s in the database",
componentModelId)));
}
}

View File

@ -1,359 +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.ui.admin.pagemodels;
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.ParameterSingleSelectionModel;
import com.arsdigita.bebop.Table;
import com.arsdigita.bebop.event.TableActionEvent;
import com.arsdigita.bebop.event.TableActionListener;
import com.arsdigita.bebop.table.TableColumn;
import com.arsdigita.bebop.table.TableColumnModel;
import com.arsdigita.bebop.table.TableModel;
import com.arsdigita.bebop.table.TableModelBuilder;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.ui.admin.AdminUiConstants;
import com.arsdigita.util.LockableImpl;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.PageModel;
import java.util.Iterator;
import java.util.List;
/**
* Table used in the {@link PageModelDetails} component to list the components
* assigned to a {@link PageModel}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class ComponentsTable extends Table {
protected static final int COL_COMPONENT_KEY = 0;
protected static final int COL_COMPONENT_TYPE = 1;
protected static final int COL_EDIT = 2;
protected static final int COL_DELETE = 3;
/**
* The {@link PageModelsTab} instance in which the table is displayed.
*/
private final PageModelsTab pageModelsTab;
/**
* The selection model for the ID of the selected page model.
*/
private final ParameterSingleSelectionModel<String> selectedModelId;
/**
* The selection model for the ID of the selected component.
*/
private final ParameterSingleSelectionModel<String> selectedComponentId;
/**
* Constructor for the table.
*
* @param pageModelsTab The {@link PageModelsTab} instance in which
* the table is displayed.
* @param selectedModelId The selection model for the ID of the selected
* page model.
* @param selectedComponentId The selection model for the ID of the selected
* component.
*/
ComponentsTable(
final PageModelsTab pageModelsTab,
final ParameterSingleSelectionModel<String> selectedModelId,
final ParameterSingleSelectionModel<String> selectedComponentId) {
super();
super.setIdAttr("pageModelComponentModelsTable");
this.pageModelsTab = pageModelsTab;
this.selectedModelId = selectedModelId;
this.selectedComponentId = selectedComponentId;
super.setEmptyView(new Label(new GlobalizedMessage(
"ui.admin.pagemodels.componentmodels.none",
AdminUiConstants.ADMIN_BUNDLE)));
final TableColumnModel columnModel = getColumnModel();
columnModel.add(new TableColumn(
COL_COMPONENT_KEY,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.componentmodels.cols.key.heading",
AdminUiConstants.ADMIN_BUNDLE))));
columnModel.add(new TableColumn(
COL_COMPONENT_TYPE,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.componentmodels.cols.type.heading",
AdminUiConstants.ADMIN_BUNDLE))));
columnModel.add(new TableColumn(
COL_EDIT,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.componentmodels.cols.edit.heading",
AdminUiConstants.ADMIN_BUNDLE))));
columnModel.add(new TableColumn(
COL_DELETE,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.componentmodels.cols.delete.heading",
AdminUiConstants.ADMIN_BUNDLE))));
columnModel.get(COL_EDIT).setCellRenderer(this::renderEditCell);
// columnModel.get(COL_EDIT).setCellRenderer(
// new 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) {
//
// final ControlLink link = new ControlLink((Component) value);
// return link;
// }
//
// });
columnModel.get(COL_DELETE).setCellRenderer(this::renderDeleteCell);
// columnModel.get(COL_DELETE).setCellRenderer(
// new 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) {
//
// final ControlLink link = new ControlLink((Component) value);
// link.setConfirmation(new GlobalizedMessage(
// "ui.admin.pagemodels.componentmodels.cols.delete.confirmation",
// AdminUiConstants.ADMIN_BUNDLE));
// return link;
// }
//
// });
super.addTableActionListener(new ComponentsTableActionListener());
// super.addTableActionListener(new TableActionListener() {
//
// @Override
// public void cellSelected(final TableActionEvent event)
// throws FormProcessException {
//
// final PageState state = event.getPageState();
// final String selectedModelIdStr = selectedModelId
// .getSelectedKey(state);
// final String key = (String) event.getRowKey();
//
// switch (event.getColumn()) {
// case COL_EDIT:
// selectedComponentId.setSelectedKey(state, key);
// pageModelsTab.showComponentForm(state);
// break;
// case COL_DELETE:
// final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
// final PageModelsController controller = cdiUtil
// .findBean(PageModelsController.class);
// controller.removeComponentModel(
// Long.parseLong(selectedModelIdStr),
// Long.parseLong(key));
// break;
// default:
// throw new IllegalArgumentException(
// "Invalid value for column");
// }
// }
//
// @Override
// public void headSelected(final TableActionEvent event) {
// //Nothing
// }
//
// });
super.setModelBuilder(new ComponentsTableModelBuilder(
selectedModelId));
}
private Component renderEditCell(final Table table,
final PageState state,
final Object value,
final boolean isSelected,
final Object key,
final int row,
final int columnv) {
final ControlLink link = new ControlLink((Component) value);
return link;
}
private Component renderDeleteCell(final Table table,
final PageState state,
final Object value,
final boolean isSelected,
final Object key,
final int row,
final int column) {
final ControlLink link = new ControlLink((Component) value);
link.setConfirmation(new GlobalizedMessage(
"ui.admin.pagemodels.componentmodels.cols.delete.confirmation",
AdminUiConstants.ADMIN_BUNDLE));
return link;
}
private class ComponentsTableActionListener implements TableActionListener {
@Override
public void cellSelected(final TableActionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
final String selectedModelIdStr = selectedModelId
.getSelectedKey(state);
final String key = (String) event.getRowKey();
switch (event.getColumn()) {
case COL_EDIT:
selectedComponentId.setSelectedKey(state, key);
pageModelsTab.showComponentForm(state);
break;
case COL_DELETE:
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelsController controller = cdiUtil
.findBean(PageModelsController.class);
controller.removeComponentModel(
Long.parseLong(selectedModelIdStr),
Long.parseLong(key));
break;
default:
throw new IllegalArgumentException(
"Invalid value for column");
}
}
@Override
public void headSelected(final TableActionEvent event) {
//Nothing
}
}
private class ComponentsTableModelBuilder
extends LockableImpl
implements TableModelBuilder {
private final ParameterSingleSelectionModel<String> selectedModelId;
public ComponentsTableModelBuilder(
final ParameterSingleSelectionModel<String> selectedModelId) {
this.selectedModelId = selectedModelId;
}
@Override
public TableModel makeModel(final Table table,
final PageState state) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelsController controller = cdiUtil
.findBean(PageModelsController.class);
final String selectedModelIdStr = selectedModelId
.getSelectedKey(state);
final List<ComponentModel> components = controller
.retrieveComponents(Long.parseLong(selectedModelIdStr));
return new ComponentsTableModel(components);
}
}
private class ComponentsTableModel implements TableModel {
private final Iterator<ComponentModel> iterator;
private ComponentModel currentComponent;
public ComponentsTableModel(
final List<ComponentModel> components) {
iterator = components.iterator();
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public boolean nextRow() {
if (iterator.hasNext()) {
currentComponent = iterator.next();
return true;
} else {
return false;
}
}
@Override
public Object getElementAt(final int columnIndex) {
switch (columnIndex) {
case ComponentsTable.COL_COMPONENT_KEY:
return currentComponent.getKey();
case ComponentsTable.COL_COMPONENT_TYPE: {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelsController controller = cdiUtil
.findBean(PageModelsController.class);
return controller
.getComponentModelTitle(currentComponent.getClass());
}
case ComponentsTable.COL_EDIT:
return new Label(new GlobalizedMessage(
"ui.admin.pagemodels.components.edit",
AdminUiConstants.ADMIN_BUNDLE));
case ComponentsTable.COL_DELETE:
return new Label(new GlobalizedMessage(
"ui.admin.pagemodels.components.delete",
AdminUiConstants.ADMIN_BUNDLE));
default:
throw new IllegalArgumentException(
"Not a valid column index");
}
}
@Override
public Object getKeyAt(final int columnIndex) {
return currentComponent.getComponentModelId();
}
}
}

View File

@ -1,254 +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.ui.admin.pagemodels;
import com.arsdigita.bebop.ActionLink;
import com.arsdigita.bebop.BoxPanel;
import com.arsdigita.bebop.Form;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.ParameterSingleSelectionModel;
import com.arsdigita.bebop.PropertySheet;
import com.arsdigita.bebop.event.ActionEvent;
import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.bebop.event.PrintEvent;
import com.arsdigita.bebop.event.PrintListener;
import com.arsdigita.bebop.form.Option;
import com.arsdigita.bebop.form.SingleSelect;
import com.arsdigita.bebop.form.Submit;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.ui.admin.AdminUiConstants;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ComponentModels;
import org.libreccm.pagemodel.PageModel;
import org.libreccm.pagemodel.PageModelComponentModel;
import org.libreccm.pagemodel.PageModelManager;
import org.libreccm.pagemodel.PageModelRepository;
import java.util.List;
import java.util.TooManyListenersException;
/**
* Shows the details about a {@link PageModel} including the
* {@link ComponentModel}s assigned to the {@link PageModel}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class PageModelDetails extends BoxPanel {
private final PageModelsTab pageModelTab;
private final ParameterSingleSelectionModel<String> selectedModelId;
private final ParameterSingleSelectionModel<String> selectedComponentId;
public PageModelDetails(
final PageModelsTab pageModelTab,
final ParameterSingleSelectionModel<String> selectedModelId,
final ParameterSingleSelectionModel<String> selectedComponentId) {
super(BoxPanel.VERTICAL);
this.pageModelTab = pageModelTab;
this.selectedModelId = selectedModelId;
this.selectedComponentId = selectedComponentId;
final ActionLink backLink = new ActionLink(new GlobalizedMessage(
"ui.admin.pagemodels.details.back",
AdminUiConstants.ADMIN_BUNDLE));
backLink.setClassAttr("back-link");
backLink.addActionListener(event -> {
selectedModelId.clearSelection(event.getPageState());
pageModelTab.showPageModelsTable(event.getPageState());
});
super.add(backLink);
final Label heading = new Label();
heading.setClassAttr("heading");
heading.addPrintListener(this::printHeading);
super.add(heading);
final PropertySheet propertySheet = new PropertySheet(
new PageModelPropertySheetModelBuilder(selectedModelId));
super.add(propertySheet);
final ActionLink editProperties = new ActionLink(new GlobalizedMessage(
"ui.admin.pagemodels.details.edit_properties",
AdminUiConstants.ADMIN_BUNDLE));
editProperties.addActionListener(event -> {
pageModelTab.showPageModelForm(event.getPageState());
});
final ActionLink publishLink = new ActionLink(new GlobalizedMessage(
"ui.admin.pagemodels.details.publish",
AdminUiConstants.ADMIN_BUNDLE));
publishLink.addActionListener(this::publishPageModel);
final BoxPanel actionsPanel = new BoxPanel(BoxPanel.HORIZONTAL);
actionsPanel.add(editProperties);
actionsPanel.add(publishLink);
super.add(actionsPanel);
final AddComponentForm addComponentForm = new AddComponentForm(
pageModelTab);
super.add(addComponentForm);
final ComponentsTable componentsTable
= new ComponentsTable(
pageModelTab, selectedModelId, selectedComponentId);
super.add(componentsTable);
}
private void printHeading(final PrintEvent event) {
final PageState state = event.getPageState();
final Label target = (Label) event.getTarget();
final PageModelRepository pageModelRepo = CdiUtil
.createCdiUtil()
.findBean(PageModelRepository.class);
final PageModel pageModel = pageModelRepo
.findById(Long.parseLong(selectedModelId.getSelectedKey(state)))
.get();
target.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.details.heading",
AdminUiConstants.ADMIN_BUNDLE,
new String[]{pageModel.getName()}));
}
private void publishPageModel(final ActionEvent event) {
final PageState state = event.getPageState();
final String selectedPageModelIdStr = selectedModelId
.getSelectedKey(state);
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelManager pageModelManager = cdiUtil
.findBean(PageModelManager.class);
final PageModelRepository pageModelRepo = cdiUtil
.findBean(PageModelRepository.class);
final PageModel pageModel = pageModelRepo
.findById(Long.parseLong(selectedPageModelIdStr))
.orElseThrow(() -> new UnexpectedErrorException(String
.format("No PageModel with ID %s in the database.",
selectedPageModelIdStr)));
final PageModel draftModel = pageModelManager.getDraftVersion(pageModel);
pageModelManager.publish(draftModel);
}
/**
* Form for selecting the type of {@link ComponentModel} to add to the
* {@link PageModel}.
*/
private class AddComponentForm extends Form {
private final PageModelsTab pageModelTab;
private final SingleSelect selectType;
public AddComponentForm(final PageModelsTab pageModelTab) {
super("pagemodel_add_component_form",
new BoxPanel(BoxPanel.HORIZONTAL));
this.pageModelTab = pageModelTab;
final Label formLabel = new Label(new GlobalizedMessage(
"ui.admin.pagemodels.add_new_component",
AdminUiConstants.ADMIN_BUNDLE));
super.add(formLabel);
selectType = new SingleSelect("select_component_type");
selectType.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.add_new_component.type",
AdminUiConstants.ADMIN_BUNDLE));
try {
selectType
.addPrintListener(new ComponentModelSelectPrintListener());
} catch (TooManyListenersException ex) {
throw new UnexpectedErrorException(ex);
}
super.add(selectType);
final Submit submit = new Submit(new GlobalizedMessage(
"ui.admin.pagemodels.add_new_component.submit",
AdminUiConstants.ADMIN_BUNDLE));
super.add(submit);
super.addProcessListener(this::process);
}
public void process(final FormSectionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
final String type = (String) selectType.getValue(state);
final Class<? extends ComponentModel> clazz = getClass(type);
pageModelTab.showNewComponentForm(state, clazz);
}
@SuppressWarnings("unchecked")
private Class<? extends ComponentModel> getClass(final String type) {
try {
return (Class<? extends ComponentModel>) Class.forName(type);
} catch (ClassNotFoundException ex) {
throw new UnexpectedErrorException(ex);
}
}
}
/**
* {@link PrintListener} implementation for the select box in the
* {@link AddComponentForm}.
*/
private class ComponentModelSelectPrintListener implements PrintListener {
@Override
public void prepare(final PrintEvent event) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final ComponentModels componentModels = cdiUtil
.findBean(ComponentModels.class);
final SingleSelect target = (SingleSelect) event.getTarget();
target.clearOptions();
final List<PageModelComponentModel> models = componentModels
.findAvailableComponentModels();
for (final PageModelComponentModel model : models) {
target.addOption(createOption(model));
}
}
private Option createOption(final PageModelComponentModel model) {
final GlobalizedMessage title = new GlobalizedMessage(
model.titleKey(), model.descBundle());
return new Option(model.modelClass().getName(),
new Label(title));
}
}
}

View File

@ -1,374 +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.ui.admin.pagemodels;
import com.arsdigita.bebop.Form;
import com.arsdigita.bebop.FormData;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.ParameterSingleSelectionModel;
import com.arsdigita.bebop.SaveCancelSection;
import com.arsdigita.bebop.Text;
import com.arsdigita.bebop.event.FormInitListener;
import com.arsdigita.bebop.event.FormProcessListener;
import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.bebop.event.FormValidationListener;
import com.arsdigita.bebop.form.Option;
import com.arsdigita.bebop.form.SingleSelect;
import com.arsdigita.bebop.form.TextArea;
import com.arsdigita.bebop.form.TextField;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.kernel.KernelConfig;
import com.arsdigita.ui.admin.AdminUiConstants;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.configuration.ConfigurationManager;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.pagemodel.PageModel;
import org.libreccm.pagemodel.PageModelManager;
import org.libreccm.pagemodel.PageModelRepository;
import org.libreccm.web.ApplicationRepository;
import org.libreccm.web.CcmApplication;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.TooManyListenersException;
/**
* Form for creating a new {@link PageModel}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class PageModelForm extends Form {
private static final String MODEL_APPLICATION = "application";
private static final String MODEL_NAME = "model_name";
private static final String MODEL_TITLE = "model_title";
private static final String MODEL_DESC = "model_desc";
private final PageModelsTab pageModelTab;
private final ParameterSingleSelectionModel<String> selectedModelId;
private final TextField nameField;
private final TextField titleField;
private final TextArea descArea;
private final SingleSelect applicationSelect;
private final SaveCancelSection saveCancelSection;
public PageModelForm(
final PageModelsTab pageModelTab,
final ParameterSingleSelectionModel<String> selectedModelId) {
super("pagemodelsform");
this.pageModelTab = pageModelTab;
this.selectedModelId = selectedModelId;
final Label heading = new Label(event -> {
final PageState state = event.getPageState();
final Label target = (Label) event.getTarget();
final String selectedModelIdStr = selectedModelId
.getSelectedKey(state);
if (selectedModelIdStr == null || selectedModelIdStr.isEmpty()) {
target.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.create_new",
AdminUiConstants.ADMIN_BUNDLE));
} else {
target.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.edit",
AdminUiConstants.ADMIN_BUNDLE));
}
});
heading.setClassAttr("heading");
super.add(heading);
nameField = new TextField(MODEL_NAME);
nameField.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.name",
AdminUiConstants.ADMIN_BUNDLE));
super.add(nameField);
titleField = new TextField(MODEL_TITLE);
titleField.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.title",
AdminUiConstants.ADMIN_BUNDLE));
super.add(titleField);
descArea = new TextArea(MODEL_DESC);
descArea.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.desc",
AdminUiConstants.ADMIN_BUNDLE));
super.add(descArea);
applicationSelect = new SingleSelect(MODEL_APPLICATION);
applicationSelect.setLabel(new GlobalizedMessage(
"ui.admin.pagemodels.application",
AdminUiConstants.ADMIN_BUNDLE));
try {
applicationSelect.addPrintListener(event -> {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final ApplicationRepository applicationRepo = cdiUtil
.findBean(ApplicationRepository.class);
final SingleSelect target = (SingleSelect) event.getTarget();
target.clearOptions();
final List<CcmApplication> applications = applicationRepo
.findAll();
applications.sort((app1, app2) -> {
return app1.getPrimaryUrl().compareTo(app2.getPrimaryUrl());
});
for (final CcmApplication app : applications) {
target.addOption(new Option(app.getPrimaryUrl(),
new Text(app.getPrimaryUrl())));
}
});
} catch (TooManyListenersException ex) {
throw new UnexpectedErrorException(ex);
}
super.add(applicationSelect);
saveCancelSection = new SaveCancelSection();
super.add(saveCancelSection);
super.addValidationListener(new ValidationListener());
super.addInitListener(new InitListener());
super.addProcessListener(new ProcessListener());
}
private class ValidationListener implements FormValidationListener {
@Override
public void validate(final FormSectionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
if (saveCancelSection.getSaveButton().isSelected(state)) {
final FormData data = event.getFormData();
final String nameValue = data.getString(MODEL_NAME);
final String titleValue = data.getString(MODEL_TITLE);
final String appValue = data.getString(MODEL_APPLICATION);
final String selectedModelIdStr = selectedModelId
.getSelectedKey(state);
final boolean modelEditedOrNew;
if (selectedModelIdStr == null || selectedModelIdStr.isEmpty()) {
modelEditedOrNew = true;
} else {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final ConfigurationManager confManager = cdiUtil
.findBean(ConfigurationManager.class);
final PageModelRepository pageModelRepo = cdiUtil
.findBean(PageModelRepository.class);
final PageModel pageModel = pageModelRepo
.findById(Long.parseLong(selectedModelIdStr))
.orElseThrow(() -> new IllegalArgumentException(String
.format("No PageModel with ID %s in the database.",
selectedModelIdStr)));
final KernelConfig kernelConfig = confManager
.findConfiguration(KernelConfig.class);
final boolean nameEdited = !pageModel
.getName()
.equals(nameValue);
final boolean titleEdited = !pageModel
.getTitle()
.getValue(kernelConfig.getDefaultLocale())
.equals(titleValue);
final boolean appEdited = !pageModel
.getApplication()
.getPrimaryUrl()
.equals(appValue);
modelEditedOrNew = nameEdited || titleEdited || appEdited;
}
if (modelEditedOrNew) {
if (nameValue == null
|| nameValue.isEmpty()
|| nameValue.matches("\\s*")) {
data.addError(MODEL_NAME,
new GlobalizedMessage(
"ui.admin.pagemodels.name.error.empty",
AdminUiConstants.ADMIN_BUNDLE));
}
if (titleValue == null
|| titleValue.isEmpty()
|| titleValue.matches("\\s*")) {
data.addError(MODEL_TITLE,
new GlobalizedMessage(
"ui.admin.pagemodels.title.error.empty",
AdminUiConstants.ADMIN_BUNDLE));
}
if (appValue == null
|| appValue.isEmpty()
|| appValue.matches("\\s*")) {
data.addError(MODEL_TITLE,
new GlobalizedMessage(
"ui.admin.pagemodels.application.error.empty",
AdminUiConstants.ADMIN_BUNDLE));
} else {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final ApplicationRepository appRepo = cdiUtil
.findBean(ApplicationRepository.class);
final Optional<CcmApplication> application = appRepo
.retrieveApplicationForPath(appValue);
if (!application.isPresent()) {
data.addError(MODEL_TITLE,
new GlobalizedMessage(
"ui.admin.pagemodels.application.error.invalid",
AdminUiConstants.ADMIN_BUNDLE));
}
}
}
}
}
}
private class InitListener implements FormInitListener {
@Override
public void init(final FormSectionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
final String selectedModelIdStr = selectedModelId
.getSelectedKey(state);
if (selectedModelIdStr != null && !selectedModelIdStr.isEmpty()) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelRepository pageModelRepo = cdiUtil
.findBean(PageModelRepository.class);
final ConfigurationManager confManager = cdiUtil
.findBean(ConfigurationManager.class);
final KernelConfig kernelConfig = confManager
.findConfiguration(KernelConfig.class);
final Locale defaultLocale = kernelConfig.getDefaultLocale();
final PageModel pageModel = pageModelRepo
.findById(Long.parseLong(selectedModelIdStr))
.orElseThrow(() -> new IllegalArgumentException(String
.format("No PageModel with ID %s in the database.",
selectedModelIdStr)));
nameField.setValue(state, pageModel.getName());
titleField.setValue(state,
pageModel.getTitle().getValue(defaultLocale));
descArea
.setValue(state,
pageModel.getDescription().getValue(defaultLocale));
applicationSelect
.setValue(state,
pageModel.getApplication().getPrimaryUrl());
}
}
}
private class ProcessListener implements FormProcessListener {
@Override
public void process(final FormSectionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
if (saveCancelSection.getSaveButton().isSelected(state)) {
final FormData data = event.getFormData();
final String nameValue = data.getString(MODEL_NAME);
final String titleValue = data.getString(MODEL_TITLE);
final String descValue = data.getString(MODEL_DESC);
final String appValue = data.getString(MODEL_APPLICATION);
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelRepository pageModelRepo = cdiUtil
.findBean(PageModelRepository.class);
final PageModelManager pageModelManager = cdiUtil
.findBean(PageModelManager.class);
final ConfigurationManager confManager = cdiUtil
.findBean(ConfigurationManager.class);
final ApplicationRepository appRepo = cdiUtil
.findBean(ApplicationRepository.class);
final KernelConfig kernelConfig = confManager
.findConfiguration(KernelConfig.class);
final Locale defaultLocale = kernelConfig.getDefaultLocale();
final String selectedModelIdStr = selectedModelId
.getSelectedKey(state);
final CcmApplication application = appRepo
.retrieveApplicationForPath(appValue)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No CcmApplication with primary URL \"%s\" in the "
+ "database.",
appValue)));
final PageModel pageModel;
if (selectedModelIdStr == null || selectedModelIdStr.isEmpty()) {
pageModel = pageModelManager.createPageModel(nameValue,
application);
} else {
pageModel = pageModelRepo
.findById(Long.parseLong(selectedModelIdStr))
.orElseThrow(() -> new IllegalArgumentException(String
.format("No PageModel with ID %s in the database.",
selectedModelIdStr)));
}
pageModel.setName(nameValue);
pageModel.getTitle().putValue(defaultLocale, titleValue);
pageModel.getDescription().putValue(defaultLocale, descValue);
pageModel.setApplication(application);
pageModelRepo.save(pageModel);
}
pageModelTab.showPageModelsTable(state);
}
}
}

View File

@ -1,115 +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.ui.admin.pagemodels;
import com.arsdigita.bebop.PropertySheetModel;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.kernel.KernelConfig;
import com.arsdigita.ui.admin.AdminUiConstants;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.configuration.ConfigurationManager;
import org.libreccm.pagemodel.PageModel;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;
/**
* Implementation of {@link PropertySheetModel} for the the property sheet used
* in {@link PageModelDetails} for displaying the basic properties of a
* {@link PageModel}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class PageModelPropertySheetModel implements PropertySheetModel {
private static enum PageModelProperty {
MODEL_NAME,
MODEL_TITLE,
MODEL_APPLICATION,
MODEL_DESC
}
private final PageModel pageModel;
private final Iterator<PageModelProperty> propertyIterator;
private PageModelProperty currentProperty;
public PageModelPropertySheetModel(final PageModel pageModel) {
this.pageModel = pageModel;
propertyIterator = Arrays
.asList(PageModelProperty.values())
.iterator();
}
@Override
public boolean nextRow() {
if (pageModel == null) {
return false;
}
if (propertyIterator.hasNext()) {
currentProperty = propertyIterator.next();
return true;
} else {
return false;
}
}
@Override
public String getLabel() {
return currentProperty.toString();
}
@Override
public GlobalizedMessage getGlobalizedLabel() {
final String key = String
.join("",
"ui.admin.pagemodels.details.",
currentProperty.toString().toLowerCase());
return new GlobalizedMessage(key, AdminUiConstants.ADMIN_BUNDLE);
}
@Override
public String getValue() {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final ConfigurationManager confManager = cdiUtil
.findBean(ConfigurationManager.class);
final KernelConfig kernelConfig = confManager
.findConfiguration(KernelConfig.class);
final Locale defaultLocale = kernelConfig.getDefaultLocale();
switch (currentProperty) {
case MODEL_APPLICATION:
return pageModel.getApplication().getPrimaryUrl();
case MODEL_DESC:
return pageModel.getDescription().getValue(defaultLocale);
case MODEL_NAME:
return pageModel.getName();
case MODEL_TITLE:
return pageModel.getTitle().getValue(defaultLocale);
default:
return "";
}
}
}

View File

@ -1,70 +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.ui.admin.pagemodels;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.ParameterSingleSelectionModel;
import com.arsdigita.bebop.PropertySheet;
import com.arsdigita.bebop.PropertySheetModel;
import com.arsdigita.bebop.PropertySheetModelBuilder;
import com.arsdigita.util.LockableImpl;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.pagemodel.PageModel;
import org.libreccm.pagemodel.PageModelRepository;
/**
* Implementation of {@link PropertySheetModelBuilder} for the the property
* sheet used in {@link PageModelDetails} for displaying the basic properties of
* a {@link PageModel}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class PageModelPropertySheetModelBuilder
extends LockableImpl
implements com.arsdigita.bebop.PropertySheetModelBuilder {
private final ParameterSingleSelectionModel<String> selectedModelId;
public PageModelPropertySheetModelBuilder(
final ParameterSingleSelectionModel<String> selectedModelId) {
this.selectedModelId = selectedModelId;
}
@Override
public PropertySheetModel makeModel(final PropertySheet sheet,
final PageState state) {
final String selectedModelIdStr = selectedModelId.getSelectedKey(
state);
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelRepository pageModelRepo = cdiUtil
.findBean(PageModelRepository.class);
final PageModel pageModel = pageModelRepo
.findById(Long.parseLong(selectedModelIdStr))
.orElseThrow(() -> new IllegalArgumentException(String
.format("No PageModel with ID %s in the database.",
selectedModelIdStr)));
return new PageModelPropertySheetModel(pageModel);
}
}

View File

@ -1,305 +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.ui.admin.pagemodels;
import com.arsdigita.bebop.Form;
import org.libreccm.l10n.GlobalizationHelper;
import org.libreccm.pagemodel.ComponentModel;
import org.libreccm.pagemodel.ComponentModelRepository;
import org.libreccm.pagemodel.ComponentModels;
import org.libreccm.pagemodel.PageModel;
import org.libreccm.pagemodel.PageModelComponentModel;
import org.libreccm.pagemodel.PageModelManager;
import org.libreccm.pagemodel.PageModelRepository;
import org.libreccm.web.ApplicationRepository;
import org.libreccm.web.CcmApplication;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
/**
* CDI bean encapsulating some actions for the components of the
* {@link PageModelTab}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
class PageModelsController implements Serializable {
private static final long serialVersionUID = -5105462163244688201L;
@Inject
private ApplicationRepository applicationRepo;
@Inject
private ComponentModelRepository componentModelRepo;
@Inject
private ComponentModels componentModels;
@Inject
private GlobalizationHelper globalizationHelper;
@Inject
private PageModelManager pageModelManager;
@Inject
private PageModelRepository pageModelRepo;
/**
* Loads the data for rows of the table of page models. Takes care of
* loading all required lazily fetched properties.
*
* @return
*/
@Transactional(Transactional.TxType.REQUIRED)
protected List<PageModelsTableRow> findPageModels() {
return pageModelRepo
.findAllDraftModels()
.stream()
.map(this::buildRow)
.sorted()
.collect(Collectors.toList());
}
/**
* Checks if the name of a {@link PageModel} is unique within the page
* models for an application.
*
* @param applicationId The ID of the application.
* @param name The name to check.
*
* @return {@code true} if the name is unique, {@code false} otherwise.
*/
@Transactional(Transactional.TxType.REQUIRED)
protected boolean isUnique(final long applicationId,
final String name) {
final CcmApplication application = applicationRepo
.findById(applicationId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No CcmApplication with ID %d in the database.",
applicationId)));
return !pageModelRepo
.findLiveByApplicationAndName(application, name)
.isPresent();
}
/**
* Deletes a {@link PageModel}.
*
* @param pageModelId The ID of the {@link PageModel} to delete.
*/
@Transactional(Transactional.TxType.REQUIRED)
protected void deletePageModel(final long pageModelId) {
final PageModel model = pageModelRepo
.findById(pageModelId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No PageModel with ID %d in the database.",
pageModelId)));
pageModelRepo.delete(model);
}
/**
* Helper method for building the data object containing all data required
* for one row the tables of {@link PageModel}s.
*
* @param model The {@link PageModel} which is represented by the row.
*
* @return The {@link PageModelsTableRow} containing all data about the
* provided {@link PageModel} required to create the row about the
* {@link PageModel} in the table of {@link PageModel}s.
*/
private PageModelsTableRow buildRow(final PageModel model) {
final PageModelsTableRow row = new PageModelsTableRow();
row.setModelId(model.getPageModelId());
row.setName(model.getName());
row.setTitle(globalizationHelper
.getValueFromLocalizedString(model.getTitle()));
row.setDescription(globalizationHelper
.getValueFromLocalizedString(model.getDescription()));
row.setApplicationName(model.getApplication().getPrimaryUrl());
row.setLive(pageModelManager.isLive(model));
return row;
}
/**
* Retrieves the localised title of the {@link ComponentModel}.
*
* @param clazz The class of the {@link ComponentModel}.
*
* @return The localised title of the {@link ComponentModel}.
*/
protected String getComponentModelTitle(
final Class<? extends ComponentModel> clazz) {
final Optional<PageModelComponentModel> info = componentModels
.getComponentModelInfo(clazz);
if (info.isPresent()) {
final ResourceBundle bundle = ResourceBundle
.getBundle(info.get().descBundle());
return bundle.getString(info.get().titleKey());
} else {
return clazz.getName();
}
}
/**
* Retrieves the form for editing a {@link ComponentModel}.
*
* @param componentModelId The ID of the {@link ComponentModel} instance.
*
* @return The form for editing the properties of the {@link ComponentModel}
* instance.
*/
protected Class<? extends Form> getComponentModelForm(
final long componentModelId) {
final ComponentModel componentModel = componentModelRepo
.findById(componentModelId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No ComponentModel with ID %d in the database.",
componentModelId)));
final Class<? extends ComponentModel> clazz = componentModel
.getClass();
return getComponentModelForm(clazz);
}
/**
* Retrieves the form for creating/editing an instance of
* {@link ComponentModel}.
*
* @param clazz The class of the {@link ComponentModel}.
*
* @return The form for the {@link ComponentModel}.
*/
protected Class<? extends Form> getComponentModelForm(
final Class<? extends ComponentModel> clazz) {
Objects.requireNonNull(clazz);
final Optional<PageModelComponentModel> info = componentModels
.getComponentModelInfo(clazz);
return info
.orElseThrow(() -> new IllegalArgumentException(String
.format("No data about ComponentModel class \"%s\" available.",
clazz.getName())))
.editor();
}
/**
* Retrieves a list of all {@link ComponentModel} instances assigned to a
* {@link PageModel}.
*
* @param pageModelId The ID of the {@link PageModel}.
*
* @return A list of all {@link ComponentModel}s assigned to the
* {@link PageModel}.
*/
@Transactional(Transactional.TxType.REQUIRED)
protected List<ComponentModel> retrieveComponents(final long pageModelId) {
final PageModel model = pageModelRepo
.findById(pageModelId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No PageModel with ID %d in the database.",
pageModelId)));
final List<ComponentModel> components = new ArrayList<>();
// ToDo
// for (final ComponentModel component : model.getComponents()) {
// components.add(component);
// }
return components;
}
/**
* Creates an instance of a {@link ComponentModel} and adds the instance to
* a {@link PageModel}.
*
* @param pageModelId The ID of the {@link PageModel} to which the new
* {@link ComponentModel} is assigned.
* @param componentModel The new {@link ComponentModel}.
*/
@Transactional(Transactional.TxType.REQUIRED)
protected void addComponentModel(final long pageModelId,
final ComponentModel componentModel) {
final PageModel model = pageModelRepo
.findById(pageModelId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No PageModel with ID %d in the database.",
pageModelId)));
// ToDo
// pageModelManager.addComponentModel(model, componentModel);
}
/**
* Removes a {@link ComponentModel} instance from a {@link PageModel}. This
* deletes the component model.
*
* @param pageModelId The ID of the {@link PageModel} from which the
* {@link ComponentModel} is removed.
* @param componentModelId The ID of the {@link ComponentModel} to remove.
*/
@Transactional(Transactional.TxType.REQUIRED)
protected void removeComponentModel(final long pageModelId,
final long componentModelId) {
final PageModel model = pageModelRepo
.findById(pageModelId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No PageModel with ID %d in the database.",
pageModelId)));
final ComponentModel componentModel = componentModelRepo
.findById(componentModelId)
.orElseThrow(() -> new IllegalArgumentException(String
.format("No ComponentModel with ID %d in the database.",
componentModelId)));
// ToDo
// pageModelManager.removeComponentModel(model, componentModel);
}
}

View File

@ -1,187 +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.ui.admin.pagemodels;
import com.arsdigita.bebop.ActionLink;
import com.arsdigita.bebop.BoxPanel;
import com.arsdigita.bebop.Form;
import com.arsdigita.bebop.MetaForm;
import com.arsdigita.bebop.Page;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.ParameterSingleSelectionModel;
import com.arsdigita.bebop.parameters.StringParameter;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.toolbox.ui.LayoutPanel;
import com.arsdigita.ui.admin.AdminUiConstants;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.pagemodel.ComponentModel;
import java.lang.reflect.InvocationTargetException;
/**
* Tab for {@code /ccm/admin} for managing {@link PageModel}s.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class PageModelsTab extends LayoutPanel {
private final ParameterSingleSelectionModel<String> selectedModelId;
private final ParameterSingleSelectionModel<String> selectedComponentId;
private final ActionLink addNewModel;
private final PageModelsTable pageModelsTable;
private final PageModelDetails pageModelDetails;
private final PageModelForm pageModelForm;
private final MetaForm componentForm;
private Class<? extends ComponentModel> componentModelClass;
public PageModelsTab() {
super();
super.setClassAttr("sidebarNavPanel");
final BoxPanel left = new BoxPanel(BoxPanel.VERTICAL);
selectedModelId = new ParameterSingleSelectionModel<>(
new StringParameter("selected_pagemodel_id"));
selectedComponentId = new ParameterSingleSelectionModel<>(
new StringParameter(("selected_pagemodel_component_id")));
pageModelsTable = new PageModelsTable(this, selectedModelId);
pageModelDetails = new PageModelDetails(this,
selectedModelId,
selectedComponentId);
pageModelForm = new PageModelForm(this, selectedModelId);
addNewModel = new ActionLink(new GlobalizedMessage(
"ui.admin.pagemodels.add_new_pagemodel_link",
AdminUiConstants.ADMIN_BUNDLE));
addNewModel.addActionListener(event -> {
showPageModelForm(event.getPageState());
});
componentForm = new MetaForm("componentsForm") {
@Override
public Form buildForm(final PageState state) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelsController controller = cdiUtil
.findBean(PageModelsController.class);
try {
final Class<? extends Form> formClass;
if (selectedComponentId.getSelectedKey(state) == null
|| selectedComponentId.getSelectedKey(state)
.isEmpty()) {
formClass = controller
.getComponentModelForm(componentModelClass);
} else {
formClass = controller
.getComponentModelForm(Long
.parseLong(selectedComponentId
.getSelectedKey(state)));
}
return formClass
.getDeclaredConstructor(PageModelsTab.class,
ParameterSingleSelectionModel.class,
ParameterSingleSelectionModel.class)
.newInstance(PageModelsTab.this,
selectedModelId,
selectedComponentId);
} catch (InstantiationException
| InvocationTargetException
| IllegalAccessException
| NoSuchMethodException ex) {
throw new UnexpectedErrorException(ex);
}
}
};
final BoxPanel right = new BoxPanel(BoxPanel.VERTICAL);
right.add(addNewModel);
right.add(pageModelsTable);
right.add(pageModelDetails);
right.add(pageModelForm);
right.add(componentForm);
setLeft(left);
setRight(right);
}
@Override
public void register(final Page page) {
super.register(page);
page.addGlobalStateParam(selectedModelId.getStateParameter());
page.addGlobalStateParam(selectedComponentId.getStateParameter());
page.setVisibleDefault(addNewModel, true);
page.setVisibleDefault(pageModelsTable, true);
page.setVisibleDefault(pageModelDetails, false);
page.setVisibleDefault(pageModelForm, false);
page.setVisibleDefault(componentForm, false);
}
protected void showNewComponentForm(
final PageState state,
final Class<? extends ComponentModel> componentModelClass) {
this.componentModelClass = componentModelClass;
showComponentForm(state);
}
protected void showComponentForm(final PageState state) {
addNewModel.setVisible(state, false);
pageModelsTable.setVisible(state, false);
pageModelDetails.setVisible(state, false);
pageModelForm.setVisible(state, false);
componentForm.setVisible(state, true);
}
protected void showPageModelDetails(final PageState state) {
addNewModel.setVisible(state, false);
pageModelsTable.setVisible(state, false);
pageModelDetails.setVisible(state, true);
pageModelForm.setVisible(state, false);
componentForm.setVisible(state, false);
}
protected void showPageModelForm(final PageState state) {
addNewModel.setVisible(state, false);
pageModelsTable.setVisible(state, false);
pageModelDetails.setVisible(state, false);
pageModelForm.setVisible(state, true);
componentForm.setVisible(state, false);
}
protected void showPageModelsTable(final PageState state) {
addNewModel.setVisible(state, true);
pageModelsTable.setVisible(state, true);
pageModelDetails.setVisible(state, false);
pageModelForm.setVisible(state, false);
componentForm.setVisible(state, false);
}
}

View File

@ -1,255 +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.ui.admin.pagemodels;
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.ParameterSingleSelectionModel;
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.table.TableCellRenderer;
import com.arsdigita.bebop.table.TableColumn;
import com.arsdigita.bebop.table.TableColumnModel;
import com.arsdigita.bebop.table.TableModel;
import com.arsdigita.bebop.table.TableModelBuilder;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.ui.admin.AdminUiConstants;
import com.arsdigita.util.LockableImpl;
import org.libreccm.cdi.utils.CdiUtil;
import java.util.Iterator;
import java.util.List;
/**
* Table showing all available {@link PageModels}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class PageModelsTable extends Table {
public static final int COL_MODEL_APPLICATION = 0;
public static final int COL_MODEL_NAME = 1;
public static final int COL_MODEL_TITLE = 2;
public static final int COL_MODEL_DESC = 3;
public static final int COL_REMOVE = 4;
public PageModelsTable(
final PageModelsTab parent,
final ParameterSingleSelectionModel<String> selectedPageModelId) {
super();
super.setIdAttr("pageModelsTable");
super.setStyleAttr("wdith: 30em");
setEmptyView(new Label(
new GlobalizedMessage("ui.admin.pagemodels.table.empty_view",
AdminUiConstants.ADMIN_BUNDLE)));
final TableColumnModel columnModel = getColumnModel();
columnModel.add(new TableColumn(
COL_MODEL_APPLICATION,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.table.columns.headers.application",
AdminUiConstants.ADMIN_BUNDLE))
));
columnModel.add(new TableColumn(
COL_MODEL_NAME,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.table.columns.headers.name",
AdminUiConstants.ADMIN_BUNDLE))
));
columnModel.add(new TableColumn(
COL_MODEL_TITLE,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.table.columns.headers.title",
AdminUiConstants.ADMIN_BUNDLE))
));
columnModel.add(new TableColumn(
COL_MODEL_DESC,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.table.columns.headers.desc",
AdminUiConstants.ADMIN_BUNDLE))
));
columnModel.add(new TableColumn(
COL_REMOVE,
new Label(new GlobalizedMessage(
"ui.admin.pagemodels.table.columns.headers.remove",
AdminUiConstants.ADMIN_BUNDLE))
));
columnModel
.get(COL_MODEL_NAME)
.setCellRenderer(new 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((String) value);
}
});
columnModel
.get(COL_REMOVE)
.setCellRenderer(new 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 ControlLink link = new ControlLink(
(Component) value);
link.setConfirmation(new GlobalizedMessage(
"ui.admin.pagemodels.delete.confirm",
AdminUiConstants.ADMIN_BUNDLE));
return link;
}
}
});
super.addTableActionListener(new TableActionListener() {
@Override
public void cellSelected(final TableActionEvent event)
throws FormProcessException {
final PageState state = event.getPageState();
final String key = (String) event.getRowKey();
switch (event.getColumn()) {
case COL_MODEL_NAME:
selectedPageModelId.setSelectedKey(state, key);
parent.showPageModelDetails(state);
break;
case COL_REMOVE:
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelsController controller = cdiUtil
.findBean(PageModelsController.class);
controller.deletePageModel(Long.parseLong(key));
break;
default:
throw new IllegalArgumentException(
"Invalid value for column.");
}
}
@Override
public void headSelected(final TableActionEvent event) {
// Nothing
}
});
super.setModelBuilder(new PageModelsTableModelBuilder());
}
private class PageModelsTableModelBuilder
extends LockableImpl
implements TableModelBuilder {
@Override
public TableModel makeModel(final Table table,
final PageState state) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final PageModelsController controller = cdiUtil
.findBean(PageModelsController.class);
return new PageModelsTableModel(controller.findPageModels());
}
}
private class PageModelsTableModel implements TableModel {
private final Iterator<PageModelsTableRow> iterator;
private PageModelsTableRow currentRow;
public PageModelsTableModel(final List<PageModelsTableRow> rows) {
iterator = rows.iterator();
}
@Override
public int getColumnCount() {
return 5;
}
@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 COL_MODEL_APPLICATION:
return currentRow.getApplicationName();
case COL_MODEL_DESC:
return currentRow.getDescription();
case COL_MODEL_NAME:
return currentRow.getName();
case COL_MODEL_TITLE:
return currentRow.getTitle();
case COL_REMOVE:
return new Label(new GlobalizedMessage(
"ui.admin.pagemodels.table.columns.remove.label",
AdminUiConstants.ADMIN_BUNDLE));
default:
throw new IllegalArgumentException("No a valid column index");
}
}
@Override
public Object getKeyAt(final int columnIndex) {
return currentRow.getModelId();
}
}
}

View File

@ -1,111 +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.ui.admin.pagemodels;
import java.io.Serializable;
/**
* Data for one row of the {@link PageModelsTable}.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
class PageModelsTableRow implements Comparable<PageModelsTableRow>,
Serializable {
private static final long serialVersionUID = 7497498047332094014L;
private long modelId;
private String name;
private boolean live;
private String title;
private String description;
private String applicationName;
public long getModelId() {
return modelId;
}
public void setModelId(final long modelId) {
this.modelId = modelId;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public boolean isLive() {
return live;
}
public void setLive(final boolean live) {
this.live = live;
}
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;
}
public String getApplicationName() {
return applicationName;
}
public void setApplicationName(final String applicationName) {
this.applicationName = applicationName;
}
@Override
public int compareTo(final PageModelsTableRow other) {
int result;
result = applicationName.compareTo(other.getApplicationName());
if (result != 0) {
return result;
}
result = name.compareTo(other.getName());
if (result != 0) {
return result;
}
return title.compareTo(other.getTitle());
}
}