1   /**
2    * Copyright (c) 2000-2008 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.portal.PortalException;
28  import com.liferay.portal.SystemException;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.model.CompanyConstants;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.model.impl.GroupImpl;
34  import com.liferay.portlet.workflow.NoSuchDefinitionException;
35  import com.liferay.portlet.workflow.model.WorkflowDefinition;
36  import com.liferay.portlet.workflow.service.base.WorkflowDefinitionServiceBaseImpl;
37  
38  import java.rmi.RemoteException;
39  
40  /**
41   * <a href="WorkflowDefinitionServiceImpl.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class WorkflowDefinitionServiceImpl
48      extends WorkflowDefinitionServiceBaseImpl {
49  
50      public WorkflowDefinition addDefinition(
51              String xml, boolean addCommunityPermissions,
52              boolean addGuestPermissions)
53          throws PortalException, SystemException {
54  
55          return addDefinition(
56              xml, Boolean.valueOf(addCommunityPermissions),
57              Boolean.valueOf(addGuestPermissions), null, null);
58      }
59  
60      public WorkflowDefinition addDefinition(
61              String xml, String[] communityPermissions,
62              String[] guestPermissions)
63          throws PortalException, SystemException {
64  
65          return addDefinition(
66              xml, null, null, communityPermissions, guestPermissions);
67      }
68  
69      public WorkflowDefinition addDefinition(
70              String xml, Boolean addCommunityPermissions,
71              Boolean addGuestPermissions, String[] communityPermissions,
72              String[] guestPermissions)
73          throws PortalException, SystemException {
74  
75          try {
76  
77              // Deploy xml
78  
79              User user = getUser();
80  
81              long definitionId = GetterUtil.getLong(
82                  workflowComponentService.deploy(xml));
83  
84              // File
85  
86              long companyId = user.getCompanyId();
87              String portletId = CompanyConstants.SYSTEM_STRING;
88              long groupId = GroupImpl.DEFAULT_PARENT_GROUP_ID;
89              long repositoryId = CompanyConstants.SYSTEM;
90              String dirName = "workflow/definitions";
91              String fileName = dirName  + "/" + definitionId + ".xml";
92  
93              try {
94                  dlService.addDirectory(companyId, repositoryId, dirName);
95              }
96              catch (DuplicateDirectoryException dde) {
97              }
98  
99              dlService.addFile(
100                 companyId, portletId, groupId, repositoryId, fileName,
101                 StringPool.BLANK, new String[0], xml.getBytes());
102 
103             // Resources
104 
105             if ((addCommunityPermissions != null) &&
106                 (addGuestPermissions != null)) {
107 
108                 addDefinitionResources(
109                     user, definitionId, addCommunityPermissions.booleanValue(),
110                     addGuestPermissions.booleanValue());
111             }
112             else {
113                 addDefinitionResources(
114                     user, definitionId, communityPermissions, guestPermissions);
115             }
116 
117             return getDefinition(definitionId);
118         }
119         catch (RemoteException re) {
120             throw new SystemException(re);
121         }
122     }
123 
124     public void addDefinitionResources(
125             User user, long definitionId, boolean addCommunityPermissions,
126             boolean addGuestPermissions)
127         throws PortalException, SystemException {
128 
129         resourceLocalService.addResources(
130             user.getCompanyId(), 0, user.getUserId(),
131             WorkflowDefinition.class.getName(), definitionId, false,
132             addCommunityPermissions, addGuestPermissions);
133     }
134 
135     public void addDefinitionResources(
136             User user, long definitionId, String[] communityPermissions,
137             String[] guestPermissions)
138         throws PortalException, SystemException {
139 
140         resourceLocalService.addModelResources(
141             user.getCompanyId(), 0, user.getUserId(),
142             WorkflowDefinition.class.getName(), definitionId,
143             communityPermissions, guestPermissions);
144     }
145 
146     public WorkflowDefinition getDefinition(long definitionId)
147         throws PortalException, SystemException {
148 
149         try {
150             long companyId = getUser().getCompanyId();
151             long repositoryId = CompanyConstants.SYSTEM;
152             String dirName = "workflow/definitions";
153             String fileName = dirName  + "/" + definitionId + ".xml";
154 
155             String xml = new String(
156                 dlService.getFile(companyId, repositoryId, fileName));
157 
158             WorkflowDefinition definition =
159                 (WorkflowDefinition)workflowComponentService.getDefinition(
160                     definitionId);
161 
162             definition.setXml(xml);
163 
164             return definition;
165         }
166         catch (NoSuchFileException nsfe) {
167             throw new NoSuchDefinitionException();
168         }
169         catch (RemoteException re) {
170             throw new SystemException(re);
171         }
172     }
173 
174 }