CCM NG: Creating new domains using /ccm/admin/

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4025 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2016-04-22 18:28:54 +00:00
parent b14529823b
commit 189c432378
9 changed files with 241 additions and 74 deletions

View File

@ -19,14 +19,19 @@
package com.arsdigita.ui.admin.categories; package com.arsdigita.ui.admin.categories;
import com.arsdigita.bebop.Form; import com.arsdigita.bebop.Form;
import com.arsdigita.bebop.FormData;
import com.arsdigita.bebop.Label; import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.Page;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.ParameterSingleSelectionModel; import com.arsdigita.bebop.ParameterSingleSelectionModel;
import com.arsdigita.bebop.SaveCancelSection; import com.arsdigita.bebop.SaveCancelSection;
import com.arsdigita.bebop.form.Date; import com.arsdigita.bebop.form.Date;
import com.arsdigita.bebop.form.TextField; import com.arsdigita.bebop.form.TextField;
import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.globalization.GlobalizedMessage;
import org.apache.logging.log4j.util.Strings;
import org.libreccm.categorization.Domain; import org.libreccm.categorization.Domain;
import org.libreccm.categorization.DomainManager;
import org.libreccm.categorization.DomainRepository; import org.libreccm.categorization.DomainRepository;
import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.cdi.utils.CdiUtil;
@ -42,6 +47,7 @@ public class DomainForm extends Form {
private static final String VERSION = "version"; private static final String VERSION = "version";
private static final String DOMAIN_URI = "domainUri"; private static final String DOMAIN_URI = "domainUri";
private static final String DOMAIN_KEY = "domainKey"; private static final String DOMAIN_KEY = "domainKey";
private static final String ROOT_CATEGORY_NAME = "rootCategoryName";
private final ParameterSingleSelectionModel<String> selectedDomainId; private final ParameterSingleSelectionModel<String> selectedDomainId;
@ -49,11 +55,12 @@ public class DomainForm extends Form {
private final TextField domainUri; private final TextField domainUri;
private final TextField version; private final TextField version;
private final Date released; private final Date released;
private final TextField rootCategoryName;
private final SaveCancelSection saveCancelSection; private final SaveCancelSection saveCancelSection;
public DomainForm( public DomainForm(
final CategoriesTab categoriesTab, final CategoriesTab categoriesTab,
final ParameterSingleSelectionModel<String> selectedDomainId) { final ParameterSingleSelectionModel<String> selectedDomainId) {
super("domainForm"); super("domainForm");
this.selectedDomainId = selectedDomainId; this.selectedDomainId = selectedDomainId;
@ -63,17 +70,17 @@ public class DomainForm extends Form {
final Label target = (Label) e.getTarget(); final Label target = (Label) e.getTarget();
if (selectedDomainId.getSelectedKey(state) == null) { if (selectedDomainId.getSelectedKey(state) == null) {
target.setLabel(new GlobalizedMessage( target.setLabel(new GlobalizedMessage(
"ui.admin.categories.domain_form.heading.create_new", "ui.admin.categories.domain_form.heading.create_new",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
} else { } else {
final DomainRepository domainRepository = CdiUtil. final DomainRepository domainRepository = CdiUtil.
createCdiUtil().findBean(DomainRepository.class); createCdiUtil().findBean(DomainRepository.class);
final Domain domain = domainRepository.findById(Long.parseLong( final Domain domain = domainRepository.findById(Long.parseLong(
selectedDomainId.getSelectedKey(state))); selectedDomainId.getSelectedKey(state)));
target.setLabel(new GlobalizedMessage( target.setLabel(new GlobalizedMessage(
"ui.admin.categories.domain_form.heading.edit", "ui.admin.categories.domain_form.heading.edit",
ADMIN_BUNDLE, ADMIN_BUNDLE,
new String[]{domain.getDomainKey()})); new String[]{domain.getDomainKey()}));
} }
}); });
heading.setClassAttr("heading"); heading.setClassAttr("heading");
@ -81,40 +88,139 @@ public class DomainForm extends Form {
domainKey = new TextField(DOMAIN_KEY); domainKey = new TextField(DOMAIN_KEY);
domainKey.setLabel(new GlobalizedMessage( domainKey.setLabel(new GlobalizedMessage(
"ui.admin.categories.domain_form.fields.domain_key", "ui.admin.categories.domain_form.fields.domain_key",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
add(domainKey); add(domainKey);
domainUri = new TextField(DOMAIN_URI); domainUri = new TextField(DOMAIN_URI);
domainUri.setLabel(new GlobalizedMessage( domainUri.setLabel(new GlobalizedMessage(
"ui.admin.categories.domain_form.fields.domain_uri", "ui.admin.categories.domain_form.fields.domain_uri",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
add(domainUri); add(domainUri);
version = new TextField(VERSION); version = new TextField(VERSION);
version.setLabel(new GlobalizedMessage( version.setLabel(new GlobalizedMessage(
"ui.admin.categories.domain_form.fields.version", "ui.admin.categories.domain_form.fields.version",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
add(version); add(version);
released = new Date(RELEASED); released = new Date(RELEASED);
released.setLabel(new GlobalizedMessage( released.setLabel(new GlobalizedMessage(
"ui.admin.categories.domain_form.fields.released", "ui.admin.categories.domain_form.fields.released",
ADMIN_BUNDLE)); ADMIN_BUNDLE));
add(released); add(released);
rootCategoryName = new TextField(ROOT_CATEGORY_NAME);
rootCategoryName.setLabel(new GlobalizedMessage(
"ui.admin.categories.domain_form.fields.root_category_name",
ADMIN_BUNDLE));
add(rootCategoryName);
saveCancelSection = new SaveCancelSection(); saveCancelSection = new SaveCancelSection();
add(saveCancelSection); add(saveCancelSection);
addInitListener(e -> {
final PageState state = e.getPageState();
if (selectedDomainId.getSelectedKey(state) == null) {
rootCategoryName.setVisible(state, true);
} else {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final DomainRepository domainRepository = cdiUtil.findBean(
DomainRepository.class);
final Domain domain = domainRepository.findById(Long.parseLong(
selectedDomainId.getSelectedKey(state)));
domainKey.setValue(state, domain.getDomainKey());
domainUri.setValue(state, domain.getUri());
version.setValue(state, domain.getVersion());
released.setValue(state, domain.getReleased());
rootCategoryName.setVisible(state, false);
}
});
addValidationListener(e -> { addValidationListener(e -> {
final PageState state = e.getPageState();
if (saveCancelSection.getSaveButton().isSelected(state)) {
final FormData data = e.getFormData();
final String domainKeyData = data.getString(DOMAIN_KEY);
final String versionData = data.getString(VERSION);
if (Strings.isBlank(domainKeyData)) {
data.addError(
DOMAIN_KEY,
new GlobalizedMessage(
"ui.admin.categories.domain_form.errors.domain_key_blank",
ADMIN_BUNDLE));
}
if (Strings.isBlank(versionData)) {
data.addError(
DOMAIN_KEY,
new GlobalizedMessage(
"ui.admin.categories.domain_form.errors.version_blank",
ADMIN_BUNDLE));
}
}
}); });
addProcessListener(e -> { addProcessListener(e -> {
final PageState state = e.getPageState(); final PageState state = e.getPageState();
if (saveCancelSection.getSaveButton().isSelected(state)) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final DomainRepository domainRepository = cdiUtil.findBean(
DomainRepository.class);
final DomainManager domainManager = cdiUtil.findBean(
DomainManager.class);
final FormData data = e.getFormData();
final String domainKeyData = data.getString(DOMAIN_KEY);
final String domainUriData;
if (Strings.isBlank(data.getString(DOMAIN_URI))) {
domainUriData = null;
} else {
domainUriData = data.getString(DOMAIN_URI);
}
final String versionData = data.getString(VERSION);
final java.util.Date releasedData = (java.util.Date) data.get(
RELEASED);
final String rootCategoryNameData = data.getString(
ROOT_CATEGORY_NAME);
final Domain domain;
if (selectedDomainId.getSelectedKey(state) == null) {
if (Strings.isBlank(rootCategoryNameData)) {
domain = domainManager.createDomain(domainKeyData,
domainKeyData);
} else {
domain = domainManager.createDomain(
domainKeyData, rootCategoryNameData);
}
} else {
domain = domainRepository.findById(Long.parseLong(
selectedDomainId.getSelectedKey(state)));
}
domain.setDomainKey(domainKeyData);
domain.setUri(domainUriData);
domain.setVersion(versionData);
domain.setReleased(releasedData);
domainRepository.save(domain);
}
categoriesTab.hideNewDomainForm(state); categoriesTab.hideNewDomainForm(state);
}); });
} }
@Override
public void register(final Page page) {
super.register(page);
page.setVisibleDefault(rootCategoryName, false);
}
} }

View File

@ -182,7 +182,7 @@ public class DomainsTable extends Table {
final DomainRepository domainRepository = cdiUtil.findBean( final DomainRepository domainRepository = cdiUtil.findBean(
DomainRepository.class); DomainRepository.class);
if (Strings.isBlank(filterTerm)) { if (Strings.isBlank(filterTerm)) {
domains = domainRepository.findAll(); domains = domainRepository.findAll("Domain.withOwners");
LOGGER.debug("Found {} domains in the database.", LOGGER.debug("Found {} domains in the database.",
domains.size()); domains.size());
} else { } else {

View File

@ -77,25 +77,34 @@ import javax.xml.bind.annotation.XmlRootElement;
query = "SELECT d FROM Domain d WHERE d.domainKey = :key"), query = "SELECT d FROM Domain d WHERE d.domainKey = :key"),
@NamedQuery(name = "Domain.findByUri", @NamedQuery(name = "Domain.findByUri",
query = "SELECT d FROM Domain d WHERE d.uri = :uri"), query = "SELECT d FROM Domain d WHERE d.uri = :uri"),
@NamedQuery(name = "Domain.findAll",
query = "SELECT d FROM Domain d ORDER BY d.domainKey"),
@NamedQuery( @NamedQuery(
name = "Domain.search", name = "Domain.search",
query query
= "SELECT d FROM Domain d " = "SELECT d FROM Domain d "
+ "WHERE d.domainKey LIKE CONCAT (LOWER(:term), '%') " + "WHERE d.domainKey LIKE CONCAT (LOWER(:term), '%') "
+ "OR d.uri LIKE CONCAT (LOWER(:term), '%')") + "OR d.uri LIKE CONCAT (LOWER(:term), '%') "
+ "ORDER BY d.domainKey")
}) })
@NamedEntityGraphs({ @NamedEntityGraphs({
@NamedEntityGraph( @NamedEntityGraph(
name = "Domain.allCategories", name = "Domain.allCategories",
attributeNodes = { attributeNodes = {
@NamedAttributeNode(value = "root", @NamedAttributeNode(value = "root",
subgraph = "subCategories")}, subgraph = "subCategories")},
subgraphs = { subgraphs = {
@NamedSubgraph( @NamedSubgraph(
name = "subCategories", name = "subCategories",
attributeNodes = { attributeNodes = {
@NamedAttributeNode("subCategories") @NamedAttributeNode("subCategories")
})}) })}),
@NamedEntityGraph(
name = "Domain.withOwners",
attributeNodes = {
@NamedAttributeNode(value = "owners")
}
)
}) })
@DefaultEntityGraph("Domain.allCategories") @DefaultEntityGraph("Domain.allCategories")
@XmlRootElement(name = "domain", namespace = CAT_XML_NS) @XmlRootElement(name = "domain", namespace = CAT_XML_NS)
@ -137,11 +146,11 @@ public class Domain extends CcmObject implements Serializable {
*/ */
@Embedded @Embedded
@AssociationOverride( @AssociationOverride(
name = "values", name = "values",
joinTable = @JoinTable(name = "DOMAIN_TITLES", joinTable = @JoinTable(name = "DOMAIN_TITLES",
schema = DB_SCHEMA, schema = DB_SCHEMA,
joinColumns = { joinColumns = {
@JoinColumn(name = "OBJECT_ID")})) @JoinColumn(name = "OBJECT_ID")}))
@XmlElement(name = "title", namespace = CAT_XML_NS) @XmlElement(name = "title", namespace = CAT_XML_NS)
private LocalizedString title; private LocalizedString title;
@ -150,11 +159,11 @@ public class Domain extends CcmObject implements Serializable {
*/ */
@Embedded @Embedded
@AssociationOverride( @AssociationOverride(
name = "values", name = "values",
joinTable = @JoinTable(name = "DOMAIN_DESCRIPTIONS", joinTable = @JoinTable(name = "DOMAIN_DESCRIPTIONS",
schema = DB_SCHEMA, schema = DB_SCHEMA,
joinColumns = { joinColumns = {
@JoinColumn(name = "OBJECT_ID")})) @JoinColumn(name = "OBJECT_ID")}))
@XmlElement(name = "description", namespace = CAT_XML_NS) @XmlElement(name = "description", namespace = CAT_XML_NS)
private LocalizedString description; private LocalizedString description;
@ -369,19 +378,19 @@ public class Domain extends CcmObject implements Serializable {
@Override @Override
public String toString(final String data) { public String toString(final String data) {
return String.format( return String.format(
", domainKey = \"%s\", " ", domainKey = \"%s\", "
+ "uri = \"%s\", " + "uri = \"%s\", "
+ "title = \"%s\", " + "title = \"%s\", "
+ "version = \"%s\", " + "version = \"%s\", "
+ "released = %tF %<tT, " + "released = %tF %<tT, "
+ "root = \"%s\"%s", + "root = \"%s\"%s",
domainKey, domainKey,
Objects.toString(uri), Objects.toString(uri),
Objects.toString(title), Objects.toString(title),
version, version,
released, released,
Objects.toString(root), Objects.toString(root),
data data
); );
} }

View File

@ -52,12 +52,13 @@ public class DomainManager {
* with the provided name. The domain and the root category can be further * with the provided name. The domain and the root category can be further
* customised after the creation. * customised after the creation.
* *
* @param domainKey The key (name) of the new domain. * @param domainKey The key (name) of the new domain.
* @param rootCategoryName The name of the root category of the new domain. * @param rootCategoryName The name of the root category of the new domain.
*
* @return The new domain. * @return The new domain.
*/ */
public Domain createDomain(final String domainKey, public Domain createDomain(final String domainKey,
final String rootCategoryName) { final String rootCategoryName) {
final Domain domain = new Domain(); final Domain domain = new Domain();
domain.setDomainKey(domainKey); domain.setDomainKey(domainKey);
domain.setVersion("1.0"); domain.setVersion("1.0");
@ -81,9 +82,9 @@ public class DomainManager {
* {@code Domain} the method does nothing. * {@code Domain} the method does nothing.
* *
* @param application The {@code CcmApplication} to add to the owners of the * @param application The {@code CcmApplication} to add to the owners of the
* {@code Domain}. * {@code Domain}.
* @param domain The {@code Domain} to which owners the * @param domain The {@code Domain} to which owners the
* {@code CcmApplication is added}. * {@code CcmApplication is added}.
*/ */
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public void addDomainOwner(final CcmApplication application, public void addDomainOwner(final CcmApplication application,
@ -108,9 +109,9 @@ public class DomainManager {
* {@code Domain} the method does nothing. * {@code Domain} the method does nothing.
* *
* @param application The {@code CcmApplication} to remove from the owners * @param application The {@code CcmApplication} to remove from the owners
* of the provided {@code Domain}. * of the provided {@code Domain}.
* @param domain The {@code Domain} from which owners the provided * @param domain The {@code Domain} from which owners the provided
* {@code CcmApplication} should be removed. * {@code CcmApplication} should be removed.
*/ */
public void removeDomainOwner(final CcmApplication application, public void removeDomainOwner(final CcmApplication application,
final Domain domain) { final Domain domain) {
@ -122,9 +123,10 @@ public class DomainManager {
* Determines if a {@link CcmApplication} is an owner of {@link Domain}. * Determines if a {@link CcmApplication} is an owner of {@link Domain}.
* *
* @param application The {@code CcmApplication} to test. * @param application The {@code CcmApplication} to test.
* @param domain The {@code Domain} to test. * @param domain The {@code Domain} to test.
*
* @return {@code true} if the provided {@code CcmApplication} is an owner * @return {@code true} if the provided {@code CcmApplication} is an owner
* of the provided {@code Domain}, {@code false} otherwise. * of the provided {@code Domain}, {@code false} otherwise.
*/ */
public boolean isDomainOwner(final CcmApplication application, public boolean isDomainOwner(final CcmApplication application,
final Domain domain) { final Domain domain) {

View File

@ -19,9 +19,11 @@
package org.libreccm.categorization; package org.libreccm.categorization;
import org.libreccm.core.AbstractEntityRepository; import org.libreccm.core.AbstractEntityRepository;
import org.libreccm.core.DefaultEntityGraph;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
import java.util.UUID;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
@ -51,6 +53,39 @@ public class DomainRepository extends AbstractEntityRepository<Long, Domain> {
return entity.getObjectId() == 0; return entity.getObjectId() == 0;
} }
@Override
public void initNewEntity(final Domain domain) {
domain.setUuid(UUID.randomUUID().toString());
}
@Override
public List<Domain> findAll() {
if (getEntityClass().isAnnotationPresent(DefaultEntityGraph.class)) {
return findAll(getEntityClass().getAnnotation(
DefaultEntityGraph.class).value());
} else {
final TypedQuery<Domain> query = getEntityManager()
.createNamedQuery("Domain.findAll", Domain.class);
return query.getResultList();
}
}
@Override
public List<Domain> findAll(final String entityGraphName) {
@SuppressWarnings("unchecked")
final EntityGraph<Domain> entityGraph = (EntityGraph<Domain>) entityManager.
getEntityGraph(entityGraphName);
return findAll(entityGraph);
}
@Override
public List<Domain> findAll(final EntityGraph<Domain> entityGraph) {
final TypedQuery<Domain> query = getEntityManager()
.createNamedQuery("Domain.findAll", Domain.class);
query.setHint(FETCH_GRAPH_HINT_KEY, entityGraph);
return query.getResultList();
}
/** /**
* Find the {@link Domain} identified by the provided {@code domainKey}. * Find the {@link Domain} identified by the provided {@code domainKey}.
* *
@ -93,8 +128,11 @@ public class DomainRepository extends AbstractEntityRepository<Long, Domain> {
public List<Domain> search(final String term) { public List<Domain> search(final String term) {
final TypedQuery<Domain> query = entityManager.createNamedQuery( final TypedQuery<Domain> query = entityManager.createNamedQuery(
"Domain.search", Domain.class); "Domain.search", Domain.class);
query.setParameter("term", term); query.setParameter("term", term);
final EntityGraph<?> graph = entityManager.getEntityGraph(
"Domain.withOwners");
query.setHint("javax.persistence.fetchgraph", graph);
return query.getResultList(); return query.getResultList();
} }

View File

@ -358,3 +358,6 @@ ui.admin.categories.domain_form.fields.domain_key=Key
ui.admin.categories.domain_form.fields.domain_uri=URI ui.admin.categories.domain_form.fields.domain_uri=URI
ui.admin.categories.domain_form.fields.version=Version ui.admin.categories.domain_form.fields.version=Version
ui.admin.categories.domain_form.fields.released=Released ui.admin.categories.domain_form.fields.released=Released
ui.admin.categories.domain_form.errors.domain_key_blank=The key of a domain can't be blank.
ui.admin.categories.domain_form.errors.version_blank=The version of domain can't be blank.
ui.admin.categories.domain_form.fields.root_category_name=Name of root category

View File

@ -361,3 +361,6 @@ ui.admin.categories.domain_form.fields.domain_key=Name
ui.admin.categories.domain_form.fields.domain_uri=URI ui.admin.categories.domain_form.fields.domain_uri=URI
ui.admin.categories.domain_form.fields.version=Version ui.admin.categories.domain_form.fields.version=Version
ui.admin.categories.domain_form.fields.released=Freigegeben ui.admin.categories.domain_form.fields.released=Freigegeben
ui.admin.categories.domain_form.errors.domain_key_blank=Der Key einer Domain darf nicht leer sein.
ui.admin.categories.domain_form.errors.version_blank=Die Version einer Domain darf nicht leer sein.
ui.admin.categories.domain_form.fields.root_category_name=Name der Wurzelkategorie

View File

@ -334,3 +334,6 @@ ui.admin.categories.domain_form.fields.domain_key=Key
ui.admin.categories.domain_form.fields.domain_uri=URI ui.admin.categories.domain_form.fields.domain_uri=URI
ui.admin.categories.domain_form.fields.version=Version ui.admin.categories.domain_form.fields.version=Version
ui.admin.categories.domain_form.fields.released=Released ui.admin.categories.domain_form.fields.released=Released
ui.admin.categories.domain_form.errors.domain_key_blank=The key of a domain can't be blank.
ui.admin.categories.domain_form.errors.version_blank=The version of domain can't be blank.
ui.admin.categories.domain_form.fields.root_category_name=Name of root category

View File

@ -325,3 +325,6 @@ ui.admin.categories.domain_form.fields.domain_key=Key
ui.admin.categories.domain_form.fields.domain_uri=URI ui.admin.categories.domain_form.fields.domain_uri=URI
ui.admin.categories.domain_form.fields.version=Version ui.admin.categories.domain_form.fields.version=Version
ui.admin.categories.domain_form.fields.released=Released ui.admin.categories.domain_form.fields.released=Released
ui.admin.categories.domain_form.errors.domain_key_blank=The key of a domain can't be blank.
ui.admin.categories.domain_form.errors.version_blank=The version of domain can't be blank.
ui.admin.categories.domain_form.fields.root_category_name=Name of root category