ContentType Loader

createPedigree von AbstractContentTypeLoader nach ContentTypeHelperImpl verschoben.
Letztere Klasse ist dafür verantwortlich, daß die Contenttypen angelegt und in der Tabelle gespeichert werden. Daher ist hier der sehr viel bessere Ort, um die createPedigree auszuführen. Es wird dadurch nicht nur das Problem mit den GenericContentTypes erledigt, sondern auch die UDCTs werden mit den Pedigree Informationen ausgestattet (hoffentlich).

git-svn-id: https://svn.libreccm.org/ccm/trunk@617 8810af33-2d31-482b-a856-94f89814c4df
master
quasi 2010-11-17 17:49:29 +00:00
parent 8d2841c573
commit 527322cd0b
2 changed files with 129 additions and 140 deletions

View File

@ -20,7 +20,6 @@ package com.arsdigita.cms.contenttypes;
import com.arsdigita.cms.ContentSection; import com.arsdigita.cms.ContentSection;
import com.arsdigita.cms.ContentType; import com.arsdigita.cms.ContentType;
import com.arsdigita.cms.ContentTypeCollection;
import com.arsdigita.cms.ContentTypeLifecycleDefinition; import com.arsdigita.cms.ContentTypeLifecycleDefinition;
import com.arsdigita.cms.ContentTypeWorkflowTemplate; import com.arsdigita.cms.ContentTypeWorkflowTemplate;
import com.arsdigita.cms.Template; import com.arsdigita.cms.Template;
@ -45,12 +44,10 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.StringTokenizer;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
@ -113,9 +110,6 @@ public abstract class AbstractContentTypeLoader extends PackageLoader {
for (Iterator it = types.iterator(); it.hasNext();) { for (Iterator it = types.iterator(); it.hasNext();) {
final ContentType type = (ContentType) it.next(); final ContentType type = (ContentType) it.next();
// Save the ancestors for this content type
createPedigree(type);
section.addContentType(type); section.addContentType(type);
prepareSection(section, type, ld, wf); prepareSection(section, type, ld, wf);
@ -208,83 +202,4 @@ public abstract class AbstractContentTypeLoader extends PackageLoader {
template.publish(ld, new Date()); template.publish(ld, new Date());
return template; return template;
} }
/**
* Generates the pedigree for this content type
* @param type The new content type
*/
private void createPedigree(ContentType type) {
// The parent content type
ContentType parent = null;
// Get all content types
ContentTypeCollection cts = ContentType.getAllContentTypes();
// This is a brute force method, but I can't come up with something
// better atm without changing either all Loader or the xml-files.
while (cts.next()) {
ContentType ct = cts.getContentType();
try {
Class.forName(type.getClassName()).asSubclass(Class.forName(ct.
getClassName()));
} catch (Exception ex) {
// This cast is not valid so type is not a sublacss of ct
continue;
}
// Save the current ct as possible parent if we haven't found any parent yet
// or if the current ancestor list is longer than that one from the possible
// parent earlier found
if (!type.getClassName().equals(ct.getClassName())
&& (parent == null
|| (parent.getAncestors() != null
&& ct.getAncestors() != null
&& parent.getAncestors().length() < ct.getAncestors().
length()))) {
parent = ct;
}
}
// If there is a valid parent content type create the pedigree
if (parent != null && !parent.getClassName().equals(type.getClassName())) {
if (parent.getAncestors() != null) {
String parentAncestors = parent.getAncestors();
StringTokenizer strTok = new StringTokenizer(parentAncestors,
"/");
// Add parent ancestors to this content types ancestor list
// Also while we iterate through the list, we also need to add
// this content type as sibling to all entries in the ancestor list
while (strTok.hasMoreElements()) {
Object token;
token = strTok.nextElement();
s_log.error(String.format(
"Trying to convert '%s' to BigDecimal...", token));
//BigDecimal ctID = (BigDecimal) strTok.nextElement();
BigDecimal ctID = (BigDecimal) strTok.nextElement();
// Get the current content type
try {
ContentType ct = new ContentType(ctID);
ct.addSiblings(ctID);
} catch (Exception ex) {
// The db is broken. There is no content type for this ID
}
// Add parent ancestor
type.addAncestor(ctID);
}
}
// Add parent to ancestor list
type.addAncestor(parent.getID());
// Add this to parent siblings
parent.addSiblings(type.getID());
}
}
} }

