View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  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 no.sintef.assetrepository.session;
22  
23  import java.util.ArrayList;
24  import java.util.Calendar;
25  import java.util.Collection;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  
29  import javax.ejb.CreateException;
30  import javax.ejb.FinderException;
31  import javax.ejb.SessionBean;
32  
33  import no.sintef.assetrepository.RepositoryException;
34  import no.sintef.assetrepository.VersionHistory;
35  import no.sintef.assetrepository.entity.AssetTypeLocalHome;
36  import no.sintef.assetrepository.interfaces.AssetRepresentationLocal;
37  import no.sintef.assetrepository.interfaces.AssetRepresentationPK;
38  import no.sintef.assetrepository.interfaces.AssetRepresentationUtil;
39  import no.sintef.assetrepository.interfaces.AssetTypeLocal;
40  import no.sintef.assetrepository.interfaces.AssetTypePK;
41  import no.sintef.assetrepository.interfaces.AssetTypeUtil;
42  import no.sintef.assetrepository.interfaces.AssetTypeValue;
43  import no.sintef.assetrepository.interfaces.AssetVersionLocal;
44  import no.sintef.assetrepository.interfaces.AssetVersionPK;
45  import no.sintef.assetrepository.interfaces.AssetVersionUtil;
46  import no.sintef.assetrepository.interfaces.AssetVersionValue;
47  import no.sintef.assetrepository.interfaces.VersionedAssetLocal;
48  import no.sintef.assetrepository.interfaces.VersionedAssetPK;
49  import no.sintef.assetrepository.interfaces.VersionedAssetUtil;
50  import no.sintef.assetrepository.interfaces.VersionedAssetValue;
51  import no.sintef.lock.AlreadyLockedException;
52  import no.sintef.lock.LockException;
53  import no.sintef.lock.LockRequiredException;
54  import no.sintef.lock.LockedByOtherUserException;
55  
56  import org.apache.log4j.Logger;
57  
58  /***
59   * AssetServices Bean User Implementation
60   * 
61   * @ejb.bean name="AssetServices" 
62   *           display-name="AssetServicesEJB"
63   *           view-type="remote" 
64   *           type="Stateless" 
65   *           transaction-type="Container"
66   *           jndi-name="ejb/AssetServices"
67   * 
68   * @ejb.transaction type="Required"
69   * 
70   * @jboss.container-configuration name="Standard Stateless SessionBean"
71   *  
72   */
73  
74  public class AssetServicesEJBImpl extends AssetServicesEJBAbstract implements
75          SessionBean {
76  
77      private static final Logger LOGGER = Logger.getLogger(AssetServicesEJBAbstract.class);
78      private static final String ADMINISTRATOR_ROLE = "administrator";
79  
80      /***
81       * Operation getAssetsByType - User implementation
82       * @throws RepositoryException
83       */
84      public Collection getAssetsByType(String typeName) throws RepositoryException {
85          Collection result = new ArrayList(); 
86          try {
87              AssetTypeLocal type = AssetTypeUtil.getLocalHome().findByName(typeName);
88              Collection assets = VersionedAssetUtil.getLocalHome().findByType((AssetTypePK) type.getPrimaryKey());
89              for (Iterator i = assets.iterator(); i.hasNext();) {
90                  VersionedAssetLocal asset = (VersionedAssetLocal) i.next();
91                  result.add(asset.getVersionedAssetValue());
92              }
93          } catch (FinderException e) { // Note: findByType throws FinderException if and only if findByName does
94              throw new RepositoryException("No asset type " + typeName); 
95          } catch (Exception e) {
96              LOGGER.error("Server error: " + e.getMessage(), e);
97              throw new RepositoryException("Server error: " + e.getMessage(), e);
98          }
99          return result;
100     }
101 
102     /***
103      * Operation createAsset - User implementation
104      * @throws LockException
105      */
106     public VersionedAssetPK createVersionedAsset(String type,
107             no.sintef.assetrepository.interfaces.VersionedAssetPK parentPk,
108             no.sintef.assetrepository.interfaces.AssetVersionValue versionContent) throws LockException, RepositoryException {
109         if (type == null || versionContent == null) {
110             return null;
111         }
112         try {
113             /* Assert that assetType, parent and representations exists */
114             AssetTypeLocal assetType;
115             AssetVersionLocal parentVersion = null;
116             
117             assetType = AssetTypeUtil.getLocalHome().findByName(type);
118             if (parentPk != null) {
119                 VersionedAssetLocal parent = VersionedAssetUtil.getLocalHome().findByPrimaryKey(parentPk);
120                 String checkedOutBy = parent.getCheckedOutBy();
121                 if (checkedOutBy == null) {
122                     throw new LockRequiredException("Parent asset must be checked out before creating child asset");
123                 } else if (!checkedOutBy.equals(getUsername())) {
124                     throw new LockedByOtherUserException(checkedOutBy, parent.getCheckedOutDate());
125                 }
126                 parentVersion = AssetVersionUtil.getLocalHome().findByPrimaryKey(parent.getBaseVersion());
127             }
128             for (Iterator i = versionContent.getRepresentations().iterator(); i.hasNext();) {
129                 AssetRepresentationPK representationPk = (AssetRepresentationPK) i.next();
130                 AssetRepresentationUtil.getLocalHome().findByPrimaryKey(representationPk);
131             }
132 
133             /* Parent must be checked out beforehand */
134 
135             VersionedAssetPK assetPk = new VersionedAssetPK(VersionedAssetUtil.generateGUID(this));
136             AssetVersionPK versionPk = new AssetVersionPK(AssetVersionUtil.generateGUID(this));
137             
138             versionContent.setPrimaryKey(versionPk);
139             versionContent.setCreationDate(Calendar.getInstance());
140             versionContent.setOwner(getUsername());
141             if (versionContent.getProperties() == null) { versionContent.setProperties(new HashMap()); }
142             versionContent.setAsset(assetPk);
143             versionContent.setPreviousVersions(new ArrayList());
144             versionContent.setNextVersions(new ArrayList());
145             versionContent.setAssetType((AssetTypePK) assetType.getPrimaryKey());
146             versionContent.setParent(parentPk);
147             versionContent.setChilds(new ArrayList());
148             if (versionContent.getRepresentations() == null) { versionContent.setRepresentations(new ArrayList()); }
149             
150             VersionedAssetValue assetContent = new VersionedAssetValue();
151             assetContent.setPrimaryKey(assetPk);
152             assetContent.setCreationDate(versionContent.getCreationDate());
153             VersionHistory history = new VersionHistory();
154             history.setRootVersion(versionPk);
155             history.addVersion(versionPk);
156             assetContent.setVersionHistory(history);
157             assetContent.setName(versionContent.getName());
158             assetContent.setDescription(versionContent.getDescription());
159             assetContent.setOwner(versionContent.getOwner());
160             assetContent.setProperties(new HashMap(versionContent.getProperties()));
161             assetContent.setBaseVersion(versionPk);
162             assetContent.setPreviousVersions(new ArrayList());
163             assetContent.setAssetType(versionContent.getAssetType());
164             assetContent.setParent(versionContent.getParent());
165             assetContent.setChilds(new ArrayList());
166             assetContent.setRepresentations(new ArrayList(versionContent.getRepresentations()));
167             
168             AssetVersionLocal version = AssetVersionUtil.getLocalHome().create(versionContent);
169             VersionedAssetLocal asset = VersionedAssetUtil.getLocalHome().create(assetContent);
170 
171             if (parentVersion != null) {
172         	    AssetVersionValue parentContent = parentVersion.getAssetVersionValue();
173                 parentContent.getChilds().add(assetPk);
174                 
175                 /* Check in changes to parent */
176                 checkIn(parentPk, parentContent);
177             }
178             return assetPk;
179         } catch (LockException e) {
180             throw e;
181         } catch (FinderException e) {
182             throw new RepositoryException("Error creating asset: " + e.getMessage(), e);
183         } catch (CreateException e) {
184             throw new RepositoryException("Error creating asset: " + e.getMessage(), e);
185         } catch (Exception e) {
186             LOGGER.error("Server error: " + e.getMessage(), e);
187             throw new RepositoryException("Server error: " + e.getMessage(), e);
188         }
189     }
190 	
191     public no.sintef.assetrepository.interfaces.AssetRepresentationPK createAssetRepresentation (no.sintef.assetrepository.interfaces.AssetRepresentationValue content) throws RepositoryException 
192     {
193         if (content == null) {
194             return null;
195         }
196         content.setId(AssetRepresentationUtil.generateGUID(this));
197         try {
198             AssetRepresentationLocal representation = AssetRepresentationUtil.getLocalHome().create(content);
199             return (AssetRepresentationPK) representation.getPrimaryKey();
200         } catch (CreateException e) {
201             throw new RepositoryException("Error creating asset representation: " + e.getMessage(), e);
202         } catch (Exception e) {
203             LOGGER.error("Server error: " + e.getMessage(), e);
204             throw new RepositoryException("Server error: " + e.getMessage(), e);
205         }
206     }
207   		
208 
209     /***
210      *  Operation getAssetTypeByName - User implementation 
211      * @throws RepositoryException
212      */
213     public no.sintef.assetrepository.interfaces.AssetTypeValue getAssetTypeByName(String name) throws RepositoryException 
214     {
215         try {
216             AssetTypeLocal assetType = AssetTypeUtil.getLocalHome().findByName(name);
217             return assetType.getAssetTypeValue();
218         } catch (FinderException e) {
219             return null; // No asset type with that name
220         } catch (Exception e) {
221             LOGGER.error("Server error: " + e.getMessage(), e);
222             throw new RepositoryException("Server error: " + e.getMessage(), e);
223         }
224     }
225 	
226     /***
227      *  Operation createAssetType - User implementation 
228      * @throws RepositoryException
229      */
230     public no.sintef.assetrepository.interfaces.AssetTypePK createAssetType(String name, String description) throws RepositoryException 
231     {
232         try {
233             AssetTypeLocalHome home = AssetTypeUtil.getLocalHome();
234             try {
235                 home.findByName(name);
236                 // AssetType with that name already exists, throw exception
237                 throw new RepositoryException("Asset type " + name + " already exists");
238             } catch (FinderException e) {
239                 AssetTypeValue assetTypeValue = new AssetTypeValue();
240                 assetTypeValue.setId(AssetTypeUtil.generateGUID(this));
241                 assetTypeValue.setName(name);
242                 assetTypeValue.setDescription(description);
243                 AssetTypeLocal assetType = home.create(assetTypeValue);
244                 return (AssetTypePK) assetType.getPrimaryKey();
245             }
246         } catch (RepositoryException e) {
247             throw e;
248         } catch (CreateException e) {
249             throw new RepositoryException("Error creating asset type " + name + ": " + e.getMessage(), e);
250         } catch (Exception e) {
251             LOGGER.error("Server error: " + e.getMessage(), e);
252             throw new RepositoryException("Server error: " + e.getMessage(), e);
253         }
254     }
255 
256     /***
257      * Operation checkOut - User implementation
258      * @throws LockException
259      * @throws RepositoryException
260      */
261     public void checkOut(no.sintef.assetrepository.interfaces.VersionedAssetPK assetPk) throws LockException, RepositoryException {
262         try {
263             VersionedAssetLocal asset = VersionedAssetUtil.getLocalHome().findByPrimaryKey(assetPk);
264             String checkedOutBy = asset.getCheckedOutBy();
265             String username = getUsername();
266             if (checkedOutBy == null) {
267                 asset.setCheckedOutBy(username);
268                 asset.setCheckedOutDate(Calendar.getInstance());
269             } else if (checkedOutBy.equals(username)) {
270                 throw new AlreadyLockedException(asset.getCheckedOutDate());
271             } else {
272         		throw new LockedByOtherUserException(checkedOutBy, asset.getCheckedOutDate());
273             } 
274         } catch (LockException e) {
275             throw e;
276         } catch (FinderException e) {
277             throw new RepositoryException("No asset with id " + (assetPk != null ? assetPk.getId() : "null"), e);
278         } catch (Exception e) {
279             LOGGER.error("Server error: " + e.getMessage(), e);
280             throw new RepositoryException("Server error: " + e.getMessage(), e);
281         }
282     }
283 
284     /***
285      * Operation checkIn - User implementation
286      * @throws CreateException
287      * @throws LockException
288      * @throws RepositoryException
289      */
290     public void checkIn(no.sintef.assetrepository.interfaces.VersionedAssetPK assetPk,
291             no.sintef.assetrepository.interfaces.AssetVersionValue newContent) throws LockException, RepositoryException {
292         try {
293             VersionedAssetLocal asset = VersionedAssetUtil.getLocalHome().findByPrimaryKey(assetPk);
294             String checkedOutBy = asset.getCheckedOutBy();
295             String username = getUsername();
296             if (checkedOutBy == null) {
297                 throw new LockRequiredException("Asset must be checked out before checking in new version");
298             } else if (!checkedOutBy.equals(username)) {
299         		throw new LockedByOtherUserException(checkedOutBy, asset.getCheckedOutDate());
300             } else {
301                 AssetVersionLocal oldVersion = AssetVersionUtil.getLocalHome().findByPrimaryKey(asset.getBaseVersion());
302                 /*
303                 if (!equals(oldVersion.getOwner(), newContent.getOwner())) {
304                     newContent.setOwner(oldVersion.getOwner());
305                 }
306                 */
307 
308                 if (newContent.getAssetType() != null && !newContent.getAssetType().equals(asset.getAssetType())) {
309                     System.err.println("Asset cannot change type!");
310                 }
311                 AssetVersionPK newVersionPk = new AssetVersionPK(AssetVersionUtil.generateGUID(this));
312                 newContent.setPrimaryKey(newVersionPk);
313                 newContent.setAssetType(asset.getAssetType());
314                 newContent.setCreationDate(Calendar.getInstance());
315                 if (newContent.getProperties() == null) { newContent.setProperties(new HashMap()); }
316                 if (newContent.getRepresentations() == null) { newContent.setRepresentations(new ArrayList()); }
317                 if (newContent.getChilds() == null) { newContent.setChilds(new ArrayList()); }
318                 newContent.setNextVersions(new ArrayList());
319                 newContent.setPreviousVersions(new ArrayList());
320                 newContent.getPreviousVersions().add(oldVersion.getPrimaryKey());
321                 newContent.setAsset(assetPk);
322 
323                 AssetVersionLocal newVersion = AssetVersionUtil.getLocalHome().create(newContent);
324                 
325                 VersionedAssetValue assetContent = asset.getVersionedAssetValue();
326                 assetContent.setName(newVersion.getName());
327                 assetContent.setDescription(newVersion.getDescription());
328                 assetContent.setOwner(newVersion.getOwner());
329                 assetContent.setProperties(new HashMap(newVersion.getProperties()));
330                 assetContent.setBaseVersion(newVersionPk);
331                 assetContent.setPreviousVersions(new ArrayList(newContent.getPreviousVersions()));
332                 assetContent.setParent(newContent.getParent());
333                 assetContent.setChilds(newVersion.getChilds());
334                 assetContent.setRepresentations(new ArrayList(newVersion.getRepresentations()));
335 
336                 assetContent.getVersionHistory().addVersion(newVersionPk);
337                 assetContent.setCheckedOutBy(null);
338                 assetContent.setCheckedOutDate(null);
339                 
340                 asset.setVersionedAssetValue(assetContent);
341                 oldVersion.getNextVersions().add(newVersionPk);
342             } 
343         } catch (LockException e) {
344             throw e;
345         } catch (FinderException e) {
346             throw new RepositoryException("Unable to load asset with id " + (assetPk != null ? assetPk.getId() : "null") + ": " + e.getMessage(), e);
347         } catch (CreateException e) {
348             throw new RepositoryException("Unable to create new version for asset with id " + (assetPk != null ? assetPk.getId() : "null") + ": " + e.getMessage(), e);
349         } catch (Exception e) {
350             LOGGER.error("Server error: " + e.getMessage(), e);
351             throw new RepositoryException("Server error: " + e.getMessage(), e);
352         }
353     }
354 
355     /***
356      * Operation unCheckOut - User implementation
357      * @throws LockException
358      * @throws RepositoryException
359      */
360     public void unCheckOut(no.sintef.assetrepository.interfaces.VersionedAssetPK assetPk) throws LockException, RepositoryException {
361         try {
362             VersionedAssetLocal asset = VersionedAssetUtil.getLocalHome().findByPrimaryKey(assetPk);
363             String checkedOutBy = asset.getCheckedOutBy();
364             if (checkedOutBy == null) {
365                 return;
366             } 
367             String username = getUsername();
368             if (checkedOutBy.equals(username) || _sessionContext.isCallerInRole(ADMINISTRATOR_ROLE)) {
369                 asset.setCheckedOutBy(null);
370                 asset.setCheckedOutDate(null);
371             } else {
372                 throw new LockedByOtherUserException(checkedOutBy, asset.getCheckedOutDate());
373             }
374         } catch (LockException e) {
375             throw e;
376         } catch (FinderException e) {
377             throw new RepositoryException("No asset with id " + (assetPk != null ? assetPk.getId() : "null"), e);
378         } catch (Exception e) {
379             LOGGER.error("Server error: " + e.getMessage(), e);
380             throw new RepositoryException("Server error: " + e.getMessage(), e);
381         }
382     }
383 
384     /***
385      * Operation makeParent - User implementation
386      */
387     public void makeParent(no.sintef.assetrepository.interfaces.VersionedAssetPK parent,
388             no.sintef.assetrepository.interfaces.VersionedAssetPK child) {
389         // 
390         // MUST BE IMPLENTED
391         // 
392         System.out.println(getClass().getName()
393                 + " makeParent - Method not implemented by developer.");
394         return;
395     }
396 
397     /***
398      * Operation markAssetAsDeleted - User implementation
399      * @throws RepositoryException
400      * @throws LockException
401      */
402     public void markAssetAsDeleted(no.sintef.assetrepository.interfaces.VersionedAssetPK assetPk) throws RepositoryException, LockException {
403         try {
404             VersionedAssetLocal asset = VersionedAssetUtil.getLocalHome().findByPrimaryKey(assetPk);
405             String username = getUsername();
406             String checkedOutBy = asset.getCheckedOutBy();
407             if (checkedOutBy == null) {
408             	throw new LockRequiredException("An asset must be checked out before it can be deleted (attempting to delete asset " + assetPk + ")");
409             } else if (checkedOutBy.equals(username) || _sessionContext.isCallerInRole(ADMINISTRATOR_ROLE)) {
410                 asset.setDeletionDate(Calendar.getInstance());
411                 asset.setCheckedOutBy(null);
412                 asset.setCheckedOutDate(null);
413             } else {
414                 throw new LockedByOtherUserException(checkedOutBy, asset.getCheckedOutDate());
415             }
416         } catch (LockException e) {
417             throw e;
418         } catch (FinderException e) {
419             throw new RepositoryException("No asset with id " + (assetPk != null ? assetPk.getId() : "null"), e);
420         } catch (Exception e) {
421             LOGGER.error("Server error: " + e.getMessage(), e);
422             throw new RepositoryException("Server error: " + e.getMessage(), e);
423         }
424     }
425 
426     /***
427      * Create the Session Bean
428      * 
429      * @ejb.create-method view-type="remote"
430      * @--ejb.transaction type="Required"
431      * @ejb.permission unchecked="true"
432      */
433     public void ejbCreate() {
434     }
435 
436     /***
437      * @return
438      */
439     private final String getUsername() {
440         return _sessionContext.getCallerPrincipal().getName();
441     }
442 
443     /***
444      * @param owner
445      * @param owner2
446      * @return
447      */
448     private static final boolean equals(String a, String b) {
449         return (a == b) || (a != null && a.equals(b));
450     }
451 
452 }