Project

General

Profile

Download (4.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2020 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
package eu.etaxonomy.taxeditor.termtree;
10

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

    
21
import org.eclipse.swt.SWTException;
22
import org.eclipse.swt.dnd.ByteArrayTransfer;
23
import org.eclipse.swt.dnd.TransferData;
24

    
25
import eu.etaxonomy.cdm.api.service.ITermNodeService;
26
import eu.etaxonomy.cdm.model.description.Feature;
27
import eu.etaxonomy.cdm.model.term.TermNode;
28
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
29
import eu.etaxonomy.taxeditor.model.MessagingUtils;
30
import eu.etaxonomy.taxeditor.store.CdmStore;
31

    
32
/**
33
 * @author k.luther
34
 * @since Oct 13, 2020
35
 */
36
public class TermNodeDtoTransfer extends ByteArrayTransfer {
37

    
38
    private static TermNodeDtoTransfer instance = new TermNodeDtoTransfer();
39
    private static final String TYPE_NAME = "featureNodeDto-transfer-format";
40
    private static final int TYPEID = registerType(TYPE_NAME);
41

    
42
    /**
43
     * <p>Getter for the field <code>instance</code>.</p>
44
     *
45
     * @return a {@link eu.etaxonomy.taxeditor.termtree.FeatureNodeTransfer} object.
46
     */
47
    public static synchronized TermNodeDtoTransfer getInstance() {
48
        return instance;
49
    }
50

    
51
    private TermNodeDtoTransfer() {
52
    }
53

    
54
    /** {@inheritDoc} */
55
    @Override
56
    protected int[] getTypeIds() {
57
        return new int[] { TYPEID };
58
    }
59

    
60
    /** {@inheritDoc} */
61
    @Override
62
    protected String[] getTypeNames() {
63
        return new String[] { TYPE_NAME };
64
    }
65
    @Override
66
    protected void javaToNative(Object object, TransferData transferData) {
67
        if (object != null){
68
            byte[] bytes = toByteArray((TermNodeDto[]) object);
69
            if (bytes != null) {
70
                super.javaToNative(bytes, transferData);
71
            }
72
        }
73
    }
74

    
75
    @Override
76
    protected Object nativeToJava(TransferData transferData) {
77
        try{
78
            byte[] bytes = (byte[]) super.nativeToJava(transferData);
79
            if (bytes != null){
80
                return fromByteArray(bytes);
81
            }
82
        }catch (SWTException e){
83
            MessagingUtils.warningDialog("The new imported node needs to be saved first", this, "Newly created nodes can not be moved without saving");
84
        }
85

    
86
        return null;
87
    }
88

    
89

    
90
    protected byte[] toByteArray(TermNodeDto[] elements) {
91
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
92
        DataOutputStream out = new DataOutputStream(byteOut);
93

    
94
        byte[] bytes = null;
95

    
96
        try {
97
            for(TermNodeDto element : elements){
98
                if (element.getUuid() == null){
99
                //  MessagingUtils.warningDialog("The new imported node needs to be saved first", this, "Newly created nodes can not be moved without saving");
100

    
101
                }else{
102
                    writeElementUuid(element, out);
103
                }
104
            }
105
            out.close();
106
            bytes = byteOut.toByteArray();
107
        } catch (IOException e) {
108

    
109
            // when in doubt send nothing
110
        }
111
        return bytes;
112
    }
113

    
114
    protected Object[] fromByteArray(byte[] bytes) {
115
        DataInputStream in = new DataInputStream(
116
                new ByteArrayInputStream(bytes));
117

    
118
        try {
119
            List<TermNodeDto> elements = new ArrayList<TermNodeDto>();
120

    
121
            try{
122
                while(true){
123
                    UUID uuid = readElementUuid(in);
124
                    elements.add(loadElement(uuid));
125
                }
126
            }catch(EOFException e){
127
                return  elements.toArray();
128
            }
129
        } catch (IOException e) {
130
            return null;
131
        }
132
    }
133

    
134
    public TermNodeDto loadElement(UUID uuid){
135
        TermNode<Feature> node = CdmStore.getService(ITermNodeService.class).load(uuid);
136
        return TermNodeDto.fromNode(node, null);
137
    }
138

    
139
    public UUID readElementUuid(DataInputStream in) throws IOException{
140
        return UUID.fromString(in.readUTF());
141
    }
142

    
143
    private void writeElementUuid(TermNodeDto element, DataOutputStream out) throws IOException {
144
        out.writeUTF(element.getUuid().toString());
145
    }
146

    
147

    
148
}
(7-7/11)