Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ac131c436d5dbd4c2c413a74003b57f82ec9846c (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004 Peter Nehrer and Composent, Inc.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Peter Nehrer - initial API and implementation
 *******************************************************************************/
package org.eclipse.ecf.sdo.emf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.sdo.ISharedDataGraph;
import org.eclipse.ecf.sdo.IUpdateProvider;
import org.eclipse.ecf.sdo.SDOPlugin;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.sdo.EChangeSummary;
import org.eclipse.emf.ecore.sdo.EDataGraph;
import org.eclipse.emf.ecore.sdo.util.SDOUtil;

import commonj.sdo.DataGraph;

/**
 * Update provider capable of handling EMF-based SDO data graphs.
 * 
 * @author pnehrer
 */
public class EMFUpdateProvider implements IUpdateProvider {

    public static final String TRACE_TAG = "EMFUpdateProvider";

    private EDataGraph clone(EDataGraph source) throws IOException {
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        source.getDataGraphResource().save(buf, null);
        return SDOUtil.loadDataGraph(
                new ByteArrayInputStream(buf.toByteArray()), null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.sdo.IUpdateProvider#createUpdate(org.eclipse.ecf.sdo.ISharedDataGraph)
     */
    public byte[] createUpdate(ISharedDataGraph graph) throws ECFException {
        EDataGraph clone;
        try {
            clone = clone((EDataGraph) graph.getDataGraph());
        } catch (IOException e) {
            throw new ECFException(e);
        }

        EChangeSummary changes = (EChangeSummary) clone.getChangeSummary();
        changes.applyAndReverse();

        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        try {
            changes.eResource().save(buf, null);
            if (SDOPlugin.isTracing(TRACE_TAG)) {
                SDOPlugin.getTraceLog().println("commit:");
                changes.eResource().save(SDOPlugin.getTraceLog(), null);
            }
        } catch (IOException e) {
            throw new ECFException(e);
        }

        return buf.toByteArray();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.sdo.IUpdateProvider#applyUpdate(org.eclipse.ecf.sdo.ISharedDataGraph,
     *      byte[])
     */
    public void applyUpdate(ISharedDataGraph graph, Object data)
            throws ECFException {
        EDataGraph dataGraph = (EDataGraph) graph.getDataGraph();
        EChangeSummary changeSummary = (EChangeSummary) dataGraph
                .getChangeSummary();
        changeSummary.endLogging();
        // throw away any local changes
        changeSummary.apply();

        Resource res = changeSummary.eResource();
        res.unload();

        // apply changes from the event
        try {
            res.load(new ByteArrayInputStream((byte[]) data), null);
            if (SDOPlugin.isTracing(TRACE_TAG)) {
                SDOPlugin.getTraceLog().println("processUpdate:");
                res.save(SDOPlugin.getTraceLog(), null);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        changeSummary = (EChangeSummary) res.getContents().get(0);
        dataGraph.setEChangeSummary(changeSummary);
        // leave a change summary showing what has changed
        changeSummary.applyAndReverse();
        changeSummary.resumeLogging();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.sdo.IUpdateProvider#serializeDataGraph(commonj.sdo.DataGraph)
     */
    public Object serializeDataGraph(DataGraph dataGraph) throws IOException {
        EDataGraph clone = clone((EDataGraph) dataGraph);
        EChangeSummary changeSummary = clone.getEChangeSummary();
        if (changeSummary != null)
            changeSummary.apply();

        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        clone.getDataGraphResource().save(buf, null);
        return buf.toByteArray();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.sdo.IUpdateProvider#deserializeDataGraph(Object)
     */
    public DataGraph deserializeDataGraph(Object data) throws IOException,
            ClassNotFoundException {
        return SDOUtil.loadDataGraph(new ByteArrayInputStream((byte[]) data),
                null);
    }
}

Back to the top