CCM NG: More classes refactored for CCM NG

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4224 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2016-08-16 12:04:56 +00:00
parent 407068e8a9
commit 172b99bb0a
5 changed files with 227 additions and 89 deletions

View File

@ -18,47 +18,41 @@
*/ */
package com.arsdigita.cms.ui.folder; package com.arsdigita.cms.ui.folder;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.SingleSelectionModel; import com.arsdigita.bebop.SingleSelectionModel;
import com.arsdigita.cms.CMS; import com.arsdigita.cms.CMS;
import com.arsdigita.cms.ItemSelectionModel; import com.arsdigita.ui.CcmObjectSelectionModel;
import org.apache.log4j.Category; import org.apache.log4j.Category;
import org.librecms.contentsection.ContentSection; import org.librecms.contentsection.ContentSection;
import java.math.BigDecimal;
/** /**
* Keeps track of the selection of an item in a folder. The objects that * Keeps track of the selection of an item in a folder. The objects that are
* are selected by this model are all subclasses of {@link * selected by this model are all subclasses of {@link
* com.arsdigita.cms.Folder}. * com.arsdigita.cms.Folder}.
* *
* @author <a href="mailto:lutter@arsdigita.com">David Lutterkort</a> * @author <a href="mailto:lutter@arsdigita.com">David Lutterkort</a>
* @version $Id$ * @version $Id$
*/ */
public class FolderSelectionModel public class FolderSelectionModel extends CcmObjectSelectionModel {
extends ItemSelectionModel {
public FolderSelectionModel(String name) { public FolderSelectionModel(String name) {
super(Category.class.getName(), super(Category.class.getName(), name);
Category.class.getName(),
name);
} }
public FolderSelectionModel(final SingleSelectionModel model) { public FolderSelectionModel(final SingleSelectionModel model) {
super(Category.class.getName(), super(Category.class.getName(), model);
Category.class.getName(),
model);
} }
public Object getSelectedKey(PageState s) { @Override
public Long getSelectedKey(final PageState state) {
// FIXME: this code will go away once parameter models support init listeners // FIXME: this code will go away once parameter models support init listeners
Object result = super.getSelectedKey(s); Long result = super.getSelectedKey(state);
if ( result == null ) { if (result == null) {
result = getRootFolderID(s); result = getRootFolderID(state);
setSelectedKey(s, result); setSelectedKey(state, result);
} }
return result; return result;
} }
@ -73,10 +67,10 @@ public class FolderSelectionModel
} }
/** /**
* Return the ID of the root folder. By default, this is the root folder * Return the ID of the root folder. By default, this is the root folder of
* of the content section in which the current request is made. If this * the content section in which the current request is made. If this model
* model is to be used outside a content section, this method has to be * is to be used outside a content section, this method has to be overriden
* overriden appropriately. * appropriately.
* *
* @param s represents the current request * @param s represents the current request
* @return the ID of the root folder * @return the ID of the root folder
@ -90,8 +84,8 @@ public class FolderSelectionModel
} }
/** /**
* Return true, since this selection model will always have * Return true, since this selection model will always have a folder
* a folder selected in it * selected in it
*/ */
public boolean isSelected(PageState s) { public boolean isSelected(PageState s) {
return true; return true;

View File

@ -20,58 +20,67 @@ package com.arsdigita.cms.ui.folder;
import com.arsdigita.bebop.List; import com.arsdigita.bebop.List;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.cms.ContentItem; import com.arsdigita.bebop.list.ListModel;
import com.arsdigita.cms.ItemCollection; import com.arsdigita.bebop.list.ListModelBuilder;
import com.arsdigita.cms.ItemSelectionModel; import com.arsdigita.cms.ItemSelectionModel;
import com.arsdigita.util.LockableImpl; import com.arsdigita.util.LockableImpl;
import org.libreccm.categorization.Category;
import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.contentsection.ContentItem;
import org.librecms.contentsection.ContentItemManager;
/** /**
* Produce a list of the items starting from the selected item's root down * Produce a list of the items starting from the selected item's root down to
* to the item itself. * the item itself.
* *
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
* @author <a href="mailto:lutter@arsdigita.com">David Lutterkort</a> * @author <a href="mailto:lutter@arsdigita.com">David Lutterkort</a>
* @version $Id: ItemPath.java 1940 2009-05-29 07:15:05Z terry $
*/ */
public class ItemPath extends List { public class ItemPath extends List {
public ItemPath(ItemSelectionModel folderSel) { public ItemPath(ItemSelectionModel folderSel) {
super(new ListModelBuilder(folderSel)); super(new ItemPathListModelBuilder(folderSel));
setAttribute("type", "item-path"); setAttribute("type", "item-path");
setSelectionModel(folderSel); setSelectionModel(folderSel);
} }
public static class ListModel public static class ItemPathListModel implements ListModel {
implements com.arsdigita.bebop.list.ListModel {
ItemCollection m_coll;
public ListModel(ContentItem i) { private final java.util.List<Category> pathFolders;
m_coll = i.getPathInfo(true); private int index = -1;
public ItemPathListModel(final ContentItem item) {
pathFolders = CdiUtil.createCdiUtil().findBean(ContentItemManager.class).getItemFolders(item);
} }
@Override
public boolean next() { public boolean next() {
return m_coll.next(); index++;
return index < pathFolders.size();
} }
public Object getElement() { public Object getElement() {
return m_coll.getName(); return pathFolders.get(index).getName();
} }
public String getKey() { public String getKey() {
return m_coll.getID().toString(); return Long.toString(pathFolders.get(index).getObjectId());
} }
} }
public static class ListModelBuilder extends LockableImpl public static class ItemPathListModelBuilder extends LockableImpl
implements com.arsdigita.bebop.list.ListModelBuilder { implements ListModelBuilder {
ItemSelectionModel m_itemSel; ItemSelectionModel m_itemSel;
public ListModelBuilder(ItemSelectionModel itemSel) { public ItemPathListModelBuilder(ItemSelectionModel itemSel) {
m_itemSel = itemSel; m_itemSel = itemSel;
} }
public com.arsdigita.bebop.list.ListModel makeModel(List l, final PageState s) { public com.arsdigita.bebop.list.ListModel makeModel(List l,
return new ListModel((ContentItem) m_itemSel.getSelectedObject(s)); final PageState s) {
return new ItemPathListModel((ContentItem) m_itemSel.
getSelectedObject(s));
} }
} }
} }

View File

@ -265,7 +265,7 @@ public class ContentItem extends CcmObject implements Serializable,
if (result.isEmpty()) { if (result.isEmpty()) {
return Optional.empty(); return Optional.empty();
} else { } else {
return Optional.of(result.get(0).getCategorizedObject()); return Optional.of(result.get(0).getCategory());
} }
} }

