Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c24f7e50ec07cc681cbe19550739ed254938dad4 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Copyright (c) 2012, 2015, 2016, 2018 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.transfer;

import org.eclipse.net4j.util.ObjectUtil;
import org.eclipse.net4j.util.om.monitor.SubProgressMonitor;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Eike Stepper
 * @since 4.2
 */
class CDOTransferMappingImpl implements CDOTransferMapping
{
  private final CDOTransfer transfer;

  private final CDOTransferElement source;

  private final CDOTransferMappingImpl parent;

  private List<CDOTransferMapping> children;

  private CDOTransferType transferType;

  private IPath relativePath;

  private Status status;

  public CDOTransferMappingImpl(CDOTransfer transfer, CDOTransferElement source, CDOTransferMapping parent, IProgressMonitor monitor)
  {
    this.transfer = transfer;
    this.source = source;
    this.parent = (CDOTransferMappingImpl)parent;
    relativePath = transfer.getPathProvider().getPath(source);

    CDOTransferType transferType = transfer.getTransferType(source);
    this.transferType = transferType;

    if (parent != null)
    {
      this.parent.addChild(this);
    }

    try
    {
      if (isDirectory())
      {
        CDOTransferElement[] children = source.getChildren();
        monitor.beginTask("", 1 + children.length);
        monitor.subTask("Mapping " + source);
        monitor.worked(1);

        for (CDOTransferElement child : children)
        {
          transfer.map(child, this, new SubProgressMonitor(monitor, 1));
        }
      }
      else
      {
        monitor.beginTask("", 1);
        monitor.subTask("Mapping " + source);
        monitor.worked(1);
      }
    }
    finally
    {
      monitor.done();
    }
  }

  public CDOTransferMappingImpl(CDOTransfer transfer)
  {
    this.transfer = transfer;
    source = null;
    parent = null;
    transferType = CDOTransferType.FOLDER;
    relativePath = Path.EMPTY;
  }

  public CDOTransfer getTransfer()
  {
    return transfer;
  }

  public CDOTransferElement getSource()
  {
    return source;
  }

  public CDOTransferMapping getParent()
  {
    return parent;
  }

  public boolean isRoot()
  {
    return parent == null;
  }

  public boolean isDirectory()
  {
    if (source == null)
    {
      return true;
    }

    return source.isDirectory();
  }

  public String getName()
  {
    return relativePath.lastSegment();
  }

  public void setName(String name)
  {
    setRelativePath(relativePath.removeLastSegments(1).append(name));
  }

  public IPath getRelativePath()
  {
    return relativePath;
  }

  public void setRelativePath(IPath path)
  {
    if (!ObjectUtil.equals(relativePath, path))
    {
      IPath oldPath = relativePath;
      relativePath = path;
      unsetStatusRecursively();
      transfer.relativePathChanged(this, oldPath, path);
    }
  }

  public void setRelativePath(String path)
  {
    setRelativePath(new Path(path));
  }

  public void accept(Visitor visitor)
  {
    if (visitor.visit(this) && children != null)
    {
      for (CDOTransferMapping child : children)
      {
        child.accept(visitor);
      }
    }
  }

  public CDOTransferMapping[] getChildren()
  {
    if (children == null || children.isEmpty())
    {
      return NO_CHILDREN;
    }

    CDOTransferMapping[] result = children.toArray(new CDOTransferMapping[children.size()]);
    Arrays.sort(result);
    return result;
  }

  public CDOTransferMapping getChild(IPath path)
  {
    if (path.isEmpty())
    {
      return this;
    }

    String name = path.segment(0);
    for (CDOTransferMapping child : getChildren())
    {
      if (name.equals(child.getName()))
      {
        return child.getChild(path.removeFirstSegments(1));
      }
    }

    return null;
  }

  public CDOTransferMapping getChild(String path)
  {
    return getChild(new Path(path));
  }

  private void addChild(CDOTransferMapping child)
  {
    ensureChildrenList();
    if (!children.contains(child))
    {
      children.add(child);
      transfer.childrenChanged(this, child, CDOTransfer.ChildrenChangedEvent.Kind.MAPPED);
    }
  }

  private void removeChild(CDOTransferMapping child)
  {
    if (children != null && children.remove(child))
    {
      transfer.childrenChanged(this, child, CDOTransfer.ChildrenChangedEvent.Kind.UNMAPPED);
    }
  }

  private void ensureChildrenList()
  {
    if (children == null)
    {
      children = new ArrayList<CDOTransferMapping>();
    }
  }

  public void unmap()
  {
    transfer.unmap(this);
    if (parent != null)
    {
      parent.removeChild(this);
    }
  }

  public CDOTransferType getTransferType()
  {
    return transferType;
  }

  public void setTransferType(CDOTransferType transferType)
  {
    if (!ObjectUtil.equals(this.transferType, transferType))
    {
      CDOTransferType oldType = this.transferType;
      this.transferType = transferType;
      transfer.transferTypeChanged(this, oldType, transferType);
    }
  }

  public IPath getFullPath()
  {
    IPath relativePath = getRelativePath();
    if (isRoot())
    {
      return relativePath;
    }

    IPath path = parent.getFullPath();
    return path.append(relativePath);
  }

  public Status getStatus()
  {
    if (status == null)
    {
      status = calculateStatus();
    }

    return status;
  }

  private Status calculateStatus()
  {
    if (parent != null)
    {
      Status status = parent.getStatus();
      if (status != Status.MERGE)
      {
        return status;
      }
    }

    CDOTransferSystem targetSystem = transfer.getTargetSystem();

    IPath fullPath = getFullPath();
    int lastSegment = fullPath.segmentCount();
    for (int i = 1; i <= lastSegment; i++)
    {
      IPath path = fullPath.uptoSegment(i);
      CDOTransferElement target = targetSystem.getElement(path);
      if (target == null)
      {
        return Status.NEW;
      }

      boolean sourceDirectory = i == lastSegment ? isDirectory() : true;
      boolean targetDirectory = target.isDirectory();
      if (!(sourceDirectory && targetDirectory))
      {
        return Status.CONFLICT;
      }
    }

    return Status.MERGE;
  }

  private void unsetStatusRecursively()
  {
    if (status != null)
    {
      status = null;
      if (children != null && !children.isEmpty())
      {
        for (CDOTransferMapping child : children)
        {
          ((CDOTransferMappingImpl)child).unsetStatusRecursively();
        }
      }
    }
  }

  public CDOTransferElement getTarget()
  {
    CDOTransferSystem targetSystem = transfer.getTargetSystem();
    IPath fullPath = getFullPath();
    return targetSystem.getElement(fullPath);
  }

  public int compareTo(CDOTransferMapping o)
  {
    boolean directory = isDirectory();
    boolean oDirectory = o.isDirectory();
    if (directory != oDirectory)
    {
      return directory ? -1 : 1;
    }

    return getName().compareTo(o.getName());
  }

  @Override
  public String toString()
  {
    return getFullPath().toString();
  }
}

Back to the top