Project

General

Profile

Download (4.12 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.editor;
11

    
12
import java.io.ByteArrayInputStream;
13
import java.io.ByteArrayOutputStream;
14
import java.io.DataInputStream;
15
import java.io.DataOutputStream;
16
import java.io.EOFException;
17
import java.io.IOException;
18
import java.util.ArrayList;
19
import java.util.List;
20
import java.util.UUID;
21

    
22
import org.apache.log4j.Logger;
23
import org.eclipse.swt.dnd.ByteArrayTransfer;
24
import org.eclipse.swt.dnd.TransferData;
25

    
26
import eu.etaxonomy.cdm.api.service.ITaxonService;
27
import eu.etaxonomy.cdm.model.common.ICdmBase;
28
import eu.etaxonomy.cdm.model.taxon.Synonym;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.taxeditor.store.CdmStore;
31

    
32
/**
33
 * <p>CdmDataTransfer class.</p>
34
 *
35
 * @author n.hoffmann
36
 * @created Jun 18, 2010
37
 */
38
public class CdmDataTransfer extends ByteArrayTransfer {
39

    
40
	@SuppressWarnings("unused")
41
    private static final Logger logger = Logger.getLogger(CdmDataTransfer.class);
42

    
43
	private static int SEPARATOR = -10;
44
	private static int EOF = -20;
45
	private static final String TYPE_NAME = "cdmdata-transfer-format"; //$NON-NLS-1$
46
	private static final int TYPE_ID = registerType (TYPE_NAME);
47
	private static final CdmDataTransfer instance = new CdmDataTransfer();
48

    
49
	/**
50
	 * <p>Getter for the field <code>instance</code>.</p>
51
	 *
52
	 * @return a {@link eu.etaxonomy.taxeditor.editor.CdmDataTransfer} object.
53
	 */
54
	public static synchronized CdmDataTransfer getInstance() {
55
		return instance;
56
	}
57

    
58
	private ICdmBase cdmBase;
59

    
60
	@Override
61
	protected int[] getTypeIds() {
62
		return new int[] { TYPE_ID };
63
	}
64

    
65
	@Override
66
	protected String[] getTypeNames() {
67
		return new String[]{ TYPE_NAME };
68
	}
69

    
70
	@Override
71
	protected void javaToNative(Object object, TransferData transferData) {
72
		byte[] bytes = toByteArray((ICdmBase[]) object);
73
		if (bytes != null) {
74
            super.javaToNative(bytes, transferData);
75
        }
76
	}
77

    
78
	@Override
79
	protected Object nativeToJava(TransferData transferData) {
80
		byte[] bytes = (byte[]) super.nativeToJava(transferData);
81
		return fromByteArray(bytes);
82
	}
83

    
84

    
85
	private byte[] toByteArray(ICdmBase[] cdmBaseObjects) {
86
		/**
87
		 * Transfer data is an array of gadgets. Serialized version is: (int)
88
		 * number of gadgets (Gadget) gadget 1 (Gadget) gadget 2 ... repeat for
89
		 * each subsequent gadget see writeGadget for the (Gadget) format.
90
		 */
91
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
92
		DataOutputStream out = new DataOutputStream(byteOut);
93

    
94
		byte[] bytes = null;
95

    
96
		try {
97

    
98
			for(ICdmBase cdmBaseObject : cdmBaseObjects){
99
				writeCdmBaseObject(cdmBaseObject, out);
100
			}
101
			out.close();
102
			bytes = byteOut.toByteArray();
103
		} catch (IOException e) {
104
			// when in doubt send nothing
105
		}
106
		return bytes;
107
	}
108

    
109

    
110
	private ICdmBase[] fromByteArray(byte[] bytes) {
111
		DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
112

    
113
		try {
114
			List<ICdmBase> cdmBaseObjects = new ArrayList<ICdmBase>();
115

    
116
			try{
117
				while(true){
118
					cdmBaseObjects.add(readCdmBaseObject(in));
119
				}
120
			}catch(EOFException e){
121
				return cdmBaseObjects.toArray(new ICdmBase[]{});
122
			}
123
		} catch (IOException e) {
124
			return null;
125
		}
126
	}
127

    
128
	private ICdmBase readCdmBaseObject(DataInputStream in) throws IOException{
129
		String className = in.readUTF();
130
		in.readInt();
131
		String idString = in.readUTF();
132
		in.readInt();
133

    
134
		if(className.equals(Synonym.class.getName()) || className.equals(Taxon.class.getName())){
135
			ICdmBase object = CdmStore.getService(ITaxonService.class).load(UUID.fromString(idString));
136

    
137
			if(object == null){
138
				return cdmBase;
139
			}
140

    
141
			return object;
142
		}
143

    
144
		return null;
145
	}
146

    
147
	private void writeCdmBaseObject(ICdmBase cdmBase, DataOutputStream out) throws IOException{
148
		/**
149
		 * CdmBase object serialization:
150
		 *
151
		 */
152
		out.writeUTF(cdmBase.getClass().getName());
153
		out.writeInt(SEPARATOR);
154
		out.writeUTF(cdmBase.getUuid().toString());
155
		out.writeInt(EOF);
156

    
157
		if(cdmBase.getId() == 0){
158
			// fallback for unsaved instances
159
			this.cdmBase = cdmBase;
160
		}
161
	}
162

    
163
}
(2-2/9)