summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/entity/transfer/TransferItem.java
blob: d63ce239d91759d3e13978aa4d3b54a0c818483e (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.c2kernel.entity.transfer;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import com.c2kernel.collection.Collection;
import com.c2kernel.collection.CollectionArrayList;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.entity.TraceableEntity;
import com.c2kernel.lifecycle.instance.Workflow;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.lookup.InvalidItemPathException;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.lookup.Path;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.persistency.outcome.Outcome;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.Property;
import com.c2kernel.property.PropertyArrayList;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.Logger;

public class TransferItem {
    private ArrayList<String> domainPaths;
    protected ItemPath itemPath;
    static AgentPath importAgentId;

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

    public TransferItem(ItemPath itemPath) throws Exception {
        this.itemPath = itemPath;
        domainPaths = new ArrayList<String>();
        Iterator<Path> paths = Gateway.getLookup().searchAliases(itemPath);
        while (paths.hasNext()) {
            DomainPath thisPath = (DomainPath)paths.next();
            domainPaths.add(thisPath.toString());
        }
    }
    
    public ArrayList<String> getDomainPaths() {
		return domainPaths;
	}

	public void setDomainPaths(ArrayList<String> domainPaths) {
		this.domainPaths = domainPaths;
	}

	public void setUUID( String uuid ) throws InvalidItemPathException
    {
    	itemPath = new ItemPath(uuid);
    }
    
    public String getUUID() {
    	return itemPath.getUUID().toString();
    }
    
    public void exportItem(File dir, String path) throws Exception {
        Logger.msg("Path " + path + " in " + itemPath);
        String[] contents = Gateway.getStorage().getClusterContents(itemPath, path);
        if (contents.length > 0) {
            FileStringUtility.createNewDir(dir.getCanonicalPath());
            for (String content : contents) {
                exportItem(new File(dir, content), path + "/" + content);
            }
        } else { //no children, try to dump object
            try {
                C2KLocalObject obj = Gateway.getStorage().get(itemPath, path, null);
                Logger.msg("Dumping object " + path + " in " + itemPath);
                File dumpPath = new File(dir.getCanonicalPath() + ".xml");
                FileStringUtility.string2File(dumpPath, Gateway.getMarshaller().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(itemPath, ClusterStorage.PROPERTY + "/Name", null);
            throw new Exception("Entity " + itemPath + " already in use as " + name.getValue());
        } catch (Exception ex) {
        }

        // retrieve objects
        ArrayList<String> objectFiles = FileStringUtility.listDir(dir.getCanonicalPath(), false, true);
        ArrayList<C2KLocalObject> objects = new ArrayList<C2KLocalObject>();
        for (String element : objectFiles) {
            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)Gateway.getMarshaller().unmarshall(xmlFile);

            objects.add(newObj);
        }

        // create item
        TraceableEntity newItem = Gateway.getCorbaServer().createItem(itemPath);
        Gateway.getLookupManager().add(itemPath);

        PropertyArrayList props = new PropertyArrayList();
        CollectionArrayList colls = new CollectionArrayList();
        Workflow wf = null;
        // put objects
        for (C2KLocalObject obj : objects) {
            if (obj instanceof Property)
                props.list.add((Property)obj);
            else if (obj instanceof Collection)
            	colls.list.add((Collection<?>)obj);
            else if (obj instanceof Workflow)
                wf = (Workflow)obj;
        }

        if (wf == null)
            throw new Exception("No workflow found in import for "+itemPath);

        // init item
        newItem.initialise(importAgentId.getSystemKey(), 
        		Gateway.getMarshaller().marshall(props), 
        		Gateway.getMarshaller().marshall(wf.search("workflow/domain")),
        		Gateway.getMarshaller().marshall(colls));

        // store objects
        importByType(ClusterStorage.HISTORY, objects);
        importByType(ClusterStorage.OUTCOME, objects);
        importByType(ClusterStorage.VIEWPOINT, objects);
        Gateway.getStorage().commit(this);
        // add domPaths
        for (String element : domainPaths) {
            DomainPath newPath = new DomainPath(element, itemPath);
            Gateway.getLookupManager().add(newPath);
        }
    }

    private void importByType(String type, ArrayList<C2KLocalObject> objects) throws Exception {
        for (C2KLocalObject element : objects) {
            if (element.getClusterType().equals(type))
                Gateway.getStorage().put(itemPath, element, this);
        }

    }
}