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.text.DateFormat;
31  import java.util.Date;
32  import java.util.List;
33  import java.util.logging.Logger;
34  
35  import org.imixs.workflow.ItemCollection;
36  import org.imixs.workflow.Plugin;
37  import org.imixs.workflow.WorkflowKernel;
38  import org.imixs.workflow.exceptions.PluginException;
39  
40  /**
41   * This Pluginmodul implements a Historyfunktion for Workflowactivitys. The
42   * Plugin generates a History Entry which will be appended to an existing
43   * workflowprotokoll. The Protocol will be stored in the Attributes
44   * 
45   * o txtworkflowhistorylog (descending sort)
46   * 
47   * o txtworkflowhistorylogrev (ascending sort)
48   * 
49   * The Plugin generate a History Entry by a set of activityEntity attributes
50   * 
51   * These attributes are:
52   * 
53   * o rtfresultlog (String): HistoryLog text
54   * 
55   * o keylogdateformat (String): Dateformat for Historyentry
56   * 
57   * o keylogtimeformat (String): Timeformat for Historyentry
58   * 
59   * Each entry will be Präfixed by a DateTime String representing the time the
60   * Entry was generated.
61   * 
62   * The HistoryLog function pares the rtfresultlog for "<field>attribute</field>"
63   * tags and replaces these tags with the values of the corresponding attributes
64   * of the workitem.
65   * 
66   * 
67   * the Attribute numworkflowhistoryLength indicates the maximum number of
68   * entries. if <= 0 no limit is set. Otherwise older entries will be cut off.
69   * 
70   * 
71   * @author Ralph Soika
72   * @version 1.2
73   * @see org.imixs.workflow.WorkflowManager
74   * 
75   */
76  
77  public class HistoryPlugin extends AbstractPlugin {
78  	public ItemCollection documentContext;
79  	public ItemCollection documentActivity;
80  	List vOldProt, vOldProtRev;
81  	private static Logger logger = Logger.getLogger("org.imixs.workflow");
82  
83  	public int run(ItemCollection adocumentContext,
84  			ItemCollection adocumentActivity) throws PluginException {
85  		String rtfItemResult;
86  
87  		documentContext = adocumentContext;
88  		documentActivity = adocumentActivity;
89  
90  		// Logtext aus Resultdocument lesen
91  		rtfItemResult = documentActivity.getItemValueString("rtfresultlog");
92  		String aProtokoll = rtfItemResult;
93  
94  		String sDatumsFormat = documentActivity
95  				.getItemValueString("keyLogDateFormat");
96  
97  		if (sDatumsFormat == null || "".equals(sDatumsFormat))
98  			sDatumsFormat = "1";
99  		String sZeitFormat = documentActivity
100 				.getItemValueString("keylogtimeformat");
101 		if (sZeitFormat == null || "".equals(sZeitFormat))
102 			sZeitFormat = "2";
103 
104 		if (ctx.getLogLevel() == WorkflowKernel.LOG_LEVEL_FINE)
105 			logger.info("[HistoryPlugin] logtimeformat=" + sZeitFormat);
106 		if (ctx.getLogLevel() == WorkflowKernel.LOG_LEVEL_FINE)
107 			logger.info("[HistoryPlugin] logdateformat=" + sDatumsFormat);
108 
109 		// get Time Date format...
110 		int iDatumsFormat = -1;
111 		int iZeitFormat = -1;
112 		try {
113 			iDatumsFormat = Integer.parseInt(sDatumsFormat);
114 			iZeitFormat = Integer.parseInt(sZeitFormat);
115 		} catch (NumberFormatException nfe) {
116 			// invalid DateTime format found
117 			if (ctx.getLogLevel() >= WorkflowKernel.LOG_LEVEL_WARNING)
118 				logger.severe("[HistoryPlugin] error logtimeformat "
119 						+ nfe.toString());
120 		}
121 
122 		String sTim = "";
123 		if ((iZeitFormat > -1) && (iDatumsFormat > -1)) {
124 			sTim = DateFormat.getDateTimeInstance(iDatumsFormat, iZeitFormat)
125 					.format(new Date());
126 		} else {
127 			if ((iZeitFormat == -1) && (iDatumsFormat > -1))
128 				sTim = DateFormat.getDateInstance(iDatumsFormat).format(
129 						new Date());
130 			else if ((iZeitFormat > -1) && (iDatumsFormat == -1))
131 				sTim = DateFormat.getTimeInstance(iZeitFormat).format(
132 						new Date());
133 		}
134 
135 		// Check if a text was found. Protocol will only be added if text
136 		// was defined
137 		if (!"".equals(aProtokoll)) {
138 			String sDoppelpunkt = "";
139 			if ((!"".equals(sTim)) && (!"".equals(aProtokoll)))
140 				sDoppelpunkt = " : ";
141 			aProtokoll = sTim + sDoppelpunkt + aProtokoll;
142 
143 			aProtokoll = replaceDynamicValues(aProtokoll, documentContext);
144 
145 			vOldProt = documentContext.getItemValue("txtworkflowhistorylog");
146 			vOldProtRev = documentContext
147 					.getItemValue("txtworkflowhistorylogrev");
148 
149 			vOldProt.add(aProtokoll);
150 			vOldProtRev.add(0, aProtokoll);
151 
152 			// check if maximum length of log is defined
153 			int iMaxLogLength = documentContext
154 					.getItemValueInteger("numworkflowhistoryLength");
155 			if (iMaxLogLength > 0) {
156 				while (vOldProt.size() > iMaxLogLength)
157 					vOldProt.remove(0);
158 
159 				while (vOldProtRev.size() > iMaxLogLength)
160 					vOldProtRev.remove(vOldProtRev.size() - 1);
161 			}
162 
163 		}
164 
165 		return Plugin.PLUGIN_OK;
166 	}
167 
168 	/**
169 	 * store history only if no error has occurred
170 	 * 
171 	 * @throws
172 	 */
173 	public void close(int status) throws PluginException {
174 
175 		// restore changes if OK or WARNING
176 		if (status < Plugin.PLUGIN_ERROR) {
177 			if (vOldProt != null)
178 
179 				documentContext.replaceItemValue("txtworkflowhistorylog",
180 						vOldProt);
181 
182 			if (vOldProtRev != null)
183 				documentContext.replaceItemValue("txtworkflowhistorylogrev",
184 						vOldProtRev);
185 			Date date = new Date();
186 			// set timWorkflowLastAccess
187 			documentContext.replaceItemValue("timworkflowlastaccess", date);
188 
189 		}
190 
191 	}
192 
193 }