KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > multiplesitesdocument > MultipleSitesDocumentUtil


1 /*
2  * Copyright 2004 Hippo Webworks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package nl.hippo.cms.workflows.multiplesitesdocument;
17
18 import com.opensymphony.module.propertyset.PropertySet;
19 import com.opensymphony.workflow.WorkflowException;
20 import java.io.IOException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import nl.hippo.cms.Constants;
25 import nl.hippo.cms.repositorylocation.CommonRepositoryLocationRoles;
26 import nl.hippo.cms.repositorylocation.RepositoryInformation;
27 import nl.hippo.cms.repositorylocation.RepositoryLocation;
28 import nl.hippo.cms.sitesdirectory.SitesDirectory;
29 import nl.hippo.cocoon.webdav.WebDAVHelper;
30 import nl.hippo.componentcontainers.AvalonSpringBridge;
31 import nl.hippo.servermanager.Project;
32 import nl.hippo.servermanager.ProjectWorkflowRepository;
33 import org.apache.avalon.framework.service.ServiceException;
34 import org.apache.avalon.framework.service.ServiceManager;
35 import org.apache.commons.httpclient.HttpState;
36
37 public class MultipleSitesDocumentUtil
38 {
39
40     private MultipleSitesDocumentUtil()
41     {
42         super();
43     }
44
45     public static int getNumberOfCheckedSites(ServiceManager serviceManager, Map JavaDoc transientVars)
46             throws WorkflowException
47     {
48         int result;
49
50         try
51         {
52             SitesDirectory sitesDir = (SitesDirectory) serviceManager.lookup(SitesDirectory.ROLE);
53             try
54             {
55                 if (sitesDir.getNumberOfSites() == 1)
56                 {
57                     result = 1;
58                 }
59                 else
60                 {
61                     result = 0;
62                     Iterator JavaDoc siteIdsIter = sitesDir.siteIdsIterator();
63                     while (siteIdsIter.hasNext())
64                     {
65                         String JavaDoc siteId = (String JavaDoc) siteIdsIter.next();
66                         Object JavaDoc siteSelection = transientVars.get(siteId + "-selection");
67                         if (siteSelection != null && siteSelection.equals(Boolean.TRUE))
68                         {
69                             result += 1;
70                         }
71                     }
72                 }
73             }
74             finally
75             {
76                 serviceManager.release(sitesDir);
77             }
78         }
79         catch (ServiceException e)
80         {
81             throw new WorkflowException(e);
82         }
83
84         return result;
85     }
86
87     public static boolean isPublishedOnSite(String JavaDoc uri, String JavaDoc siteId, Project project,
88             HttpState httpState) throws IOException JavaDoc
89     {
90         boolean result = false;
91
92         String JavaDoc siteWorkflowId = WebDAVHelper.propfindAsString(uri, Constants.CMS_1_0_NAMESPACE,
93                 siteId + "-workflow-id", httpState);
94         if (siteWorkflowId != null && !siteWorkflowId.equals(""))
95         {
96             ProjectWorkflowRepository workflowRepository = project.getWorkflowRepository();
97             PropertySet sitePropertySet = workflowRepository.getPropertySet(Long.parseLong(siteWorkflowId));
98             int publishedVersion = sitePropertySet.getInt("publishedVersion");
99             
100             result = publishedVersion != 0;
101         }
102
103         return result;
104     }
105
106     public static VersionInformation getPublishedVersionInformation(String JavaDoc uri, Project project,
107             String JavaDoc siteId, ServiceManager manager) throws ServiceException, IOException JavaDoc
108     {
109         int publishedVersion = 0;
110         String JavaDoc versionComment = "";
111         
112         AvalonSpringBridge avalonSpringBridge = (AvalonSpringBridge) manager
113                 .lookup(AvalonSpringBridge.ROLE);
114         try
115         {
116             HttpState httpState = (HttpState) avalonSpringBridge.getBean("httpState");
117             String JavaDoc workflowIdAsString = WebDAVHelper.propfindAsString(uri, Constants.CMS_1_0_NAMESPACE, Constants.WORKFLOW_ID_PROPERTY_NAME, httpState);
118             if (workflowIdAsString != null)
119             {
120                 long workflowId =Long.parseLong(workflowIdAsString);
121                 ProjectWorkflowRepository workflow = project.getWorkflowRepository();
122                 PropertySet documentPropertySet = workflow.getPropertySet(workflowId);
123                 
124                 Map JavaDoc siteWorkflowIds = (Map JavaDoc) documentPropertySet.getAsActualType(MultipleSitesDocumentConstants.SITE_WORKFLOW_IDS_KEY);
125                 if (siteWorkflowIds == null)
126                 {
127                     siteWorkflowIds = new HashMap JavaDoc();
128                 }
129                 
130                 Long JavaDoc siteWorkflowIdAsLong = (Long JavaDoc)siteWorkflowIds.get(siteId);
131                 if (siteWorkflowIdAsLong != null)
132                 {
133                     long siteWorkflowId = (siteWorkflowIdAsLong).longValue();
134                     PropertySet sitePropertySet = workflow.getPropertySet(siteWorkflowId);
135                     publishedVersion = sitePropertySet.getInt("publishedVersion");
136                     String JavaDoc relativeLocation = documentPropertySet.getString("location");
137                     RepositoryLocation historyLocation = (RepositoryLocation) manager.lookup(CommonRepositoryLocationRoles.HISTORY_REPOSITORY_LOCATION_ROLE);
138                     try
139                     {
140                         RepositoryInformation historyInfo = historyLocation.getRepositoryInformation();
141                         String JavaDoc absoluteLocation = historyInfo.getAbsoluteUri(relativeLocation + "/" + publishedVersion + ".xml");
142                         versionComment = WebDAVHelper.propfindAsString(absoluteLocation, Constants.CMS_1_0_NAMESPACE, "versionComment", httpState);
143                     }
144                     finally
145                     {
146                         manager.release(historyLocation);
147                     }
148                 }
149             }
150         }
151         finally
152         {
153             manager.release(avalonSpringBridge);
154         }
155         
156         return new VersionInformation(publishedVersion, versionComment);
157     }
158
159 }
160
Popular Tags