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.Enumeration;
31  import java.util.List;
32  import java.util.Vector;
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 Pluginmodul implements a Ownership Control for Workflowactivitys. The
42   * Plugin manages the modifications of the field namOwner of a Workitem inside a
43   * activity by setting this Attribute. A Workflowmanager can use this attributes
44   * to reflect there settings to the ownership inside a certain databasesystem.
45   * The Plugin checks a set of activity attributes to manage the new settings of
46   * the ownership defined inside the activity entity These attributes are:
47   * 
48   * o keyOwnershipMode (Vector): '1'=modify access '0'=renew access
49   * 
50   * o namOwnershipNames (Vector): Names & Groups to add to the namOwner (Vector):
51   * 
52   * o keyaddwriteroles (Vector): Roles to add to the namOwner attribute
53   * 
54   * @author Ralph Soika
55   * @version 1.0
56   * @see org.imixs.workflow.WorkflowManager
57   */
58  
59  public class OwnerPlugin extends AbstractPlugin {
60  	ItemCollection documentContext;
61  	ItemCollection documentActivity;
62  	Vector itemOwnerRollback;
63  	WorkflowContext workflowContext;
64  
65  	public void init(WorkflowContext actx) throws PluginException {
66  		workflowContext = actx;
67  	}
68  
69  	/**
70  	 * changes the namworkflowreadaccess and namworkflowwriteaccess attribues
71  	 * depending to the activityentity
72  	 */
73  	public int run(ItemCollection adocumentContext,
74  			ItemCollection adocumentActivity) throws PluginException {
75  		List itemOwner;
76  		List vectorAccess;
77  		
78  			documentContext = adocumentContext;
79  			documentActivity = adocumentActivity;
80  
81  			// Validate Activity and Workitem
82  			validate();
83  
84  			itemOwner = (Vector) documentContext.getItemValue("namowner");
85  
86  			// save Attribute for roleback
87  			itemOwnerRollback = (Vector) documentContext
88  					.getItemValue("namOwners");
89  
90  			// add new ownership
91  			if ("1".equals(documentActivity
92  					.getItemValueString("keyOwnershipMode")))
93  				vectorAccess = itemOwner;
94  			else
95  				vectorAccess = new Vector();
96  			if (workflowContext.getLogLevel() == WorkflowKernel.LOG_LEVEL_FINE)
97  				System.out.println("[OwnerPlugin] AccessMode: '"
98  						+ documentActivity
99  								.getItemValueString("keyOwnershipMode") + "'");
100 
101 			if (vectorAccess == null)
102 				vectorAccess = new Vector();
103 
104 			// **1** AllowAccess add names
105 			mergeVectors(vectorAccess,
106 					documentActivity.getItemValue("namOwnershipNames"));
107 
108 			// **3** AllowAccess add Mapped Fields
109 			mergeMappedFieldValues(documentContext,vectorAccess,
110 					documentActivity.getItemValue("keyOwnershipFields"));
111 
112 			// clean Vector
113 			vectorAccess = uniqueList(vectorAccess);
114 
115 			// save Vector
116 			documentContext.replaceItemValue("namOwner", vectorAccess);
117 			if ((workflowContext.getLogLevel() == WorkflowKernel.LOG_LEVEL_FINE)
118 					&& (vectorAccess.size() > 0)) {
119 				System.out.println("[OwnerPlugin] Owner:");
120 				for (int j = 0; j < vectorAccess.size(); j++)
121 					System.out.println("              "
122 							+ (String) vectorAccess.get(j));
123 			}
124 
125 		
126 
127 		return Plugin.PLUGIN_OK;
128 	}
129 
130 	public void close(int status) throws PluginException {
131 		try {
132 
133 			// restore changes?
134 			if (status == Plugin.PLUGIN_ERROR) {
135 				documentContext.replaceItemValue("namOwner", itemOwnerRollback);
136 
137 			}
138 		} catch (Exception e) {
139 
140 			throw new PluginException("[AccessPlugin] Error close() "
141 					+ e.toString());
142 		}
143 	}
144 
145 	
146 
147 	
148 	/**
149 	 * Ensures that the workitem and activityentity has a valid set of
150 	 * attributes to be process by this plugin.
151 	 */
152 	private void validate() {
153 
154 		// validate activity
155 		if (!documentActivity.hasItem("keyOwnershipMode"))
156 			documentActivity.replaceItemValue("keyOwnershipMode", "");
157 
158 		if (!documentActivity.hasItem("namOwnershipNames"))
159 			documentActivity.replaceItemValue("namOwnershipNames", "");
160 
161 		if (!documentActivity.hasItem("keyOwnershipFields"))
162 			documentActivity.replaceItemValue("keyOwnershipFields", "");
163 
164 		// validate document
165 		if (!documentContext.hasItem("namOwner"))
166 			documentContext.replaceItemValue("namOwner", "");
167 
168 	}
169 
170 	
171 }