KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > dav > server > managers > VersionedNamespaceManager


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.dav.server.managers;
21
22 import java.net.*;
23 import java.util.*;
24 import java.util.logging.*;
25
26 import org.openharmonise.dav.server.apm.APMException;
27 import org.openharmonise.dav.server.utils.*;
28 import org.openharmonise.rm.*;
29 import org.openharmonise.rm.factory.*;
30 import org.openharmonise.rm.metadata.*;
31 import org.openharmonise.rm.resources.*;
32 import org.openharmonise.rm.resources.lifecycle.*;
33 import org.openharmonise.rm.resources.metadata.properties.*;
34 import org.openharmonise.rm.resources.users.User;
35
36 import com.ibm.webdav.*;
37 import com.ibm.webdav.impl.ResourceImpl;
38 import com.ibm.webdav.protocol.http.*;
39
40 /**
41  * Implementation of the <code>VersionedNamespaceManager</code> interface
42  * providing the Delta-V functionality on top of <code>NamespaceManager</code>.
43  *
44  * @author Michael Bell
45  * @version $Revision: 1.3 $
46  *
47  */

48 public class VersionedNamespaceManager
49     extends HarmoniseNamespaceManager
50     implements com.ibm.webdav.impl.VersionedNamespaceManager {
51
52     static private Logger m_logger = Logger.getLogger(VersionedNamespaceManager.class.getName());
53     
54     /**
55      *
56      */

57     public VersionedNamespaceManager() {
58         super();
59     }
60
61     /**
62      * @param resource
63      */

64     public VersionedNamespaceManager(ResourceImpl resource) {
65         super(resource);
66     }
67     
68
69     /**
70      * Checkin this resource, i.e. make live/approved
71      *
72      * @return
73      * @throws WebDAVException
74      */

75     public String JavaDoc checkin() throws WebDAVException {
76         String JavaDoc versionURL = null;
77
78         try {
79             if (m_child.isPendingVersion() == false) {
80                 throw new WebDAVException(
81                     WebDAVStatus.SC_FORBIDDEN,
82                     "Resource needs to be checked out");
83             }
84
85             addCreator();
86
87             addPublicationDate();
88
89             User usr =
90                 ((HarmoniseSessionManager) m_resource.getUserAuthenticator())
91                     .getUser(m_resource);
92
93             //ensure change status is allowed
94
if (m_auxillary != null
95                 && m_auxillary.isChangeStatusValid(usr, m_child) == false) {
96
97                 throw new WebDAVException(
98                     WebDAVStatus.SC_FORBIDDEN,
99                     "Object workflow not complete");
100
101             }
102
103             m_child =
104                 CommandWrapper.changeStatus(
105                     m_dsi,
106                     m_child,
107                     usr,
108                     Status.APPROVED);
109
110             versionURL = HarmoniseNameResolver.getVersionPath(m_child);
111
112             if (m_auxillary != null) {
113                 m_auxillary.changeStatus(
114                     usr,
115                     m_child,
116                     Status.UNAPPROVED,
117                     Status.APPROVED);
118             }
119
120         } catch (NameResolverException e) {
121             throw new WebDAVException(
122                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
123                 e.getLocalizedMessage());
124         } catch (DataAccessException e) {
125             throw new WebDAVException(
126                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
127                 e.getLocalizedMessage());
128         } catch (APMException e) {
129             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
130             throw new WebDAVException(
131                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
132                 e.getLocalizedMessage());
133         }
134         return versionURL;
135     }
136     
137     /**
138      * Adds a 'creator-displayname' property to this resource.
139      *
140      * @throws WebDAVException
141      */

142     private void addCreator() throws WebDAVException {
143
144         try {
145             if (m_child.isPendingVersion() == true
146                 && (m_child instanceof Property) == false) {
147
148                 Profile prof = m_child.getProfile();
149
150                 if (prof == null) {
151                     prof = new Profile(m_dsi);
152                     prof.setName("default");
153                     m_child.setProfile(prof);
154                 }
155
156                 Property creatorProp =
157                     PropertyFactory.getPropertyFromName(
158                         m_dsi,
159                         VersionedPropertiesManager.TAG_CREATOR_DISPLAYNAME);
160
161                 if (prof.isValidProperty(creatorProp) == true) {
162
163                     prof.removeProperty(creatorProp);
164
165                     GeneralPropertyInstance propInst =
166                         new GeneralPropertyInstance(m_dsi, creatorProp);
167
168                     User usr =
169                         ((HarmoniseSessionManager) m_resource
170                             .getUserAuthenticator())
171                             .getUser(m_resource);
172
173                     propInst.addValue(usr.getName());
174
175                     prof.addPropertyInstance(propInst);
176
177                     m_child = (AbstractChildObject) m_child.save();
178                 }
179             }
180
181         } catch (DataAccessException e) {
182             throw new WebDAVException(
183                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
184                 e.getLocalizedMessage());
185         } catch (HarmoniseFactoryException e) {
186             throw new WebDAVException(
187                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
188                 e.getLocalizedMessage());
189         } catch (ProfileException e) {
190             throw new WebDAVException(
191                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
192                 e.getLocalizedMessage());
193         } catch (InvalidPropertyValueException e) {
194             throw new WebDAVException(
195                 WebDAVStatus.SC_FORBIDDEN,
196                 e.getLocalizedMessage());
197         } catch (EditException e) {
198             throw new WebDAVException(
199                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
200                 e.getLocalizedMessage());
201         } catch (InvalidProfileException e) {
202             throw new WebDAVException(
203                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
204                 e.getLocalizedMessage());
205         } catch (InvalidNameException e) {
206             throw new WebDAVException(
207                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
208                 e.getLocalizedMessage());
209         } catch (PopulateException e) {
210             throw new WebDAVException(
211                     WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
212                     e.getLocalizedMessage());
213         }
214     }
215     
216     /**
217      * Add START property.
218      */

219     private void addPublicationDate() throws WebDAVException {
220
221         try {
222             if (m_child.isPendingVersion() == true
223                 && (m_child instanceof AbstractParentObject) == false) {
224
225                 Profile prof = m_child.getProfile();
226
227                 if (prof == null) {
228                     prof = new Profile(m_dsi);
229                     prof.setName("default");
230                     m_child.setProfile(prof);
231                 }
232
233                 Property prop =
234                     PropertyFactory.getPropertyFromName(
235                         m_dsi,
236                         HarmoniseNameResolver.START_PROP_NAME);
237
238                 if (prof.isValidProperty(prop) == true) {
239                     GeneralPropertyInstance propInst =
240                         (GeneralPropertyInstance) prof.getPropertyInstance(
241                             prop);
242
243                     if (propInst == null) {
244                         propInst =
245                             (
246                                 GeneralPropertyInstance) PropertyInstanceFactory
247                                     .getPropertyInstance(
248                                 m_dsi,
249                                 prop);
250                         prof.addPropertyInstance(propInst);
251                     }
252
253                     propInst.clearValues();
254
255                     Date date = new Date();
256
257                     propInst.addValue(date);
258
259                     m_child = (AbstractChildObject) m_child.save();
260                 }
261
262             }
263         } catch (InvalidPropertyInstanceException e) {
264             throw new WebDAVException(
265                 WebDAVStatus.SC_FORBIDDEN,
266                 e.getLocalizedMessage());
267         } catch (DataAccessException e) {
268             throw new WebDAVException(
269                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
270                 e.getLocalizedMessage());
271         } catch (InvalidProfileException e) {
272             throw new WebDAVException(
273                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
274                 e.getLocalizedMessage());
275         } catch (HarmoniseFactoryException e) {
276             throw new WebDAVException(
277                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
278                 e.getLocalizedMessage());
279         } catch (ProfileException e) {
280             throw new WebDAVException(
281                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
282                 e.getLocalizedMessage());
283         } catch (InvalidPropertyValueException e) {
284             throw new WebDAVException(
285                 WebDAVStatus.SC_FORBIDDEN,
286                 e.getLocalizedMessage());
287         } catch (EditException e) {
288             throw new WebDAVException(
289                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
290                 e.getLocalizedMessage());
291         } catch (InvalidNameException e) {
292             throw new WebDAVException(
293                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
294                 e.getLocalizedMessage());
295         } catch (PopulateException e) {
296             throw new WebDAVException(
297                     WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
298                     e.getLocalizedMessage());
299         }
300
301     }
302
303
304     /**
305      * Checkout this resource, i.e. create a new pending version.
306      *
307      * @throws WebDAVException
308      */

309     public void checkout() throws WebDAVException {
310         try {
311             if (m_child.isLiveVersion() == true) {
312                 m_child = (AbstractChildObject) m_child.createNewVersion();
313             } else if (m_child.isHistorical() == true) {
314                 AbstractChildObject liveChild =
315                     (AbstractChildObject) m_child.getLiveVersion();
316                 if (liveChild != null
317                     && liveChild.getPendingVersions().size() > 0) {
318                     throw new WebDAVException(
319                         WebDAVStatus.SC_FORBIDDEN,
320                         "Can only have once checked out resource at a time");
321                 }
322
323                 User usr =
324                     ((HarmoniseSessionManager) m_resource.getUserAuthenticator())
325                         .getUser(m_resource);
326                 
327                 //first need to check that there isn't already a
328
//resource with the same name in the live collection
329
String JavaDoc sHistPath = m_child.getPath();
330                 
331                 AbstractParentObject parent = (AbstractParentObject) HarmoniseObjectFactory.instantiateHarmoniseObject(m_dsi, m_child.getParentObjectClassName(), sHistPath);
332                 if (parent == null) {
333                     m_logger.log(Level.WARNING, "Unable to instantiate " + m_child.getParentObjectClassName() + " in path " + sHistPath);
334                     throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Missing Parent Object");
335                 }
336                 AbstractChildObject sameNameChild = parent.getChildByName(m_child.getName());
337                 if(sameNameChild == null || sameNameChild.equals(m_child.getLiveVersion())) {
338                     //no child with name exists in live collection
339
//or the child that does exist is the current live version so reactivate
340
m_child = CommandWrapper.reactivate(m_dsi, m_child, usr);
341                 } else {
342                     //child with same name exists in live collection
343
if(m_logger.isLoggable(Level.INFO)) {
344                         m_logger.logp(Level.INFO, VersionedNamespaceManager.class.getName(),"checkout","throwing PRECONDICTION_FAILED on checkout method (reactivate) - Resource " + m_child.getName() + " with same name already exists in live collecttion");
345                     }
346                     throw new WebDAVException(WebDAVStatus.SC_PRECONDITION_FAILED,"Resource with the same name exists in the live collection");
347                 }
348                 
349                 
350             } else if (m_child.isPendingVersion() == true) {
351                 if(m_logger.isLoggable(Level.INFO)) {
352                     m_logger.logp(Level.INFO, VersionedNamespaceManager.class.getName(),"checkout","throwing FORBIDDEN on checkout method - Resource " + m_child.getName() + " is already checked out");
353                 }
354                 
355                 throw new WebDAVException(
356                     WebDAVStatus.SC_FORBIDDEN,
357                     "Resource is already checked out");
358             }
359
360             m_resource.getResponseContext().location(
361                 HarmoniseNameResolver.getVersionPath(m_child));
362
363         } catch (EditException e) {
364             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
365             throw new WebDAVException(
366                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
367                 e.getLocalizedMessage());
368         } catch (DataAccessException e) {
369             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
370             throw new WebDAVException(
371                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
372                 e.getLocalizedMessage());
373         } catch (NameResolverException e) {
374             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
375             throw new WebDAVException(
376                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
377                 e.getLocalizedMessage());
378         } catch (HarmoniseFactoryException e) {
379             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
380             throw new WebDAVException(
381                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
382                 e.getLocalizedMessage());
383         }
384     }
385     
386     /**
387      * Returns a list of allowed DeltaV methods for this resource.
388      *
389      * @return
390      */

391     public List getAllowedMethods() throws WebDAVException {
392         List allowedMethods = super.getAllowedMethods();
393
394         if (m_child != null) {
395             if (HarmoniseNameResolver.isArchiveURL(m_resource.getURL()) == true
396                     || (m_child != null && m_child.isHistorical())) {
397                 try {
398                     AbstractChildObject liveChild =
399                         (AbstractChildObject) m_child.getLiveVersion();
400                     if ((m_child instanceof AbstractParentObject) == false) {
401                         if (liveChild == null
402                             || liveChild.getPendingVersions().size() == 0) {
403                             allowedMethods.add(CheckOutMethod.METHOD_NAME);
404                         }
405                     }
406                 } catch (DataAccessException e) {
407                     throw new WebDAVException(
408                         WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
409                         e.getLocalizedMessage());
410                 }
411             } else {
412                 List versionMethods = new ArrayList();
413                 try {
414                     if (m_child.isPendingVersion() == true) {
415                         if (m_child.getLiveVersion() == null) {
416                             versionMethods.add(VersionControlMethod.METHOD_NAME);
417                         } else {
418                             versionMethods.add(CheckInMethod.METHOD_NAME);
419                             versionMethods.add(UncheckOutMethod.METHOD_NAME);
420                             versionMethods.add(ReportMethod.METHOD_NAME);
421                         }
422     
423                     } else {
424                         if (m_child.isHistorical() == true) {
425                             AbstractChildObject liveChild =
426                                 (AbstractChildObject) m_child.getLiveVersion();
427     
428                             if (liveChild == null
429                                 || liveChild.getPendingVersions().size() == 0) {
430                                     versionMethods.add(CheckOutMethod.METHOD_NAME);
431                             }
432     
433                         } else {
434                             versionMethods.add(CheckOutMethod.METHOD_NAME);
435                         }
436     
437                         versionMethods.add(ReportMethod.METHOD_NAME);
438                     }
439                 } catch (DataAccessException e) {
440                     throw new WebDAVException(
441                         WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
442                         e.getLocalizedMessage());
443                 }
444                 
445                 versionMethods = super.filterAllowedMethods(versionMethods);
446                 
447                 allowedMethods.addAll(versionMethods);
448             
449             }
450         }
451
452         return allowedMethods;
453     }
454     
455     /**
456      * Returns a list of all versions of this resource represented as <code>ResourceImpl</code>.
457      * objects
458      *
459      * @return
460      */

461     public List getVersions() throws WebDAVException {
462         Vector versions;
463         try {
464             versions = new Vector();
465
466             AbstractChildObject live =
467                 (AbstractChildObject) m_child.getLiveVersion();
468
469             ResourceImpl member =
470                 new ResourceImpl(
471                     new URL(HarmoniseNameResolver.getVersionPath(live)),
472                     live.getName());
473             versions.add(member);
474
475             List hists = live.getHistoricalVersions();
476             addVersionResources(versions, hists);
477
478         } catch (DataAccessException e) {
479             throw new WebDAVException(
480                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
481                 e.getLocalizedMessage());
482         } catch (MalformedURLException e) {
483             throw new WebDAVException(
484                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
485                 e.getLocalizedMessage());
486         } catch (NameResolverException e) {
487             throw new WebDAVException(
488                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
489                 e.getLocalizedMessage());
490         }
491
492         return versions;
493     }
494
495     /**
496      * Adds <code>ResourceImpl</code> representations of the child objects held in
497      * <code>childObjs</code> to <code>versionResources</code>.
498      *
499      * @param versionResources
500      * @param childObjs
501      * @throws WebDAVException
502      * @throws MalformedURLException
503      * @throws DataAccessException
504      * @throws NameResolverException
505      */

506     private void addVersionResources(List versionResources, List childObjs)
507         throws
508             WebDAVException,
509             MalformedURLException,
510             DataAccessException,
511             NameResolverException {
512         Iterator iter = childObjs.iterator();
513
514         while (iter.hasNext()) {
515             AbstractChildObject child = (AbstractChildObject) iter.next();
516
517             ResourceImpl member =
518                 new ResourceImpl(
519                     new URL(HarmoniseNameResolver.getVersionPath(child)),
520                     child.getName());
521             versionResources.add(member);
522         }
523     }
524     
525     /**
526      * Returns <code>true</code> if this version is checked in.
527      *
528      * @return
529      */

530     public boolean isCheckedInVersion() throws WebDAVException {
531         boolean bIsVersioned = false;
532
533         try {
534             bIsVersioned = m_child.isLiveVersion();
535         } catch (DataAccessException e) {
536             throw new WebDAVException(
537                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
538                 e.getLocalizedMessage());
539         }
540
541         return bIsVersioned;
542     }
543
544     /**
545      * Returns <code>true</code> of this version is checked out.
546      *
547      * @return
548      */

549     public boolean isCheckedOutVersion() throws WebDAVException {
550         boolean bIsVersioned = false;
551
552         try {
553             bIsVersioned = m_child.isPendingVersion();
554         } catch (DataAccessException e) {
555             throw new WebDAVException(
556                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
557                 e.getLocalizedMessage());
558         }
559
560         return bIsVersioned;
561     }
562     
563     /**
564      * Returns <code>true</code> if this resource is under version control in the
565      * DAV sense, i.e. it has a live Harmonise version
566      *
567      * @return
568      */

569     public boolean isVersioned() throws WebDAVException {
570         boolean bIsVersioned = false;
571
572         if (m_child != null) {
573             try {
574                 bIsVersioned =
575                     (m_child.isHistorical() == true
576                         || m_child.getLiveVersion() != null);
577             } catch (DataAccessException e) {
578                 throw new WebDAVException(
579                     WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
580                     e.getLocalizedMessage());
581             }
582         }
583
584         return bIsVersioned;
585     }
586     
587     /**
588      * Uncheckout this resource. In Harmonise terms, this means getting rid of the pending
589      * version.
590      *
591      * @throws WebDAVException
592      */

593     public void uncheckout() throws WebDAVException {
594         try {
595             if (m_child.isPendingVersion() == true) {
596                 m_child.archive();
597             } else {
598                 throw new WebDAVException(
599                     WebDAVStatus.SC_FORBIDDEN,
600                     "Resource is not checked out");
601             }
602         } catch (DataAccessException e) {
603             throw new WebDAVException(
604                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
605                 e.getLocalizedMessage());
606         } catch (EditException e) {
607             throw new WebDAVException(
608                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
609                 e.getLocalizedMessage());
610         }
611     }
612     
613     /**
614      * Adds this resource to version control, in the DAV sense. In Harmonise terms,
615      * the child object is made live/approved.
616      *
617      */

618     public void versionControl() throws WebDAVException {
619         try {
620             if (m_child.isPendingVersion() == false
621                 || m_child.getLiveVersion() != null) {
622                 throw new WebDAVException(
623                     WebDAVStatus.SC_FORBIDDEN,
624                     "Resource needs to be a new unversioned resource");
625             }
626
627             addCreator();
628
629             addPublicationDate();
630
631             User usr =
632                 ((HarmoniseSessionManager) m_resource.getUserAuthenticator())
633                     .getUser(m_resource);
634
635             // ensure change status is allowed
636
if (m_auxillary != null
637                 && m_auxillary.isChangeStatusValid(usr, m_child) == false) {
638
639                 throw new WebDAVException(
640                     WebDAVStatus.SC_FORBIDDEN,
641                     "Object workflow not complete");
642
643             }
644
645             m_child =
646                 CommandWrapper.changeStatus(
647                     m_dsi,
648                     m_child,
649                     usr,
650                     Status.APPROVED);
651
652             if (m_auxillary != null) {
653                 m_auxillary.changeStatus(
654                     usr,
655                     m_child,
656                     Status.UNAPPROVED,
657                     Status.APPROVED);
658             }
659         } catch (DataAccessException e) {
660             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
661             throw new WebDAVException(
662                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
663                 e.getLocalizedMessage());
664         } catch (APMException e) {
665             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
666             throw new WebDAVException(
667                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
668                 e.getLocalizedMessage());
669         }
670
671     }
672
673     /* (non-Javadoc)
674      * @see com.ibm.webdav.impl.NamespaceManager#isVersionable()
675      */

676     public boolean isVersionable() throws WebDAVException {
677         return true;
678     }
679
680 }
681
Popular Tags