CCM NG: More JavaDoc for PageModel

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4466 8810af33-2d31-482b-a856-94f89814c4df

Former-commit-id: 5fd3824c46
pull/2/head
jensp 2016-12-06 14:07:44 +00:00
parent f0928243cb
commit eb4087ed84
3 changed files with 241 additions and 138 deletions

View File

@ -50,6 +50,10 @@ import javax.validation.constraints.NotNull;
* a page. The {@code PageModel} specifics which components are used on a page. * a page. The {@code PageModel} specifics which components are used on a page.
* *
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*
* @see PageModelRepository
* @see PageModelManager
* @see PageBuilder
*/ */
@Entity @Entity
@Table(name = "PAGE_MODELS", schema = CoreConstants.DB_SCHEMA) @Table(name = "PAGE_MODELS", schema = CoreConstants.DB_SCHEMA)
@ -76,49 +80,77 @@ import javax.validation.constraints.NotNull;
, ,
@NamedQuery( @NamedQuery(
name = "PageModel.findByApplication", name = "PageModel.findByApplication",
query = "SELECT p FROM PageModel p WHERE p.application = :application") query = "SELECT p FROM PageModel p "
+ "WHERE p.application = :application "
+ "AND p.version = org.libreccm.pagemodel.PageModelVersion.LIVE")
, ,
@NamedQuery( @NamedQuery(
name = "PageModel.countByApplication", name = "PageModel.countByApplication",
query = "SELECT COUNT(p) FROM PageModel p " query = "SELECT COUNT(p) FROM PageModel p "
+ "WHERE p.application = :application") + "WHERE p.application = :application "
+ "AND p.version = org.libreccm.pagemodel.PageModelVersion.LIVE")
, ,
@NamedQuery( @NamedQuery(
name = "PageModel.findByApplicationAndName", name = "PageModel.findByApplicationAndName",
query = "SELECT p FROM PageModel p " query = "SELECT p FROM PageModel p "
+ "WHERE p.name = :name AND p.application = :application" + "WHERE p.name = :name "
+ "AND p.application = :application "
+ "AND p.version = org.libreccm.pagemodel.PageModelVersion.LIVE"
) )
, ,
@NamedQuery( @NamedQuery(
name = "PageModel.countByApplicationAndName", name = "PageModel.countByApplicationAndName",
query = "SELECT COUNT(p) FROM PageModel p " query = "SELECT COUNT(p) FROM PageModel p "
+ "WHERE p.name = :name AND p.application = :application" + "WHERE p.name = :name "
+ "AND p.application = :application "
+ "AND p.version = org.libreccm.pagemodel.PageModelVersion.LIVE"
) )
}) })
public class PageModel implements Serializable { public class PageModel implements Serializable {
private static final long serialVersionUID = 7252512839926020978L; private static final long serialVersionUID = 7252512839926020978L;
/**
* The ID of the entity in the database.
*/
@Id @Id
@Column(name = "PAGE_MODEL_ID") @Column(name = "PAGE_MODEL_ID")
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private long pageModelId; private long pageModelId;
/**
* The UUID of this {@code PageModel}. Please note that this UUID identifies
* the dataset not the model. Therefore the draft and the live version have
* different values for this field.
*/
@Column(name = "UUID", length = 255, nullable = false) @Column(name = "UUID", length = 255, nullable = false)
@NotNull @NotNull
private String uuid; private String uuid;
/**
* The UUID of the model. Same for draft and live version.
*/
@Column(name = "MODEL_UUID", length = 255, nullable = false) @Column(name = "MODEL_UUID", length = 255, nullable = false)
@NotNull @NotNull
private String modelUuid; private String modelUuid;
/**
* The name of this {@code PageModel}. Not localised, for use in URLs.
*/
@Column(name = "NAME", length = 255) @Column(name = "NAME", length = 255)
private String name; private String name;
/**
* The version of this {@code PageModel}.
*/
@Column(name = "VERSION", length = 255, nullable = false) @Column(name = "VERSION", length = 255, nullable = false)
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
private PageModelVersion version; private PageModelVersion version;
/**
* The localised title of this {@code PageModel} (shown in the
* administration UI),
*/
@Embedded @Embedded
@AssociationOverride( @AssociationOverride(
name = "values", name = "values",
@ -129,6 +161,9 @@ public class PageModel implements Serializable {
})) }))
private LocalizedString title; private LocalizedString title;
/**
* A description of this {@code PageModel} describing its purpose.
*/
@Embedded @Embedded
@AssociationOverride( @AssociationOverride(
name = "values", name = "values",
@ -139,14 +174,23 @@ public class PageModel implements Serializable {
})) }))
private LocalizedString description; private LocalizedString description;
/**
* The application with which this {@code PageModel} is associated.
*/
@ManyToOne @ManyToOne
@JoinColumn(name = "APPLICATION_ID") @JoinColumn(name = "APPLICATION_ID")
private CcmApplication application; private CcmApplication application;
/**
* The type of this {@code PageModel}.
*/
@Column(name = "TYPE", length = 255, nullable = false) @Column(name = "TYPE", length = 255, nullable = false)
@NotNull @NotNull
private String type; private String type;
/**
* The components of the page described by this {@code PageModel}.
*/
@OneToMany(mappedBy = "pageModel") @OneToMany(mappedBy = "pageModel")
private List<ComponentModel> components; private List<ComponentModel> components;

