1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.workflow.service.impl;
24  
25  import com.liferay.documentlibrary.DuplicateDirectoryException;
26  import com.liferay.documentlibrary.NoSuchFileException;
27  import com.liferay.documentlibrary.service.DLServiceUtil;
28  import com.liferay.portal.PortalException;
29  import com.liferay.portal.SystemException;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.model.impl.CompanyImpl;
34  import com.liferay.portal.model.impl.GroupImpl;
35  import com.liferay.portal.service.ResourceLocalServiceUtil;
36  import com.liferay.portal.service.impl.PrincipalBean;
37  import com.liferay.portlet.workflow.NoSuchDefinitionException;
38  import com.liferay.portlet.workflow.model.WorkflowDefinition;
39  import com.liferay.portlet.workflow.service.WorkflowComponentServiceUtil;
40  import com.liferay.portlet.workflow.service.WorkflowDefinitionService;
41  
42  import java.rmi.RemoteException;
43  
44  /**
45   * <a href="WorkflowDefinitionServiceImpl.java.html"><b><i>View Source</i></b>
46   * </a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class WorkflowDefinitionServiceImpl
52      extends PrincipalBean implements WorkflowDefinitionService {
53  
54      public WorkflowDefinition addDefinition(
55              String xml, boolean addCommunityPermissions,
56              boolean addGuestPermissions)
57          throws PortalException, SystemException {
58  
59          return addDefinition(
60              xml, Boolean.valueOf(addCommunityPermissions),
61              Boolean.valueOf(addGuestPermissions), null, null);
62      }
63  
64      public WorkflowDefinition addDefinition(
65              String xml, String[] communityPermissions,
66              String[] guestPermissions)
67          throws PortalException, SystemException {
68  
69          return addDefinition(
70              xml, null, null, communityPermissions, guestPermissions);
71      }
72  
73      public WorkflowDefinition addDefinition(
74              String xml, Boolean addCommunityPermissions,
75              Boolean addGuestPermissions, String[] communityPermissions,
76              String[] guestPermissions)
77          throws PortalException, SystemException {
78  
79          try {
80  
81              // Deploy xml
82  
83              User user = getUser();
84  
85              long definitionId = GetterUtil.getLong(
86                  WorkflowComponentServiceUtil.deploy(xml));
87  
88              // File
89  
90              long companyId = user.getCompanyId();
91              String portletId = CompanyImpl.SYSTEM_STRING;
92              long groupId = GroupImpl.DEFAULT_PARENT_GROUP_ID;
93              long repositoryId = CompanyImpl.SYSTEM;
94              String dirName = "workflow/definitions";
95              String fileName = dirName  + "/" + definitionId + ".xml";
96  
97              try {
98                  DLServiceUtil.addDirectory(
99                      companyId, repositoryId, dirName);
100             }
101             catch (DuplicateDirectoryException dde) {
102             }
103 
104             DLServiceUtil.addFile(
105                 companyId, portletId, groupId, repositoryId, fileName,
106                 StringPool.BLANK, xml.getBytes());
107 
108             // Resources
109 
110             if ((addCommunityPermissions != null) &&
111                 (addGuestPermissions != null)) {
112 
113                 addDefinitionResources(
114                     user, definitionId, addCommunityPermissions.booleanValue(),
115                     addGuestPermissions.booleanValue());
116             }
117             else {
118                 addDefinitionResources(
119                     user, definitionId, communityPermissions, guestPermissions);
120             }
121 
122             return getDefinition(definitionId);
123         }
124         catch (RemoteException re) {
125             throw new SystemException(re);
126         }
127     }
128 
129     public void addDefinitionResources(
130             User user, long definitionId, boolean addCommunityPermissions,
131             boolean addGuestPermissions)
132         throws PortalException, SystemException {
133 
134         ResourceLocalServiceUtil.addResources(
135             user.getCompanyId(), 0, user.getUserId(),
136             WorkflowDefinition.class.getName(), definitionId, false,
137             addCommunityPermissions, addGuestPermissions);
138     }
139 
140     public void addDefinitionResources(
141             User user, long definitionId, String[] communityPermissions,
142             String[] guestPermissions)
143         throws PortalException, SystemException {
144 
145         ResourceLocalServiceUtil.addModelResources(
146             user.getCompanyId(), 0, user.getUserId(),
147             WorkflowDefinition.class.getName(), definitionId,
148             communityPermissions, guestPermissions);
149     }
150 
151     public WorkflowDefinition getDefinition(long definitionId)
152         throws PortalException, SystemException {
153 
154         try {
155             long companyId = getUser().getCompanyId();
156             long repositoryId = CompanyImpl.SYSTEM;
157             String dirName = "workflow/definitions";
158             String fileName = dirName  + "/" + definitionId + ".xml";
159 
160             String xml = new String(
161                 DLServiceUtil.getFile(companyId, repositoryId, fileName));
162 
163             WorkflowDefinition definition =
164                 (WorkflowDefinition)WorkflowComponentServiceUtil.getDefinition(
165                     definitionId);
166 
167             definition.setXml(xml);
168 
169             return definition;
170         }
171         catch (NoSuchFileException nsfe) {
172             throw new NoSuchDefinitionException();
173         }
174         catch (RemoteException re) {
175             throw new SystemException(re);
176         }
177     }
178 
179 }