View File

@ -23,6 +23,7 @@ import com.arsdigita.cms.AuthoringStep;
import com.arsdigita.cms.AuthoringStepCollection; import com.arsdigita.cms.AuthoringStepCollection;
import com.arsdigita.cms.ContentPage; import com.arsdigita.cms.ContentPage;
import com.arsdigita.cms.ContentType; import com.arsdigita.cms.ContentType;
import com.arsdigita.cms.ContentTypeCollection;
import com.arsdigita.cms.search.ContentPageMetadataProvider; import com.arsdigita.cms.search.ContentPageMetadataProvider;
import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.domain.DataObjectNotFoundException;
import com.arsdigita.persistence.SessionManager; import com.arsdigita.persistence.SessionManager;
@ -30,15 +31,14 @@ import com.arsdigita.persistence.metadata.ObjectType;
import com.arsdigita.search.MetadataProviderRegistry; import com.arsdigita.search.MetadataProviderRegistry;
import com.arsdigita.util.Assert; import com.arsdigita.util.Assert;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.StringTokenizer;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
public class ContentTypeHelperImpl implements ContentTypeHelper { public class ContentTypeHelperImpl implements ContentTypeHelper {
private static final Logger s_log
= Logger.getLogger(ContentTypeHelperImpl.class); private static final Logger s_log = Logger.getLogger(ContentTypeHelperImpl.class);
private ContentType m_type; private ContentType m_type;
private ContentType m_parent; private ContentType m_parent;
// Basic Content type properties // Basic Content type properties
private String m_label; private String m_label;
private String m_labelKey; private String m_labelKey;
@ -50,10 +50,10 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
private String m_className; private String m_className;
private String m_createComponent; private String m_createComponent;
private AuthoringKit m_kit; private AuthoringKit m_kit;
private boolean m_internal; private boolean m_internal;
public ContentTypeHelperImpl() {} public ContentTypeHelperImpl() {
}
/** /**
* @deprecated use setLabelBundle and setLabelKey * @deprecated use setLabelBundle and setLabelKey
@ -61,7 +61,7 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
public void setLabel(String label) { public void setLabel(String label) {
m_label = label; m_label = label;
} }
/** /**
* @deprecated use getLabelBundle and getLabelKey * @deprecated use getLabelBundle and getLabelKey
*/ */
@ -77,7 +77,7 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
public void setLabelBundle(String labelBundle) { public void setLabelBundle(String labelBundle) {
m_labelBundle = labelBundle; m_labelBundle = labelBundle;
} }
public String getLabelBundle() { public String getLabelBundle() {
return m_labelBundle; return m_labelBundle;
} }
@ -90,7 +90,7 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
public void setLabelKey(String labelKey) { public void setLabelKey(String labelKey) {
m_labelKey = labelKey; m_labelKey = labelKey;
} }
public String getLabelKey() { public String getLabelKey() {
return m_labelKey; return m_labelKey;
} }
@ -125,7 +125,7 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
public void setDescriptionBundle(String descriptionBundle) { public void setDescriptionBundle(String descriptionBundle) {
m_descriptionBundle = descriptionBundle; m_descriptionBundle = descriptionBundle;
} }
public String getDescriptionBundle() { public String getDescriptionBundle() {
return m_descriptionBundle; return m_descriptionBundle;
} }
@ -138,7 +138,7 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
public void setDescriptionKey(String descriptionKey) { public void setDescriptionKey(String descriptionKey) {
m_descriptionKey = descriptionKey; m_descriptionKey = descriptionKey;
} }
public String getDescriptionKey() { public String getDescriptionKey() {
return m_descriptionKey; return m_descriptionKey;
} }
@ -150,7 +150,7 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
public String getObjectType() { public String getObjectType() {
return m_objectType; return m_objectType;
} }
public void setClassName(String classname) { public void setClassName(String classname) {
m_className = classname; m_className = classname;
} }
@ -172,13 +172,14 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
Assert.exists(m_type); Assert.exists(m_type);
return m_type; return m_type;
} }
/** Doesn't do anything */ /** Doesn't do anything */
public void setName(String name) {} public void setName(String name) {
/** Doesn't do anything */ }
public void setParentType(String parentType) {}
/** Doesn't do anything */
public void setParentType(String parentType) {
}
public ContentType createType() { public ContentType createType() {
Assert.exists(m_label); Assert.exists(m_label);
@ -186,20 +187,20 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
Assert.exists(m_objectType); Assert.exists(m_objectType);
Assert.exists(m_className); Assert.exists(m_className);
try { try {
s_log.debug("making new content type"); s_log.debug("making new content type");
m_type = m_type =
ContentType.findByAssociatedObjectType(m_objectType); ContentType.findByAssociatedObjectType(m_objectType);
m_type.setLabel(m_label); m_type.setLabel(m_label);
m_type.setDescription(m_description); m_type.setDescription(m_description);
m_type.save(); m_type.save();
} catch (DataObjectNotFoundException e) { } catch (DataObjectNotFoundException e) {
s_log.debug("Looking for content type"); s_log.debug("Looking for content type");
s_log.debug("Creating ContentType Label: " + m_label s_log.debug("Creating ContentType Label: " + m_label
+ " Description: " + m_description + " Description: " + m_description
+ " className: " + m_className + " className: " + m_className
+ " AssociatedObjectType: " + m_objectType); + " AssociatedObjectType: " + m_objectType);
// this is what would need to be changed to make the // this is what would need to be changed to make the
// label and description multi-lingual. To do that, // label and description multi-lingual. To do that,
// you need to use labelKey, labelBundle, descriptionKey // you need to use labelKey, labelBundle, descriptionKey
@ -210,21 +211,24 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
m_type.setClassName(m_className); m_type.setClassName(m_className);
m_type.setAssociatedObjectType(m_objectType); m_type.setAssociatedObjectType(m_objectType);
m_type.setInternal(m_internal); m_type.setInternal(m_internal);
// create pedigree for this content type
createPedigree(m_type);
m_type.save(); m_type.save();
} }
// Turn on search indexing for this type // Turn on search indexing for this type
ObjectType type = SessionManager.getMetadataRoot() ObjectType type = SessionManager.getMetadataRoot().getObjectType(m_objectType);
.getObjectType(m_objectType); if (type.isSubtypeOf(ContentPage.BASE_DATA_OBJECT_TYPE)
if (type.isSubtypeOf(ContentPage.BASE_DATA_OBJECT_TYPE) && && !m_internal) {
!m_internal) { s_log.debug("Registering search adapter for "
s_log.debug("Registering search adapter for " + + m_objectType);
m_objectType);
MetadataProviderRegistry.registerAdapter( MetadataProviderRegistry.registerAdapter(
m_objectType, m_objectType,
new ContentPageMetadataProvider()); new ContentPageMetadataProvider());
} }
Assert.exists(m_type); Assert.exists(m_type);
return m_type; return m_type;
} }
@ -244,8 +248,8 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
AuthoringStepCollection ac = m_kit.getSteps(); AuthoringStepCollection ac = m_kit.getSteps();
while (ac.next()) { while (ac.next()) {
AuthoringStep step = ac.getAuthoringStep(); AuthoringStep step = ac.getAuthoringStep();
s_log.debug("Deleting authoringStep " s_log.debug("Deleting authoringStep "
+ step.getLabel()); + step.getLabel());
m_kit.removeStep(step); m_kit.removeStep(step);
step.delete(); step.delete();
} }
@ -265,27 +269,27 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
/** /**
* @deprecated * @deprecated
*/ */
public void addAuthoringStep(String label, public void addAuthoringStep(String label,
String description, String description,
String component, String component,
BigDecimal ordering) { BigDecimal ordering) {
addAuthoringStep(label, null, description, null, component, ordering); addAuthoringStep(label, null, description, null, component, ordering);
} }
public void addAuthoringStep(String labelKey, public void addAuthoringStep(String labelKey,
String labelBundle, String labelBundle,
String descriptionKey, String descriptionKey,
String descriptionBundle, String descriptionBundle,
String component, String component,
BigDecimal ordering) { BigDecimal ordering) {
s_log.debug("Creating AuthoringStep " s_log.debug("Creating AuthoringStep "
+ " LabelKey: " + labelKey + " LabelKey: " + labelKey
+ " LabelBundle: " + labelBundle + " LabelBundle: " + labelBundle
+ " DescriptionKey: " + descriptionKey + " DescriptionKey: " + descriptionKey
+ " DescriptionBundle: " + descriptionBundle + " DescriptionBundle: " + descriptionBundle
+ " Component " + component + " Component " + component
+ " Ordering: " + ordering); + " Ordering: " + ordering);
Assert.exists(m_kit); Assert.exists(m_kit);
Assert.exists(labelKey); Assert.exists(labelKey);
Assert.exists(descriptionKey); Assert.exists(descriptionKey);
@ -293,8 +297,8 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
Assert.exists(ordering); Assert.exists(ordering);
m_kit.createStep(labelKey, labelBundle, m_kit.createStep(labelKey, labelBundle,
descriptionKey, descriptionBundle, descriptionKey, descriptionBundle,
component, ordering); component, ordering);
m_kit.save(); m_kit.save();
m_type.save(); m_type.save();
} }
@ -304,4 +308,74 @@ public class ContentTypeHelperImpl implements ContentTypeHelper {
m_type.save(); m_type.save();
} }
/**
* Generates the pedigree for this content type
* @param type The new content type
*/
private void createPedigree(ContentType type) {
// The parent content type
ContentType parent = null;
// Get all content types
ContentTypeCollection cts = ContentType.getAllContentTypes();
// This is a brute force method, but I can't come up with something
// better atm without changing either all Loader or the xml-files.
while (cts.next()) {
ContentType ct = cts.getContentType();
try {
Class.forName(type.getClassName()).asSubclass(Class.forName(ct.getClassName()));
} catch (Exception ex) {
// This cast is not valid so type is not a sublacss of ct
continue;
}
// Save the current ct as possible parent if we haven't found any parent yet
// or if the current ancestor list is longer than that one from the possible
// parent earlier found
if (!type.getClassName().equals(ct.getClassName())
&& (parent == null
|| (parent.getAncestors() != null
&& ct.getAncestors() != null
&& parent.getAncestors().length() < ct.getAncestors().length()))) {
parent = ct;
}
}
// If there is a valid parent content type create the pedigree
if (parent != null && !parent.getClassName().equals(type.getClassName())) {
if (parent.getAncestors() != null) {
String parentAncestors = parent.getAncestors();
StringTokenizer strTok = new StringTokenizer(parentAncestors, "/");
// Add parent ancestors to this content types ancestor list
// Also while we iterate through the list, we also need to add
// this content type as sibling to all entries in the ancestor list
while (strTok.hasMoreElements()) {
BigDecimal ctID = (BigDecimal) strTok.nextElement();
// Get the current content type
try {
ContentType ct = new ContentType(ctID);
ct.addSiblings(ctID);
} catch (Exception ex) {
// The db is broken. There is no content type for this ID
}
// Add parent ancestor
type.addAncestor(ctID);
}
}
// Add parent to ancestor list
type.addAncestor(parent.getID());
// Add this to parent siblings
parent.addSiblings(type.getID());
}
}
} }