2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
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
;
22 import org
.apache
.logging
.log4j
.LogManager
;import org
.apache
.logging
.log4j
.Logger
;
23 import org
.eclipse
.swt
.dnd
.ByteArrayTransfer
;
24 import org
.eclipse
.swt
.dnd
.TransferData
;
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
;
33 * <p>CdmDataTransfer class.</p>
36 * @created Jun 18, 2010
38 public class CdmDataTransfer
extends ByteArrayTransfer
{
40 @SuppressWarnings("unused")
41 private static final Logger logger
= LogManager
.getLogger(CdmDataTransfer
.class);
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();
50 * <p>Getter for the field <code>instance</code>.</p>
52 * @return a {@link eu.etaxonomy.taxeditor.editor.CdmDataTransfer} object.
54 public static synchronized CdmDataTransfer
getInstance() {
58 private ICdmBase cdmBase
;
61 protected int[] getTypeIds() {
62 return new int[] { TYPE_ID
};
66 protected String
[] getTypeNames() {
67 return new String
[]{ TYPE_NAME
};
71 protected void javaToNative(Object object
, TransferData transferData
) {
72 byte[] bytes
= toByteArray((ICdmBase
[]) object
);
74 super.javaToNative(bytes
, transferData
);
79 protected Object
nativeToJava(TransferData transferData
) {
80 byte[] bytes
= (byte[]) super.nativeToJava(transferData
);
81 return fromByteArray(bytes
);
85 private byte[] toByteArray(ICdmBase
[] cdmBaseObjects
) {
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.
91 ByteArrayOutputStream byteOut
= new ByteArrayOutputStream();
92 DataOutputStream out
= new DataOutputStream(byteOut
);
98 for(ICdmBase cdmBaseObject
: cdmBaseObjects
){
99 writeCdmBaseObject(cdmBaseObject
, out
);
102 bytes
= byteOut
.toByteArray();
103 } catch (IOException e
) {
104 // when in doubt send nothing
110 private ICdmBase
[] fromByteArray(byte[] bytes
) {
111 DataInputStream in
= new DataInputStream(new ByteArrayInputStream(bytes
));
114 List
<ICdmBase
> cdmBaseObjects
= new ArrayList
<ICdmBase
>();
118 cdmBaseObjects
.add(readCdmBaseObject(in
));
120 }catch(EOFException e
){
121 return cdmBaseObjects
.toArray(new ICdmBase
[]{});
123 } catch (IOException e
) {
128 private ICdmBase
readCdmBaseObject(DataInputStream in
) throws IOException
{
129 String className
= in
.readUTF();
131 String idString
= in
.readUTF();
134 if(className
.equals(Synonym
.class.getName()) || className
.equals(Taxon
.class.getName())){
135 ICdmBase object
= CdmStore
.getService(ITaxonService
.class).load(UUID
.fromString(idString
));
147 private void writeCdmBaseObject(ICdmBase cdmBase
, DataOutputStream out
) throws IOException
{
149 * CdmBase object serialization:
152 out
.writeUTF(cdmBase
.getClass().getName());
153 out
.writeInt(SEPARATOR
);
154 out
.writeUTF(cdmBase
.getUuid().toString());
157 if(cdmBase
.getId() == 0){
158 // fallback for unsaved instances
159 this.cdmBase
= cdmBase
;