1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package coras.common;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Observable;
30 import java.util.Observer;
31
32 import no.sintef.assetrepository.Asset;
33 import no.sintef.assetrepository.Representation;
34 import no.sintef.lock.LockException;
35 import no.sintef.lock.LockRequiredException;
36 import coras.client.SwingWorker;
37 import coras.representations.RepresentationFactory;
38 import coras.representations.RepresentationTypeEnum;
39
40 /***
41 * @author fvr
42 *
43 * TODO To change the template for this generated type comment go to
44 * Window - Preferences - Java - Code Style - Code Templates
45 */
46 public class CorasAsset extends Observable {
47
48 protected static final String SHORT_DESCRIPTION = "shortDescription";
49
50 private Asset asset = null;
51 private Map children = new HashMap();
52
53 /***
54 * @param asset
55 */
56 protected CorasAsset(Asset asset) {
57 if (asset == null) {
58 throw new IllegalArgumentException("Asset is null");
59 }
60 this.asset = asset;
61 asset.addObserver(new Observer() {
62 public void update(Observable o, Object arg) {
63 setChanged();
64 notifyObservers();
65 }
66 });
67 }
68
69 public boolean isDirty() {
70 return asset.isDirty();
71 }
72
73 public boolean isDeleted() {
74 return asset.isDeleted();
75 }
76
77 /***
78 * @return
79 * @throws LockException
80 */
81 public void markAsDeleted() throws LockException {
82 asset.markAsDeleted();
83 }
84
85 public String getId() {
86 return asset.getId();
87 }
88
89 public String getName() {
90 return asset.getName();
91 }
92 public void setName(String newName) throws LockRequiredException {
93 asset.setName(newName);
94 }
95
96 public String getFullDescription() {
97 return asset.getDescription();
98 }
99 public void setFullDescription(String newDescription) throws LockRequiredException {
100 asset.setDescription(newDescription);
101 }
102
103 public String getShortDescription() {
104 return asset.getProperty(SHORT_DESCRIPTION);
105 }
106 public void setShortDescription(String newDescription) throws LockRequiredException {
107 asset.setProperty(SHORT_DESCRIPTION, newDescription);
108 }
109
110
111 public void checkOut() throws LockException {
112 asset.checkOut();
113 }
114 public void checkIn(String annotation) throws LockException {
115 asset.checkIn(annotation);
116 }
117 public void unCheckOut() throws LockException {
118 asset.unCheckOut();
119 }
120
121 protected String getProperty(String key) {
122 return asset.getProperty(key);
123 }
124 protected void setProperty(String key, String value) throws LockRequiredException {
125 asset.setProperty(key, value);
126 }
127
128 protected Collection getChildAssets(SwingWorker worker) {
129
130 Collection result = new ArrayList();
131 Collection childIds = asset.getChildIds();
132 if (worker != null) {
133 worker.setTaskLength(childIds.size());
134 worker.setProgress(0);
135 }
136 int count = 0;
137 for (Iterator i = childIds.iterator(); i.hasNext();) {
138 String id = (String) i.next();
139 Asset child = (Asset) children.get(id);
140 if (child == null) {
141 child = Asset.getAssetById(id);
142 children.put(id, child);
143 }
144 result.add(child);
145 if (worker != null) {
146 worker.setProgress(++count);
147 }
148 }
149 return result;
150 }
151 protected Asset createChildAsset(AssetTypeEnum assetType, String name, String description, Map properties, String annotation, Representation[] representations)
152 throws LockException {
153 return asset.createChildAsset(assetType.toString(), name, description, properties, annotation, representations);
154 }
155
156
157 protected CorasRepresentation getRepresentation(RepresentationTypeEnum type) {
158 if (type == null)
159 return null;
160 Representation[] representations = asset.getRepresentations();
161 for (Iterator i = Arrays.asList(representations).iterator(); i.hasNext();) {
162 Representation representation = (Representation) i.next();
163 if (type.toString().equals(representation.getType())) {
164 return RepresentationFactory.createRepresentation(representation);
165 }
166 }
167 return null;
168 }
169 protected void addRepresentation(CorasRepresentation representation) throws LockRequiredException {
170 if (representation != null) {
171 asset.addRepresentation(representation.getRepresentation());
172 }
173 }
174 protected void removeRepresentation(CorasRepresentation representation) throws LockRequiredException {
175 if (representation != null) {
176 asset.removeRepresentation(representation.getRepresentation());
177 }
178 }
179
180 }