View File

@ -18,15 +18,20 @@
*/ */
package org.librecms.contentsection; package org.librecms.contentsection;
import java.util.ArrayList;
import java.util.Collections;
import org.libreccm.categorization.Category; import org.libreccm.categorization.Category;
import org.libreccm.workflow.WorkflowTemplate; import org.libreccm.workflow.WorkflowTemplate;
import org.librecms.lifecycle.LifecycleDefinition; import org.librecms.lifecycle.LifecycleDefinition;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import org.libreccm.categorization.Categorization;
import org.librecms.CmsConstants;
/** /**
* *
@ -46,19 +51,19 @@ public class ContentItemManager {
* {@link ContentSection#rootDocumentsFolder} of the provided content * {@link ContentSection#rootDocumentsFolder} of the provided content
* section. Otherwise an {@link IllegalArgumentException} is thrown. * section. Otherwise an {@link IllegalArgumentException} is thrown.
* *
* @param <T> The type of the content item. * @param <T> The type of the content item.
* @param name The name (URL stub) of the new content item. * @param name The name (URL stub) of the new content item.
* @param section The content section in which the item is generated. * @param section The content section in which the item is generated.
* @param folder The folder in which in the item is stored. * @param folder The folder in which in the item is stored.
* @param type The type of the new content item. * @param type The type of the new content item.
* *
* @return The new content item. * @return The new content item.
*/ */
public <T extends ContentItem> T createContentItem( public <T extends ContentItem> T createContentItem(
final String name, final String name,
final ContentSection section, final ContentSection section,
final Category folder, final Category folder,
final Class<T> type) { final Class<T> type) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -74,24 +79,23 @@ public class ContentItemManager {
* {@link WorkflowTemplate} must be defined in the provided content section. * {@link WorkflowTemplate} must be defined in the provided content section.
* Otherwise an {@link IllegalArgumentException} is thrown. * Otherwise an {@link IllegalArgumentException} is thrown.
* *
* @param <T> The type of the content item. * @param <T> The type of the content item.
* @param name The name (URL stub) of the new content item. * @param name The name (URL stub) of the new content item.
* @param section The content section in which the item is * @param section The content section in which the item is generated.
* generated. * @param folder The folder in which in the item is stored.
* @param folder The folder in which in the item is stored.
* @param workflowTemplate * @param workflowTemplate
* @param lifecycleDefinition * @param lifecycleDefinition
* @param type The type of the new content item. * @param type The type of the new content item.
* *
* @return The new content item. * @return The new content item.
*/ */
public <T extends ContentItem> T createContentItem( public <T extends ContentItem> T createContentItem(
final String name, final String name,
final ContentSection section, final ContentSection section,
final Category folder, final Category folder,
final WorkflowTemplate workflowTemplate, final WorkflowTemplate workflowTemplate,
final LifecycleDefinition lifecycleDefinition, final LifecycleDefinition lifecycleDefinition,
final Class<T> type) { final Class<T> type) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -100,7 +104,7 @@ public class ContentItemManager {
* only moves the draft version of the item. The live version is moved after * only moves the draft version of the item. The live version is moved after
* a the item is republished. * a the item is republished.
* *
* @param item The item to move. * @param item The item to move.
* @param targetFolder The folder to which the item is moved. * @param targetFolder The folder to which the item is moved.
*/ */
public void move(final ContentItem item, final Category targetFolder) { public void move(final ContentItem item, final Category targetFolder) {
@ -111,11 +115,10 @@ public class ContentItemManager {
* Creates an copy of the draft version of the item in the provided * Creates an copy of the draft version of the item in the provided
* {@code targetFolder}. * {@code targetFolder}.
* *
* @param item The item to copy. * @param item The item to copy.
* @param targetFolder The folder in which the copy is created. If the * @param targetFolder The folder in which the copy is created. If the
* target folder is the same folder as the folder of the * target folder is the same folder as the folder of the original item an
* original item an index is appended to the name of the * index is appended to the name of the item.
* item.
*/ */
public void copy(final ContentItem item, final Category targetFolder) { public void copy(final ContentItem item, final Category targetFolder) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -148,7 +151,7 @@ public class ContentItemManager {
* @param item The item * @param item The item
* *
* @return {@code true} if the content item has a live version, * @return {@code true} if the content item has a live version,
* {@code false} if not. * {@code false} if not.
*/ */
public boolean isLive(final ContentItem item) { public boolean isLive(final ContentItem item) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -157,53 +160,154 @@ public class ContentItemManager {
/** /**
* Retrieves the live version of the provided content item if any. * Retrieves the live version of the provided content item if any.
* *
* @param <T> Type of the content item. * @param <T> Type of the content item.
* @param item The item of which the live version should be retrieved. * @param item The item of which the live version should be retrieved.
* @param type Type of the content item. * @param type Type of the content item.
* *
* @return The live version of an item. If the item provided is already the * @return The live version of an item. If the item provided is already the
* live version the provided item is returned, otherwise the live * live version the provided item is returned, otherwise the live version is
* version is returned. If there is no live version an empty * returned. If there is no live version an empty {@link Optional} is
* {@link Optional} is returned. * returned.
*/ */
public <T extends ContentItem> Optional<T> getLiveVersion( public <T extends ContentItem> Optional<T> getLiveVersion(
final ContentItem item, final ContentItem item,
final Class<T> type) { final Class<T> type) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/** /**
* Retrieves the pending versions of an item if there are any. * Retrieves the pending versions of an item if there are any.
* *
* @param <T> Type of the content item to retrieve. * @param <T> Type of the content item to retrieve.
* @param item The item of which the pending versions are retrieved. * @param item The item of which the pending versions are retrieved.
* @param type Type of the content item to retrieve. * @param type Type of the content item to retrieve.
* *
* @return A list of the pending versions of the item. * @return A list of the pending versions of the item.
*/ */
public <T extends ContentItem> List<T> getPendingVersions( public <T extends ContentItem> List<T> getPendingVersions(
final ContentItem item, final ContentItem item,
final Class<T> type) { final Class<T> type) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/** /**
* Retrieves the draft version * Retrieves the draft version
* *
* @param <T> Type of the item. * @param <T> Type of the item.
* @param item The item of which the draft version is retrieved. * @param item The item of which the draft version is retrieved.
* @param type Type of the item. * @param type Type of the item.
* *
* @return The draft version of the provided content item. If the provided * @return The draft version of the provided content item. If the provided
* item is the draft version the provided item is simply returned. * item is the draft version the provided item is simply returned. Otherwise
* Otherwise the draft version is retrieved from the database and is * the draft version is retrieved from the database and is returned. Each
* returned. Each content item has a draft version (otherwise * content item has a draft version (otherwise something is seriously wrong
* something is seriously wrong with the database) this method will * with the database) this method will
* <b>never</b> return {@code null}. * <b>never</b> return {@code null}.
*/ */
public <T extends ContentItem> T getDraftVersion(final ContentItem item, public <T extends ContentItem> T getDraftVersion(final ContentItem item,
final Class<T> type) { final Class<T> type) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/**
* Return the path of an as String. The path of an item is the path of the
* folder category the item is a member of concatenated with the name of the
* item. The path is relative to the content section. For instance, the path
* of an item in the folder category
* {@code /research/computer-science/artifical-intelligence} and with the
* name {@code neural-nets} has the path
* {@code /research/computer-science/artificial-intelligence/neural-nets}.
*
* @param item The item which path is generated.
* @return The path of the content item
* @see #getItemPath(org.librecms.contentsection.ContentItem, boolean)
*/
public String getItemPath(final ContentItem item) {
return getItemPath(item, false);
}
/**
* Return the path of an as String. The path of an item is the path of the
* folder category the item is a member of concatenated with the name of the
* item. The path is relative to the content section. For instance, the path
* of an item in the folder category
* {@code /research/computer-science/artifical-intelligence} and with the
* name {@code neural-nets} has the path
* {@code /research/computer-science/artificial-intelligence/neural-nets}.
* If the parameter {@code withContentSection} is set to {@code true} the
* the path will be prefixed with the name of the content section. For
* instance if the item {@link neural-nets} is part of the content section
* {@code info}, the path including the content section would be
* {@link info:/research/computer-science/artificial-intelligence/neural-nets}.
*
* @param item The item which path is generated.
* @param withContentSection Wether to include the content section into the
* path.
* @return The path of the content item
* @see #getItemPath(org.librecms.contentsection.ContentItem, boolean)
*/
public String getItemPath(final ContentItem item,
final boolean withContentSection) {
final List<Categorization> result = item.getCategories().stream().
filter(categorization -> CmsConstants.CATEGORIZATION_TYPE_FOLDER.
equals(categorization.getType()))
.collect(Collectors.toList());
if (result.isEmpty()) {
return item.getDisplayName();
} else {
final List<String> tokens = new ArrayList<>();
tokens.add(item.getDisplayName());
Category current = result.get(0).getCategory();
tokens.add(current.getName());
while (current.getParentCategory() != null) {
current = current.getParentCategory();
tokens.add(current.getName());
}
Collections.reverse(result);
final String path = String.join("/", tokens);
if (withContentSection) {
final String sectionName = item.getContentType().
getContentSection().getDisplayName();
return String.format(
"%s/%s", sectionName, path);
} else {
return String.format("/%s", path);
}
}
}
/**
* Creates as list of the folders in which is item is placed.
*
* @param item
* @return
*/
public List<Category> getItemFolders(final ContentItem item) {
final List<Categorization> result = item.getCategories().stream().
filter(categorization -> CmsConstants.CATEGORIZATION_TYPE_FOLDER.
equals(categorization.getType()))
.collect(Collectors.toList());
final List<Category> folders = new ArrayList<>();
if (!result.isEmpty()) {
Category current = result.get(0).getCategory();
folders.add(current);
while(current.getParentCategory()!= null) {
current = current.getParentCategory();
folders.add(current);
}
Collections.reverse(folders);
return folders;
}
return folders;
}
} }

View File

@ -36,17 +36,17 @@ import org.libreccm.core.CcmObjectRepository;
* *
*/ */
public class CcmObjectSelectionModel<T extends CcmObject> public class CcmObjectSelectionModel<T extends CcmObject>
implements SingleSelectionModel<Long> { implements SingleSelectionModel<Long> {
private final Class<T> clazz; private final Class<T> clazz;
private final SingleSelectionModel<Long> model; private final SingleSelectionModel<Long> model;
public CcmObjectSelectionModel(final LongParameter parameter) { public CcmObjectSelectionModel(final LongParameter parameter) {
this(null, parameter); this("", parameter);
} }
public CcmObjectSelectionModel(final String parameterName) { public CcmObjectSelectionModel(final String parameterName) {
this(null, new LongParameter(parameterName)); this("", new LongParameter(parameterName));
} }
// public CcmObjectSelectionModel(final SingleSelectionModel<T> model ) { // public CcmObjectSelectionModel(final SingleSelectionModel<T> model ) {
@ -58,17 +58,48 @@ public class CcmObjectSelectionModel<T extends CcmObject>
this(clazz, new LongParameter(parameterName)); this(clazz, new LongParameter(parameterName));
} }
public CcmObjectSelectionModel(final String className,
final String parameterName) {
try {
clazz = (Class<T>) Class.forName(className);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
model = new ParameterSingleSelectionModel(new LongParameter(
parameterName));
}
public CcmObjectSelectionModel(final Class<T> clazz, public CcmObjectSelectionModel(final Class<T> clazz,
final LongParameter parameter) { final LongParameter parameter) {
this(clazz, new ParameterSingleSelectionModel<>(parameter)); this(clazz, new ParameterSingleSelectionModel<>(parameter));
} }
public CcmObjectSelectionModel(final String className,
final LongParameter parameter) {
try {
clazz = (Class<T>) Class.forName(className);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
model = new ParameterSingleSelectionModel<>(parameter);
}
public CcmObjectSelectionModel(final Class<T> clazz, public CcmObjectSelectionModel(final Class<T> clazz,
final SingleSelectionModel<Long> model) { final SingleSelectionModel<Long> model) {
this.clazz = clazz; this.clazz = clazz;
this.model = model; this.model = model;
} }
public CcmObjectSelectionModel(final String className,
final SingleSelectionModel<Long> model) {
try {
clazz = (Class<T>) Class.forName(className);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
this.model = model;
}
@Override @Override
public boolean isSelected(final PageState state) { public boolean isSelected(final PageState state) {
return model.isSelected(state); return model.isSelected(state);
@ -87,7 +118,7 @@ public class CcmObjectSelectionModel<T extends CcmObject>
public T getSelectedObject(final PageState state) { public T getSelectedObject(final PageState state) {
final Long key = getSelectedKey(state); final Long key = getSelectedKey(state);
final CcmObjectRepository repository = CdiUtil.createCdiUtil().findBean( final CcmObjectRepository repository = CdiUtil.createCdiUtil().findBean(
CcmObjectRepository.class); CcmObjectRepository.class);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final T object = (T) repository.findById(key); final T object = (T) repository.findById(key);
return object; return object;