View Javadoc

1   /*******************************************************************************
2    *  Imixs Workflow 
3    *  Copyright (C) 2001, 2011 Imixs Software Solutions GmbH,  
4    *  http://www.imixs.com
5    *  
6    *  This program is free software; you can redistribute it and/or 
7    *  modify it under the terms of the GNU General Public License 
8    *  as published by the Free Software Foundation; either version 2 
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   *  General Public License for more details.
15   *  
16   *  You can receive a copy of the GNU General Public
17   *  License at http://www.gnu.org/licenses/gpl.html
18   *  
19   *  Project: 
20   *  	http://www.imixs.org
21   *  	http://java.net/projects/imixs-workflow
22   *  
23   *  Contributors:  
24   *  	Imixs Software Solutions GmbH - initial API and implementation
25   *  	Ralph Soika - Software Developer
26   *******************************************************************************/
27  
28  package org.imixs.workflow.plugins;
29  
30  import java.util.List;
31  import java.util.Vector;
32  import java.util.logging.Logger;
33  
34  import org.imixs.workflow.ItemCollection;
35  import org.imixs.workflow.Plugin;
36  import org.imixs.workflow.WorkflowContext;
37  import org.imixs.workflow.WorkflowKernel;
38  import org.imixs.workflow.exceptions.PluginException;
39  
40  /**
41   * This Plugin module implements a generic Access management controled throught
42   * the configuration in a Workflowactivity. The Plugin updates the attriubtes
43   * $ReadAccess and $WriteAccess depending on the configuration in a
44   * ActivityEntiy.
45   * <p>
46   * These attributes are:
47   * <ul>
48   * <li>keyaccessmode (Vector): '1'=update '0'=renew
49   * <li>namaddreadaccess (Vector): Names & Groups to be added /replaced
50   * <li>namaddwriteaccess (Vector): Names & Groups to be added/replaced
51   * <li>keyaddreadroles (Vector): Roles to be added/replaced
52   * <li>keyaddwriteroles (Vector): Roles to added/replaced
53   * <li>keyaddreadfields (Vector): Attributes of the processd workitem to add
54   * there values
55   * <li>keyaddwritefields (Vector): Attributes of the processd workitem to add
56   * therevalues
57   * 
58   * @author Ralph Soika
59   * @version 2.0
60   * @see org.imixs.workflow.WorkflowManager
61   */
62  
63  public class AccessPlugin extends AbstractPlugin {
64  	ItemCollection documentContext;
65  	ItemCollection documentActivity;
66  	List itemReadRollback, itemWriteRollback;
67  	WorkflowContext workflowContext;
68  
69  	private static Logger logger = Logger.getLogger("org.imixs.workflow");
70  
71  	public void init(WorkflowContext actx) throws PluginException {
72  		workflowContext = actx;
73  	}
74  
75  	/**
76  	 * changes the $readAccess and $writeAccess attribues depending to the
77  	 * activityentity
78  	 */
79  	public int run(ItemCollection adocumentContext,
80  			ItemCollection adocumentActivity) throws PluginException {
81  		List itemRead;
82  		List itemWrite;
83  		List vectorAccess;
84  
85  		documentContext = adocumentContext;
86  		documentActivity = adocumentActivity;
87  
88  		// Validate Activity and Workitem
89  		validate();
90  
91  		itemRead = (Vector) documentContext.getItemValue("$readAccess");
92  
93  		// save Attribute for roleback
94  		itemReadRollback = (Vector) documentContext.getItemValue("$readAccess");
95  
96  		// neuen ReadAccess hinzuf�gen
97  		if ("1".equals(documentActivity.getItemValueString("keyaccessmode")))
98  			vectorAccess = itemRead;
99  		else
100 			vectorAccess = new Vector();
101 		if (workflowContext.getLogLevel() == WorkflowKernel.LOG_LEVEL_FINE)
102 			logger.info("[AccessPlugin] AccessMode: '"
103 					+ documentActivity.getItemValueString("keyaccessmode")
104 					+ "'");
105 
106 		if (vectorAccess == null)
107 			vectorAccess = new Vector();
108 
109 		// **1** AllowAccess add names
110 		mergeVectors(vectorAccess,
111 				documentActivity.getItemValue("namaddreadaccess"));
112 		// **2** AllowAccess add roles
113 		mergeVectors(vectorAccess,
114 				documentActivity.getItemValue("keyaddreadroles"));
115 		// **3** AllowAccess add Mapped Fields
116 		mergeMappedFieldValues(documentContext,vectorAccess,
117 				documentActivity.getItemValue("keyaddreadfields"));
118 
119 		// clean Vector
120 		vectorAccess = uniqueList(vectorAccess);
121 
122 		// save Vector
123 		documentContext.replaceItemValue("$readAccess", vectorAccess);
124 		if ((workflowContext.getLogLevel() == WorkflowKernel.LOG_LEVEL_FINE)
125 				&& (vectorAccess.size() > 0)) {
126 			logger.info("[AccessPlugin] ReadAccess:");
127 			for (int j = 0; j < vectorAccess.size(); j++)
128 				logger.info("              "
129 						+ (String) vectorAccess.get(j));
130 		}
131 
132 		/**** now process write access ***/
133 
134 		// check for $writeAccess
135 		itemWrite = documentContext.getItemValue("$writeAccess");
136 
137 		// save Attribute for roleback
138 		itemWriteRollback = documentContext.getItemValue("$writeAccess");
139 
140 		// add new WriteAccess
141 
142 		if ("1".equals(documentActivity.getItemValueString("keyaccessmode")))
143 			vectorAccess = itemWrite;
144 		else
145 			vectorAccess = new Vector();
146 
147 		if (vectorAccess == null)
148 			vectorAccess = new Vector();
149 
150 		// **1** AllowAccess add Names
151 		mergeVectors(vectorAccess,
152 				documentActivity.getItemValue("namaddwriteaccess"));
153 		// **2** AllowAccess add Rolles
154 		mergeVectors(vectorAccess,
155 				documentActivity.getItemValue("keyaddwriteroles"));
156 		// **3** AllowAccess add Mapped Fields �gen
157 		mergeMappedFieldValues(documentContext,vectorAccess,
158 				documentActivity.getItemValue("keyaddwritefields"));
159 
160 		// clean Vector
161 		vectorAccess = uniqueList(vectorAccess);
162 
163 		// save Vector
164 		documentContext.replaceItemValue("$writeAccess", vectorAccess);
165 		if ((workflowContext.getLogLevel() == WorkflowKernel.LOG_LEVEL_FINE)
166 				&& (vectorAccess.size() > 0)) {
167 			logger.info("[AccessPlugin] WriteAccess:");
168 			for (int j = 0; j < vectorAccess.size(); j++)
169 				logger.info("               "
170 						+ (String) vectorAccess.get(j));
171 		}
172 
173 		return Plugin.PLUGIN_OK;
174 	}
175 
176 	public void close(int status) throws PluginException {
177 		try {
178 
179 			// restore changes?
180 			if (status == Plugin.PLUGIN_ERROR) {
181 				documentContext.replaceItemValue("$writeAccess",
182 						itemWriteRollback);
183 				documentContext.replaceItemValue("$readAccess",
184 						itemReadRollback);
185 			}
186 		} catch (Exception e) {
187 			throw new PluginException("[AccessPlugin] Error close() "
188 					+ e.toString());
189 
190 		}
191 	}
192 
193 	
194 
195 
196 	/**
197 	 * Ensures that the workitem and activityentity has a valid set of
198 	 * attributes to be process by this plugin.
199 	 */
200 	private void validate() {
201 
202 		// validate activity
203 		if (!documentActivity.hasItem("keyaccessmode"))
204 			documentActivity.replaceItemValue("keyaccessmode", "");
205 
206 		if (!documentActivity.hasItem("namaddreadaccess"))
207 			documentActivity.replaceItemValue("namaddreadaccess", "");
208 
209 		if (!documentActivity.hasItem("keyaddreadroles"))
210 			documentActivity.replaceItemValue("keyaddreadroles", "");
211 
212 		if (!documentActivity.hasItem("keyaddreadfields"))
213 			documentActivity.replaceItemValue("keyaddreadfields", "");
214 
215 		if (!documentActivity.hasItem("namaddwriteaccess"))
216 			documentActivity.replaceItemValue("namaddwriteaccess", "");
217 
218 		if (!documentActivity.hasItem("keyaddwriteroles"))
219 			documentActivity.replaceItemValue("keyaddwriteroles", "");
220 
221 		if (!documentActivity.hasItem("keyaddreadfields"))
222 			documentActivity.replaceItemValue("keyaddreadfields", "");
223 
224 		// validate document
225 		if (!documentContext.hasItem("$readAccess"))
226 			documentContext.replaceItemValue("$readAccess", "");
227 
228 		if (!documentContext.hasItem("$writeAccess"))
229 			documentContext.replaceItemValue("$writeAccess", "");
230 
231 	}
232 
233 	
234 }