View File

@ -24,8 +24,11 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.ResourceBundle;
/** /**
* Used in the description of {@link CcmModule} to specify which
* {@link ComponentModel}s are provided by an module.
* *
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/ */
@ -33,14 +36,44 @@ import java.lang.annotation.Target;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
public @interface PageModelComponentModel { public @interface PageModelComponentModel {
/**
* Fully qualified name of a resource bundle providing the title and
* description of the {@link ComponentModel}.
*
* @return The fully qualified name of the {@link ResourceBundle} which
* provides the title and description of the {@link ComponentModel}.
*/
String descBundle() default ""; String descBundle() default "";
/**
* Key for the title of the {@link ComponentModel} in the
* {@link ResourceBundle} specified by {@link #descBundle()}.
*
* @return The key for the title of the {@link ComponentModel}.
*/
String titleKey() default "component_model_title"; String titleKey() default "component_model_title";
/**
* Key for the description of the {@link ComponentModel} in the
* {@link ResourceBundle} specified by {@link #descBundle()}.
*
* @return The key for the description of the {@link ComponentModel}.
*/
String descKey() default "component_model_desc"; String descKey() default "component_model_desc";
/**
* The class which provides the {@link ComponentModel}.
*
* @return The class which provides the {@link ComponentModel}.
*/
Class<? extends ComponentModel> modelClass(); Class<? extends ComponentModel> modelClass();
/**
* A (Bebop) form for editing the properties of an instance of the
* {@link ComponentModel}.
*
* @return A (Bebop) form for editing the {@code ComponentModel}.
*/
Class<? extends Form> editor(); Class<? extends Form> editor();
} }

View File

