From 0f7948f18ad5e8c3f833dae9294e1d2312aaf3e2 Mon Sep 17 00:00:00 2001 From: quasi Date: Wed, 26 Jan 2011 15:21:22 +0000 Subject: [PATCH] =?UTF-8?q?Erzeuge=20die=20Vererbungen=20f=C3=BCr=20alle?= =?UTF-8?q?=20Contenttypen=20beim=20Upgrade.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.libreccm.org/ccm/trunk@714 8810af33-2d31-482b-a856-94f89814c4df --- .../upgrade/CreateGenericContentTypes.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/ccm-cms/src/com/arsdigita/cms/upgrade/CreateGenericContentTypes.java b/ccm-cms/src/com/arsdigita/cms/upgrade/CreateGenericContentTypes.java index 92e16802e..187216f97 100644 --- a/ccm-cms/src/com/arsdigita/cms/upgrade/CreateGenericContentTypes.java +++ b/ccm-cms/src/com/arsdigita/cms/upgrade/CreateGenericContentTypes.java @@ -98,10 +98,95 @@ public class CreateGenericContentTypes extends Program { tc.commitTxn(); // save database additions for re-reading + tc.beginTxn(); + + ContentTypeCollection ctc = ContentType.getAllContentTypes(); + + s_log.debug("Starte content types update"); + + while (ctc.next()) { + + ContentType ct = ctc.getContentType(); + + s_log.debug("Verarbeite " + ct.getClassName()); + + createPedigree(ct); + + } + tc.commitTxn(); } }.run(); } + /** + * 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 descendant to all entries in the ancestor list + while (strTok.hasMoreElements()) { + BigDecimal ctID = new BigDecimal(strTok.nextToken()); + + // Get the current content type + try { + ContentType ct = new ContentType(ctID); + ct.addDescendants(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 descendants + parent.addDescendants(type.getID()); + } + } }