You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2006/08/03 21:40:07 UTC

svn commit: r428498 - in /lenya/trunk/src/modules/migration: ./ java/ java/test/ java/test/org/ java/test/org/apache/ java/test/org/apache/lenya/ java/test/org/apache/lenya/cms/ java/test/org/apache/lenya/cms/migration/

Author: andreas
Date: Thu Aug  3 12:40:06 2006
New Revision: 428498

URL: http://svn.apache.org/viewvc?rev=428498&view=rev
Log:
Added migration module

Added:
    lenya/trunk/src/modules/migration/
    lenya/trunk/src/modules/migration/java/
    lenya/trunk/src/modules/migration/java/test/
    lenya/trunk/src/modules/migration/java/test/org/
    lenya/trunk/src/modules/migration/java/test/org/apache/
    lenya/trunk/src/modules/migration/java/test/org/apache/lenya/
    lenya/trunk/src/modules/migration/java/test/org/apache/lenya/cms/
    lenya/trunk/src/modules/migration/java/test/org/apache/lenya/cms/migration/
    lenya/trunk/src/modules/migration/java/test/org/apache/lenya/cms/migration/MigrateUuidsTest.java
    lenya/trunk/src/modules/migration/module.xml

Added: lenya/trunk/src/modules/migration/java/test/org/apache/lenya/cms/migration/MigrateUuidsTest.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/migration/java/test/org/apache/lenya/cms/migration/MigrateUuidsTest.java?rev=428498&view=auto
==============================================================================
--- lenya/trunk/src/modules/migration/java/test/org/apache/lenya/cms/migration/MigrateUuidsTest.java (added)
+++ lenya/trunk/src/modules/migration/java/test/org/apache/lenya/cms/migration/MigrateUuidsTest.java Thu Aug  3 12:40:06 2006
@@ -0,0 +1,133 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.migration;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.avalon.framework.service.ServiceSelector;
+import org.apache.lenya.ac.impl.AbstractAccessControlTest;
+import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.DocumentFactory;
+import org.apache.lenya.cms.publication.DocumentManager;
+import org.apache.lenya.cms.publication.DocumentUtil;
+import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.publication.PublicationUtil;
+import org.apache.lenya.cms.repository.RepositoryUtil;
+import org.apache.lenya.cms.repository.Session;
+import org.apache.lenya.cms.site.SiteManager;
+import org.apache.lenya.cms.site.SiteUtil;
+
+public class MigrateUuidsTest extends AbstractAccessControlTest {
+
+    public void testMigrateUuids() throws Exception {
+
+        login("lenya");
+
+        Session session = RepositoryUtil.getSession(getManager(), getRequest());
+        DocumentFactory factory = DocumentUtil.createDocumentIdentityMap(getManager(), session);
+        Publication[] pubs = PublicationUtil.getPublications(getManager());
+        for (int i = 0; i < pubs.length; i++) {
+            migratePublication(pubs[i], factory);
+        }
+        session.commit();
+    }
+
+    private void migratePublication(Publication pub, DocumentFactory factory) throws Exception {
+        getLogger().info("Migrating publication [" + pub.getId() + "]");
+
+        String[] areas = { Publication.AUTHORING_AREA, Publication.LIVE_AREA,
+                Publication.ARCHIVE_AREA, Publication.TRASH_AREA };
+        for (int i = 0; i < areas.length; i++) {
+            migrateArea(pub, areas[i], factory);
+        }
+    }
+
+    private void migrateArea(Publication pub, String area, DocumentFactory factory)
+            throws Exception {
+        getLogger().info("Migrating area [" + pub.getId() + ":" + area + "]");
+        Document[] docs = SiteUtil.getDocuments(getManager(), factory, pub, area);
+        for (int i = 0; i < docs.length; i++) {
+            if (docs[i].getUUID().startsWith("/")) {
+                migrateDocument(docs[i]);
+            }
+        }
+    }
+
+    private Map migratedDocs = new HashMap();
+
+    private void migrateDocument(Document doc) throws Exception {
+
+        getLogger().info("Migrating document [" + doc + "]");
+
+        DocumentManager docManager = null;
+        SiteManager siteManager = null;
+        ServiceSelector selector = null;
+        try {
+            docManager = (DocumentManager) getManager().lookup(DocumentManager.ROLE);
+            selector = (ServiceSelector) getManager().lookup(SiteManager.ROLE + "Selector");
+            siteManager = (SiteManager) selector.select(doc.getPublication().getSiteManagerHint());
+
+            String path = SiteUtil.getPath(getManager(), doc);
+
+            siteManager.getSiteStructure(doc.getFactory(), doc.getPublication(), doc.getArea())
+                    .getRepositoryNode()
+                    .lock();
+
+            Document newDoc;
+
+            String docId = doc.getUUID();
+            if (this.migratedDocs.containsKey(docId)) {
+                Document migratedDoc = (Document) this.migratedDocs.get(docId);
+                newDoc = docManager.addVersion(migratedDoc, doc.getArea(), doc.getLanguage());
+            } else {
+                newDoc = docManager.add(doc.getFactory(),
+                        doc.getResourceType(),
+                        doc.getSourceURI(),
+                        doc.getPublication(),
+                        doc.getArea(),
+                        doc.getLanguage(),
+                        doc.getExtension());
+                
+                String[] uris = doc.getMetaDataNamespaceUris();
+                for (int i = 0; i < uris.length; i++) {
+                    newDoc.getMetaData(uris[i]).replaceBy(doc.getMetaData(uris[i]));
+                }
+                
+                migratedDocs.put(docId, newDoc);
+                siteManager.set(path, newDoc);
+            }
+
+            doc.getRepositoryNode().lock();
+            doc.delete();
+
+        } finally {
+            if (docManager != null) {
+                getManager().release(docManager);
+            }
+            if (selector != null) {
+                if (siteManager != null) {
+                    selector.release(siteManager);
+                }
+                getManager().release(selector);
+            }
+        }
+    }
+
+}

Added: lenya/trunk/src/modules/migration/module.xml
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/migration/module.xml?rev=428498&view=auto
==============================================================================
--- lenya/trunk/src/modules/migration/module.xml (added)
+++ lenya/trunk/src/modules/migration/module.xml Thu Aug  3 12:40:06 2006
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright 1999-2005 The Apache Software Foundation
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<!-- $Id: publication.xml 374687 2006-02-03 15:24:55Z michi $ -->
+
+<module xmlns="http://apache.org/lenya/module/1.0">
+  <id>org.apache.lenya.modules.migration</id>
+  <package>org.apache.lenya.modules</package>
+  <version>0.1-dev</version>
+  <name>Migration Module</name>
+  <lenya-version>@lenya.version@</lenya-version>
+  <description>Migration</description>
+</module>



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org