CCM NG: Additional queries for ContentType
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4225 8810af33-2d31-482b-a856-94f89814c4dfpull/2/head
parent
172b99bb0a
commit
0838a6df5f
|
|
@ -43,6 +43,12 @@ public class ContentItemManager {
|
|||
@Inject
|
||||
private ContentItemRepository contentItemRepo;
|
||||
|
||||
@Inject
|
||||
private ContentTypeRepository typeRepo;
|
||||
|
||||
@Inject
|
||||
private ContentSectionManager sectionManager;
|
||||
|
||||
/**
|
||||
* Creates a new content item in the provided content section and folder
|
||||
* with the default lifecycle and workflow.
|
||||
|
|
@ -64,7 +70,23 @@ public class ContentItemManager {
|
|||
final ContentSection section,
|
||||
final Category folder,
|
||||
final Class<T> type) {
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
final Optional<ContentType> contentType = typeRepo
|
||||
.findByContentSectionAndClass(section, type);
|
||||
|
||||
if (!contentType.isPresent()) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"ContentSection \"%s\" has no content type for \"%s\".",
|
||||
section.getLabel(),
|
||||
type.getName()));
|
||||
}
|
||||
|
||||
return createContentItem(name,
|
||||
section,
|
||||
folder,
|
||||
contentType.get().getDefaultWorkflow(),
|
||||
contentType.get().getDefaultLifecycle(),
|
||||
type);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -81,7 +103,8 @@ public class ContentItemManager {
|
|||
*
|
||||
* @param <T> The type of the 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 workflowTemplate
|
||||
* @param lifecycleDefinition
|
||||
|
|
@ -117,8 +140,9 @@ public class ContentItemManager {
|
|||
*
|
||||
* @param item The item to copy.
|
||||
* @param targetFolder The folder in which the copy is created. If the
|
||||
* target folder is the same folder as the folder of the original item an
|
||||
* index is appended to the name of the item.
|
||||
* target folder is the same folder as the folder of the
|
||||
* original item an index is appended to the name of the
|
||||
* item.
|
||||
*/
|
||||
public void copy(final ContentItem item, final Category targetFolder) {
|
||||
throw new UnsupportedOperationException();
|
||||
|
|
@ -165,9 +189,9 @@ public class ContentItemManager {
|
|||
* @param type Type of the content item.
|
||||
*
|
||||
* @return The live version of an item. If the item provided is already the
|
||||
* live version the provided item is returned, otherwise the live version is
|
||||
* returned. If there is no live version an empty {@link Optional} is
|
||||
* returned.
|
||||
* live version the provided item is returned, otherwise the live
|
||||
* version is returned. If there is no live version an empty
|
||||
* {@link Optional} is returned.
|
||||
*/
|
||||
public <T extends ContentItem> Optional<T> getLiveVersion(
|
||||
final ContentItem item,
|
||||
|
|
@ -198,10 +222,10 @@ public class ContentItemManager {
|
|||
* @param type Type of the item.
|
||||
*
|
||||
* @return The draft version of the provided content item. If the provided
|
||||
* item is the draft version the provided item is simply returned. Otherwise
|
||||
* the draft version is retrieved from the database and is returned. Each
|
||||
* content item has a draft version (otherwise something is seriously wrong
|
||||
* with the database) this method will
|
||||
* item is the draft version the provided item is simply returned.
|
||||
* Otherwise the draft version is retrieved from the database and is
|
||||
* returned. Each content item has a draft version (otherwise
|
||||
* something is seriously wrong with the database) this method will
|
||||
* <b>never</b> return {@code null}.
|
||||
*/
|
||||
public <T extends ContentItem> T getDraftVersion(final ContentItem item,
|
||||
|
|
@ -219,7 +243,9 @@ public class ContentItemManager {
|
|||
* {@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) {
|
||||
|
|
@ -243,7 +269,9 @@ public class ContentItemManager {
|
|||
* @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,
|
||||
|
|
@ -285,6 +313,7 @@ public class ContentItemManager {
|
|||
* Creates as list of the folders in which is item is placed.
|
||||
*
|
||||
* @param item
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Category> getItemFolders(final ContentItem item) {
|
||||
|
|
|
|||
|
|
@ -52,9 +52,14 @@ import javax.persistence.Table;
|
|||
@Table(name = "CONTENT_TYPES", schema = DB_SCHEMA)
|
||||
@NamedQueries({
|
||||
@NamedQuery(
|
||||
name = "ContentType.findByContenSection",
|
||||
name = "ContentType.findByContentSection",
|
||||
query = "SELECT c FROM ContentType c "
|
||||
+ "WHERE c.contentSection = :contentSection")
|
||||
+ "WHERE c.contentSection = :contentSection"),
|
||||
@NamedQuery(
|
||||
name = "ContentType.findByContentSectionAndClass",
|
||||
query = "SELECT c FROM ContentType c "
|
||||
+ "WHERE c.contentSection = :contentSection "
|
||||
+ "AND c.contentItemClass =:clazz")
|
||||
})
|
||||
public class ContentType extends CcmObject implements Serializable {
|
||||
|
||||
|
|
@ -165,7 +170,8 @@ public class ContentType extends CcmObject implements Serializable {
|
|||
return defaultLifecycle;
|
||||
}
|
||||
|
||||
protected void setDefaultLifecycle(final LifecycleDefinition defaultLifecycle) {
|
||||
protected void setDefaultLifecycle(
|
||||
final LifecycleDefinition defaultLifecycle) {
|
||||
this.defaultLifecycle = defaultLifecycle;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,11 @@ package org.librecms.contentsection;
|
|||
|
||||
import org.libreccm.core.AbstractEntityRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -40,4 +44,127 @@ public class ContentTypeRepository
|
|||
return type.getObjectId() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all {@link ContentType}s of a specific content section.
|
||||
*
|
||||
* @param section The section whose {@link ContentTyp}s are retrieved. Can't
|
||||
* be {@code null}.
|
||||
*
|
||||
* @return A list of all {@link ContentType}s of the provided section.
|
||||
*/
|
||||
public List<ContentType> findByContentSection(final ContentSection section) {
|
||||
if (section == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Parameter section of method "
|
||||
+ "ContentTypeRepo#findByContentSection("
|
||||
+ "ContentSection section) can't be null.");
|
||||
}
|
||||
|
||||
final TypedQuery<ContentType> query = getEntityManager()
|
||||
.createNamedQuery("ContentType.findByContentSection",
|
||||
ContentType.class);
|
||||
query.setParameter("contentSection", section);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a specific {@link ContentType}.
|
||||
*
|
||||
* @param section The section associated with the {@link ContentType} to
|
||||
* retrieve. Can't be {@code null}.
|
||||
* @param clazz The subclass of the {@link ContentItem} class associated
|
||||
* with the content type. Can't be {@code null}.
|
||||
*
|
||||
* @return The requested {@link ContentType} if their is a
|
||||
* {@link ContentType} for the provided class in the provided
|
||||
* section. Otherwise the returned {@link Optional} will be empty.
|
||||
*/
|
||||
public Optional<ContentType> findByContentSectionAndClass(
|
||||
final ContentSection section,
|
||||
final Class<? extends ContentItem> clazz) {
|
||||
|
||||
if (section == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Parameter section of method "
|
||||
+ "ContentTypeRepo#findByContentSectionAndClass("
|
||||
+ "ContentSection section, "
|
||||
+ "Class<? extends ContentItem> clazz) can't be null.");
|
||||
}
|
||||
if (clazz == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Parameter clazz of method "
|
||||
+ "ContentTypeRepo#findByContentSectionAndClass("
|
||||
+ "ContentSection section, "
|
||||
+ "Class<? extends ContentItem> clazz) can't be null.");
|
||||
}
|
||||
|
||||
return findByContentSectionAndClass(section, clazz.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a specific {@link ContentType}.
|
||||
*
|
||||
* @param section The section associated with the {@link ContentType} to
|
||||
* retrieve.
|
||||
* @param className The name of the subclass of the {@link ContentItem}
|
||||
* class associated with the content type. The class must
|
||||
* be a subclass of {@link ContentItem}.
|
||||
*
|
||||
* @return The requested {@link ContentType} if their is a
|
||||
* {@link ContentType} for the provided class in the provided
|
||||
* section. Otherwise the returned {@link Optional} will be empty.
|
||||
*/
|
||||
public Optional<ContentType> findByContentSectionAndClass(
|
||||
final ContentSection section,
|
||||
final String className) {
|
||||
if (section == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Parameter section of method "
|
||||
+ "ContentTypeRepo#findByContentSectionAndClass("
|
||||
+ "ContentSection section, "
|
||||
+ "String className) can't be null.");
|
||||
}
|
||||
if (className == null || className.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Parameter className of method "
|
||||
+ "ContentTypeRepo#findByContentSectionAndClass("
|
||||
+ "ContentSection section, "
|
||||
+ "String className) can't be null.");
|
||||
}
|
||||
|
||||
try {
|
||||
final Class<?> clazz = Class.forName(className);
|
||||
if (!clazz.isAssignableFrom(ContentItem.class)) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"The provided class \"%s\" is not a subclass of \"%s\".",
|
||||
className,
|
||||
ContentItem.class.getName()));
|
||||
}
|
||||
} catch (ClassNotFoundException ex) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Class \"%s\" does not exist.", className));
|
||||
}
|
||||
|
||||
final TypedQuery<ContentType> query = getEntityManager()
|
||||
.createNamedQuery("ContentType.findByContentSectionAndClass",
|
||||
ContentType.class);
|
||||
query.setParameter("contentSection", section);
|
||||
query.setParameter("clazz", className);
|
||||
|
||||
final List<ContentType> result = query.getResultList();
|
||||
if (result.isEmpty()) {
|
||||
return Optional.empty();
|
||||
} else if (result.size() > 1) {
|
||||
throw new RuntimeException(String.format(
|
||||
"More than one ContentType for section \"%s\" and type \"%s\" "
|
||||
+ "found. This is an invalid state. Check your installation"
|
||||
+ "immediatly.",
|
||||
section.getLabel(),
|
||||
className));
|
||||
} else {
|
||||
return Optional.of(result.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue