summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/entity/transfer/TransferItem.java
blob: a869741bcd27b4545ee2133cd4c43a4c103a4514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.c2kernel.entity.transfer;

import java.io.File;
import java.util.*;

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.*;
import com.c2kernel.lifecycle.instance.Workflow;
import com.c2kernel.lookup.*;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.persistency.outcome.Outcome;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.*;
import com.c2kernel.utils.*;

public class TransferItem {
    public ArrayList domainPaths;
    public int sysKey;
    static int importAgentId;

    public TransferItem() throws Exception {
        try {
            importAgentId = Gateway.getLDAPLookup().getRoleManager().getAgentPath("system").getSysKey();
        } catch (ObjectNotFoundException e) {
            Logger.error("TransferItem - System user not found!");
            throw e;
        }
    }

    public TransferItem(int sysKey) throws Exception {
        this.sysKey = sysKey;
        domainPaths = new ArrayList();
        Property name = (Property)Gateway.getStorage().get(sysKey, ClusterStorage.PROPERTY + "/Name", null);
        Enumeration paths = Gateway.getLDAPLookup().search(new DomainPath(), name.getValue());
        while (paths.hasMoreElements()) {
            DomainPath thisPath = (DomainPath)paths.nextElement();
            domainPaths.add(thisPath.toString());
        }
    }

    public void exportItem(File dir, String path) throws Exception {
        Logger.msg("Path " + path + " in " + sysKey);
        String[] contents = Gateway.getStorage().getClusterContents(sysKey, path);
        if (contents.length > 0) {
            FileStringUtility.createNewDir(dir.getCanonicalPath());
            for (int i = 0; i < contents.length; i++) {
                exportItem(new File(dir, contents[i]), path + "/" + contents[i]);
            }
        } else { //no children, try to dump object
            try {
                C2KLocalObject obj = Gateway.getStorage().get(sysKey, path, null);
                Logger.msg("Dumping object " + path + " in " + sysKey);
                File dumpPath = new File(dir.getCanonicalPath() + ".xml");
                FileStringUtility.string2File(dumpPath, CastorXMLUtility.marshall(obj));
                return;
            } catch (ObjectNotFoundException ex) {
            } // not an object
        }
    }

    public void importItem(File dir) throws Exception {
        // check if already exists
        try {
            Property name = (Property)Gateway.getStorage().get(sysKey, ClusterStorage.PROPERTY + "/Name", null);
            throw new Exception("Syskey " + sysKey + " already in use as " + name.getValue());
        } catch (Exception ex) {
        }

        ArrayList events, outcomes, viewpoints = new ArrayList();
        // retrieve objects
        ArrayList objectFiles = FileStringUtility.listDir(dir.getCanonicalPath(), false, true);
        ArrayList objects = new ArrayList();
        for (Iterator iter = objectFiles.iterator(); iter.hasNext();) {
            String element = (String)iter.next();
            String xmlFile = FileStringUtility.file2String(element);
            C2KLocalObject newObj;
            String choppedPath = element.substring(dir.getCanonicalPath().length()+1, element.length()-4);
            Logger.msg(choppedPath);
            if (choppedPath.startsWith(ClusterStorage.OUTCOME))
                newObj = new Outcome(choppedPath, xmlFile);
            else
                newObj = (C2KLocalObject)CastorXMLUtility.unmarshall(xmlFile);

            objects.add(newObj);
        }

        // create item
        EntityPath entityPath = new EntityPath(sysKey);
        TraceableEntity newItem = (TraceableEntity)Gateway.getCorbaServer().createEntity(entityPath);
        Gateway.getLDAPLookup().add(entityPath);
        
        PropertyArrayList props = new PropertyArrayList();
        Workflow wf = null;
        // put objects
        for (Iterator iter = objects.iterator(); iter.hasNext();) {
            C2KLocalObject obj = (C2KLocalObject)iter.next();
            if (obj instanceof Property)
                props.list.add(obj);
            else if (obj instanceof Workflow)
                wf = (Workflow)obj;
        }

        if (wf == null)
            throw new Exception("No workflow found in import for "+sysKey);
            
        // init item
        newItem.initialise(importAgentId, CastorXMLUtility.marshall(props), CastorXMLUtility.marshall(wf.search("workflow/domain")));

        // store objects
        importByType(ClusterStorage.COLLECTION, objects);
        importByType(ClusterStorage.HISTORY, objects);
        importByType(ClusterStorage.OUTCOME, objects);
        importByType(ClusterStorage.VIEWPOINT, objects);
        Gateway.getStorage().commit(this);
        // add domPaths
        for (Iterator iter = domainPaths.iterator(); iter.hasNext();) {
            String element = (String)iter.next();
            DomainPath newPath = new DomainPath(element, entityPath);
            Gateway.getLDAPLookup().add(newPath);
        }
    }
    
    private void importByType(String type, ArrayList objects) throws Exception {
        for (Iterator iter = objects.iterator(); iter.hasNext();) {
            C2KLocalObject element = (C2KLocalObject)iter.next();
            if (element.getClusterType().equals(type))
                Gateway.getStorage().put(sysKey, element, this);
        }
        
    }
}