Julian Klein <[EMAIL PROTECTED]> writes:

> I was wondering if there was a way to import export
> from one store to another.  For example, one desides
> to switch from an XML store to a RDBMS store?

I asked the same at Dec 6. There seems to be no offical solution.
I wrote a small program, which does copied my file store to JDBC 
store. I used it exactly once and it worked. However its completly 
unpolished (including hardcoded pathes etc.). It does not copy
/users since it is a read only store at FCH, but only /files.
Versioning is not supported. Locks are not copied.

Maybe I will polish it some day, but I am currently to busy.
The programm is bascially cut&paste from MacroImpl.


You need to configure two namespaces in your Domain.xml, using
the old and the new store. Ask me, if you have any problems.

Martin






package de.vs_c.slide.copy;

import java.util.Enumeration;
import java.util.Stack;
import org.apache.slide.authenticate.CredentialsToken;
import org.apache.slide.authenticate.SecurityToken;
import org.apache.slide.common.Domain;
import org.apache.slide.common.NamespaceAccessToken;
import org.apache.slide.common.SlideException;
import org.apache.slide.common.SlideToken;
import org.apache.slide.common.SlideTokenImpl;
import org.apache.slide.content.Content;
import org.apache.slide.content.NodeRevisionContent;
import org.apache.slide.content.NodeRevisionDescriptor;
import org.apache.slide.content.NodeRevisionDescriptors;
import org.apache.slide.content.NodeRevisionNumber;
import org.apache.slide.macro.Macro;
import org.apache.slide.security.NodePermission;
import org.apache.slide.security.Security;
import org.apache.slide.structure.ObjectNode;
import org.apache.slide.structure.ObjectNotFoundException;
import org.apache.slide.structure.Structure;
import org.apache.slide.util.logger.Logger;

/**
 * A tool to create a copy of a namespace.
 * This tool should be independent of the underlying stores.
 *
 * <h2>Notes</h2>
 * <ul>
 *  <li>Current state is alpha at best.</li>
 *  <li> Currently only "/files" is copied. Is there a need to copy the rest?</li>
 *  <li>Versioning is not (yet) supported.</li>
 *  <li>Locks are not copied.</li> 
 * </ul> 
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Martin Holberg,Martin Holz</a>
 * @version  $Revision: 1.6 $ $Date: 2003/02/19 20:00:53 $ $State: Exp $
 *
 * Copyright 2003 FIZ CHEMIE Berlin
 */


public class NamespaceCopy {

    static SecurityToken security = new SecurityToken("root");
    static CredentialsToken credentials = new CredentialsToken("root");
    static SlideToken token = new SlideTokenImpl(credentials);

    private Logger logger;

    private NamespaceAccessToken srcAccess;
    private NamespaceAccessToken destAccess; 

    private Security srcSecurity;
    private Security destSecurity;

    private Structure srcStructure;
    private Structure destStructure;

    private Content srcContent;
    private Content destContent;

    //private Lock srcLock;
    //private Lock destLock;
    
    private Macro destMacro;


    final static String srcNS = "vscmodules.old"; 
    final static String destNS = "vscmodules"; 
    final static String FILES = "/files";
    
    public NamespaceCopy(String filename) throws Exception {
        
        Domain.init(filename);

        this.srcAccess = Domain.accessNamespace(security, srcNS);
        this.srcStructure = srcAccess.getStructureHelper();
        this.srcSecurity = srcAccess.getSecurityHelper();
        this.srcContent = srcAccess.getContentHelper();
        this.logger = srcAccess.getLogger();
        //this.srcLock = srcAccess.getLockHelper();     


        this.destAccess = Domain.accessNamespace(security, destNS);
        this.destStructure = destAccess.getStructureHelper();
        this.destSecurity = destAccess.getSecurityHelper();
        this.destContent = destAccess.getContentHelper();
        //this.destLock = destAccess.getLockHelper();

        this.destMacro = destAccess.getMacroHelper();
    }