@ -41,8 +41,6 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -52,6 +50,7 @@ import javax.persistence.TypedQuery;
import javax.transaction.Transactional; import javax.transaction.Transactional;
/** /**
* Provides several methods for managing {@link PageModel}s.
* *
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/ */
@ -70,6 +69,11 @@ public class PageModelManager {
private final Map<String, PageModelComponentModel> components private final Map<String, PageModelComponentModel> components
= new HashMap<>(); = new HashMap<>();
/**
* Called by CDI after an instance of this class is created. Initialises the
* {@link #components} by retrieving the data about all available
* {@link ComponentModel}s.
*/
@PostConstruct @PostConstruct
private void init() { private void init() {
final ServiceLoader<CcmModule> modules = ServiceLoader.load( final ServiceLoader<CcmModule> modules = ServiceLoader.load(
@ -90,7 +94,11 @@ public class PageModelManager {
} }
/** /**
* Creates a new {@link PageModel} for the provided application. * Creates a new {@link PageModel} for the provided application. The tries
* to retrieve the appropriate page model by using
* {@link PageModelRepository#findByApplicationAndName(org.libreccm.web.CcmApplication, java.lang.String)}.
* Please note that this method will always return the <strong>live</strong>
* version of the page model.
* *
* @param name The name of the new page model. Must be unique for the * @param name The name of the new page model. Must be unique for the
* application. * application.
@ -138,6 +146,15 @@ public class PageModelManager {
return pageModel; return pageModel;
} }
/**
* Retrieves the draft version of a {@link PageModel}. To invoke this method
* the current user needs a permission granting the
* {@link CoreConstants#PRIVILEGE_ADMIN} privilege.
*
* @param pageModel The {@link PageModel} for which the draft version is
* retrieved.
* @return The draft version of the provided {@link PageModel}.
*/
@AuthorizationRequired @AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public PageModel getDraftVersion( public PageModel getDraftVersion(
@ -156,6 +173,13 @@ public class PageModelManager {
return query.getSingleResult(); return query.getSingleResult();
} }
/**
* Checks if a {@link PageModel} has a live version.
*
* @param pageModel The {@link PageModel} to check for a live version.
* @return {@code true} if there is a live version for the provided
* {@link PageModel}, {@code false} otherwise.
*/
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public boolean isLive(final PageModel pageModel) { public boolean isLive(final PageModel pageModel) {
final TypedQuery<Boolean> query = entityManager.createNamedQuery( final TypedQuery<Boolean> query = entityManager.createNamedQuery(
@ -165,10 +189,13 @@ public class PageModelManager {
return query.getSingleResult(); return query.getSingleResult();
} }
@AuthorizationRequired /**
*
* @param pageModel
* @return
*/
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public Optional<PageModel> getLiveVersion( public Optional<PageModel> getLiveVersion(final PageModel pageModel) {
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) PageModel pageModel) {
if (isLive(pageModel)) { if (isLive(pageModel)) {
final TypedQuery<PageModel> query = entityManager.createNamedQuery( final TypedQuery<PageModel> query = entityManager.createNamedQuery(
@ -208,9 +235,8 @@ public class PageModelManager {
liveModel.setApplication(draftModel.getApplication()); liveModel.setApplication(draftModel.getApplication());
liveModel.setType(draftModel.getType()); liveModel.setType(draftModel.getType());
liveModel.clearComponents(); liveModel.clearComponents();
for(final ComponentModel draft : draftModel.getComponents()) { for (final ComponentModel draft : draftModel.getComponents()) {
final ComponentModel live = publishComponentModel(draft); final ComponentModel live = publishComponentModel(draft);
addComponentModel(liveModel, live); addComponentModel(liveModel, live);
} }
@ -235,11 +261,12 @@ public class PageModelManager {
final BeanInfo beanInfo; final BeanInfo beanInfo;
try { try {
beanInfo = Introspector.getBeanInfo(clazz); beanInfo = Introspector.getBeanInfo(clazz);
} catch(IntrospectionException ex) { } catch (IntrospectionException ex) {
throw new UncheckedWrapperException(ex); throw new UncheckedWrapperException(ex);
} }
for(final PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { for (final PropertyDescriptor propertyDescriptor : beanInfo.
getPropertyDescriptors()) {
final Class<?> propType = propertyDescriptor.getPropertyType(); final Class<?> propType = propertyDescriptor.getPropertyType();
final Method readMethod = propertyDescriptor.getReadMethod(); final Method readMethod = propertyDescriptor.getReadMethod();
final Method writeMethod = propertyDescriptor.getWriteMethod(); final Method writeMethod = propertyDescriptor.getWriteMethod();
@ -260,14 +287,14 @@ public class PageModelManager {
try { try {
source = (List<Object>) readMethod.invoke(draftModel); source = (List<Object>) readMethod.invoke(draftModel);
target = (List<Object>) readMethod.invoke(liveModel); target = (List<Object>) readMethod.invoke(liveModel);
} catch(IllegalAccessException } catch (IllegalAccessException
| IllegalArgumentException | IllegalArgumentException
| InvocationTargetException ex) { | InvocationTargetException ex) {
throw new UncheckedWrapperException(ex); throw new UncheckedWrapperException(ex);
} }
target.addAll(source); target.addAll(source);
} else if(propType != null } else if (propType != null
&& propType.isAssignableFrom(Map.class)) { && propType.isAssignableFrom(Map.class)) {
final Map<Object, Object> source; final Map<Object, Object> source;
@ -276,7 +303,7 @@ public class PageModelManager {
try { try {
source = (Map<Object, Object>) readMethod.invoke(draftModel); source = (Map<Object, Object>) readMethod.invoke(draftModel);
target = (Map<Object, Object>) readMethod.invoke(liveModel); target = (Map<Object, Object>) readMethod.invoke(liveModel);
} catch(IllegalAccessException } catch (IllegalAccessException
| IllegalArgumentException | IllegalArgumentException
| InvocationTargetException ex) { | InvocationTargetException ex) {
throw new UncheckedWrapperException(ex); throw new UncheckedWrapperException(ex);
@ -284,7 +311,7 @@ public class PageModelManager {
source.forEach((key, value) -> target.put(key, value)); source.forEach((key, value) -> target.put(key, value));
} else if(propType != null } else if (propType != null
&& propType.isAssignableFrom(Set.class)) { && propType.isAssignableFrom(Set.class)) {
final Set<Object> source; final Set<Object> source;
@ -293,7 +320,7 @@ public class PageModelManager {
try { try {
source = (Set<Object>) readMethod.invoke(draftModel); source = (Set<Object>) readMethod.invoke(draftModel);
target = (Set<Object>) readMethod.invoke(liveModel); target = (Set<Object>) readMethod.invoke(liveModel);
}catch(IllegalAccessException } catch (IllegalAccessException
| IllegalArgumentException | IllegalArgumentException
| InvocationTargetException ex) { | InvocationTargetException ex) {
throw new UncheckedWrapperException(ex); throw new UncheckedWrapperException(ex);
@ -305,7 +332,7 @@ public class PageModelManager {
try { try {
value = readMethod.invoke(draftModel); value = readMethod.invoke(draftModel);
writeMethod.invoke(liveModel, value); writeMethod.invoke(liveModel, value);
} catch(IllegalAccessException } catch (IllegalAccessException
| IllegalArgumentException | IllegalArgumentException
| InvocationTargetException ex) { | InvocationTargetException ex) {
throw new UncheckedWrapperException(ex); throw new UncheckedWrapperException(ex);
@ -325,7 +352,7 @@ public class PageModelManager {
}; };
boolean result = false; boolean result = false;
for(final String current : excluded) { for (final String current : excluded) {
if (current.equals(name)) { if (current.equals(name)) {
result = true; result = true;
break; break;
@ -359,8 +386,7 @@ public class PageModelManager {
/** /**
* Add a {@link ComponentModel} to a {@link PageModel}. * Add a {@link ComponentModel} to a {@link PageModel}.
* *
* @param pageModel The {@link PageModel} to which component model is * @param pageModel The {@link PageModel} to which component model is added.
* added.
* @param componentModel The {@link ComponentModel} to add. * @param componentModel The {@link ComponentModel} to add.
*/ */
public void addComponentModel(final PageModel pageModel, public void addComponentModel(final PageModel pageModel,