View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
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         // FIXME was brute force - added caching, check that this is ok
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     // FIXME !!! should return same object for same type every time!
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 }