    public void run() throws Exception {
            this.destAccess.begin();
            try {
                this.destMacro.delete(this.token,FILES);
            } catch(SlideException e) {
            }
            this.destAccess.commit();
            

        this.logger.log("Starting Iteration",Logger.INFO);


        srcAccess.begin();
        copy(FILES,FILES,0);
        srcAccess.commit();
        
    }
  
 
    public void copy(String srcUri, String destUri, int depth) throws Exception {
        /*SlideException,NotSupportedException {
        char spacer[] = new char[4*depth];
        for (int i=0; i<4*depth; i=i+4) {
            spacer[i] = ' ';  spacer[i+1] = ' ';
            spacer[i+2] = ' ';  spacer[i+3] = ' ';
            
        }       
        String s = new String(spacer);
        */


        ObjectNode srcNode;
        try {
            srcNode = srcStructure.retrieve(token,srcUri);
        } catch (ObjectNotFoundException oe) {
            logger.log("Can't retrive object at uri " + srcUri
                       + ": " + oe.getMessage(),Logger.WARNING);
            return;
        }


        this.logger.log("Copying " + srcUri,Logger.INFO);

        try {
            this.destAccess.begin();

            ObjectNode destNode = srcNode.copyObject();
            this.destStructure.create(this.token,destNode,destUri);


            // Copying permissions
            Enumeration permissions = srcSecurity
                .enumeratePermissions(token, srcNode);
                
            while (permissions.hasMoreElements()) {
                NodePermission permission =
                    (NodePermission) permissions.nextElement();
                NodePermission newPermission =
                    new NodePermission(destUri,
                                       permission.getSubjectUri(),
                                       permission.getActionUri(),
                                       permission.isInheritable());
                destSecurity.grantPermission(token, newPermission);
            }

            // Now copying revision descriptors and content             
            NodeRevisionDescriptors revisionDescriptors =
                srcContent.retrieve(token, srcNode.getUri());


                
            if (revisionDescriptors.hasRevisions()) {
                
                // Iterators
                NodeRevisionNumber currentRevisionNumber =
                    revisionDescriptors.getInitialRevision();
                NodeRevisionDescriptor currentRevisionDescriptor = null;
                NodeRevisionContent currentRevisionContent = null;
                
                Stack revisionNumbers = new Stack();
                revisionNumbers.push(currentRevisionNumber);
                
                // Creating the initial revision
                currentRevisionDescriptor = srcContent
                    .retrieve(token, revisionDescriptors,
                              currentRevisionNumber);
                if (currentRevisionDescriptor.getContentLength() > 0) {
                    currentRevisionContent = srcContent
                        .retrieve(token, revisionDescriptors,
                                  currentRevisionDescriptor);
                } else {
                    currentRevisionContent = null;
                }
                
                destContent.create(token, destUri,
                                   currentRevisionDescriptor,
                                   currentRevisionContent);
            }
            // TODO : Parse stack
            // Algorithm :
            // - While Stack is not empty
            // - Pop a revision number
            // - For each successor of that number, create a new revision
            //   based of the old revision
            // - Add the successor to the stack

            destAccess.commit();
        } catch (SlideException se) {
            logger.log("Failed to copy object at uri " + srcUri
                       + ": " + se.getMessage(),Logger.ERROR);
            destAccess.rollback();
            return;
        }

        // Recursion over children
        Enumeration children = srcNode.enumerateChildren();
        while (children.hasMoreElements()) {
            String childUri =  (String)children.nextElement();
            copy(childUri,childUri,depth + 1);
        } 
    }

    
    /**
     * Remove object at uri and all its children.
     * Do nothing, if no object exists at uri.
     *
    public void delete(String uri) {
        
        
    }*/
 

    public static void main(String[] args) throws Exception {

        String filename = (args.length > 0 && args[0] != null) ? args[0] : 
"/usr/home/kelso/jbproject/NSC/slide.xconf";
        
        NamespaceCopy nsc = new NamespaceCopy(filename);
        nsc.run();
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to