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.jee.faces;
29  
30  import java.util.Collection;
31  import java.util.Vector;
32  
33  import javax.ejb.EJB;
34  
35  import org.imixs.workflow.ItemCollection;
36  
37  /**
38   * This BLOBWorkitemController is used to store large objects into a single
39   * ItemCollection mapped to a EntityBean. The BLOBWorkitemController supports
40   * also the management of file attachment inside an ItemCollection. So the
41   * BLOBWorkitemController can be used to save large data objects or files into a
42   * Workitem (called BlobWorkitems). The BlobWorkitem is always bounded to a
43   * parent workitem by its referrer id ($uniqueidRef). An application can
44   * implement a lazy loading for BLOBWorkitems. The read- and write access
45   * settings of BLOBWorkitems are always synchronized to the settings of the
46   * parent workitem. Before the BlobWorkitem can be accessed the workitem needs
47   * to be loaded by the load() method. The Data can be accessed by the embedded
48   * Itemcollection through the method getWorkitem(). The BlobWorkitem can be
49   * saved by calling the save() method. Both - the load() and the save() method
50   * expect the Parent ItemCollection where the BlobWorkitem should be bound to.
51   * 
52   * @version 0.0.1
53   * @author rsoika
54   * 
55   */
56  public class BLOBWorkitemController {
57  
58  	@EJB
59  	org.imixs.workflow.jee.ejb.EntityService entityService;
60  
61  	private ItemCollection blobWorkitem = null;
62  
63  	public BLOBWorkitemController() {
64  		super();
65  
66  		// set the workitemAdapter
67  		clear();
68  	}
69  
70  	/**
71  	 * update the workitemAdapter and the blobworkitem reference
72  	 * 
73  	 * @param aItemcol
74  	 */
75  	public void setWorkitem(ItemCollection aItemcol) {
76  		blobWorkitem = aItemcol;
77  
78  	}
79  
80  	/**
81  	 * returns the ItemCollection for the curren BlobWorkitem object.
82  	 * 
83  	 * @return
84  	 */
85  	public ItemCollection getWorkitem() {
86  		return blobWorkitem;
87  	}
88  
89  	/**
90  	 * Returns a list of file names attached to the current BlobWorkitem. File
91  	 * Attachments can be added using the method addFile().
92  	 * 
93  	 * @return
94  	 */
95  	public String[] getFiles() {
96  		return blobWorkitem.getFiles();
97  
98  	}
99  
100 	/**
101 	 * Returns a Vector of file names attached to the current BlobWorkitem.
102 	 * 
103 	 * @return
104 	 */
105 	public Vector<String> getFileList() {
106 		Vector<String> vFileList = new Vector<String>();
107 		String[] sFileList = getFiles();
108 		for (String aName : sFileList)
109 			vFileList.add(aName);
110 
111 		return vFileList;
112 	}
113 
114 	/**
115 	 * Removes the connection to the parend workitem and clear the
116 	 * itemCollection
117 	 */
118 	public void clear() {
119 		blobWorkitem = new ItemCollection();
120 		try {
121 			blobWorkitem.replaceItemValue("type", "workitemlob");
122 		} catch (Exception e) {
123 			e.printStackTrace();
124 		}
125 		setWorkitem(blobWorkitem);
126 	}
127 
128 	/**
129 	 * This method saves the current BlobWorkitem. Therefore the method copies
130 	 * the read- and write access list from the given parent workitem into the
131 	 * BlobWorkitem before save.
132 	 * 
133 	 * So this method should be called after a WorkflowProcessing step to update
134 	 * the read- and write access identically to the parentWorkitem
135 	 * <p>
136 	 * The method did not save the blobWorkitem if the parent workitem has no
137 	 * $unqiueID!
138 	 * <p>
139 	 * 
140 	 * @throws Exception
141 	 */
142 	public void save(ItemCollection parentWorkitem) throws Exception {
143 
144 		if (blobWorkitem != null && parentWorkitem != null) {
145 
146 			// verifiy if a uniqueid is still defined. If not return without
147 			// saving!
148 			if ("".equals(parentWorkitem.getItemValueString("$uniqueID")))
149 				return;
150 
151 			// Update Read and write access list from parent workitem
152 			Vector vAccess = parentWorkitem.getItemValue("$ReadAccess");
153 			blobWorkitem.replaceItemValue("$ReadAccess", vAccess);
154 
155 			vAccess = parentWorkitem.getItemValue("$WriteAccess");
156 			blobWorkitem.replaceItemValue("$WriteAccess", vAccess);
157 
158 			blobWorkitem.replaceItemValue("$uniqueidRef",
159 					parentWorkitem.getItemValueString("$uniqueID"));
160 			blobWorkitem.replaceItemValue("type", "workitemlob");
161 			// Update BlobWorkitem
162 			blobWorkitem = entityService.save(blobWorkitem);
163 			// update adapter
164 			setWorkitem(blobWorkitem);
165 
166 		}
167 	}
168 
169 	/**
170 	 * This method adds a single file to the ItemCollection. files will be
171 	 * stored into the property $file.
172 	 * 
173 	 * @param data
174 	 *            - byte array with file data
175 	 * @param fileName
176 	 *            - name of the file attachment
177 	 * @param contentType
178 	 *            - the contenttype (e.g. 'Text/HTML')
179 	 * @throws Exception
180 	 */
181 	public void addFile(byte[] data, String fileName, String contentType)
182 			throws Exception {
183 
184 		blobWorkitem.addFile(data, fileName, contentType);
185 
186 	}
187 
188 	/**
189 	 * This method removes a single file attachment from the BlobWorkitem
190 	 * 
191 	 * @throws Exception
192 	 */
193 	public void removeFile(String aFilename) throws Exception {
194 		blobWorkitem.removeFile(aFilename);
195 	}
196 
197 	/**
198 	 * Loads the BlobWorkitem of a given parent Workitem. The BlobWorkitem is
199 	 * identified by the $unqiueidRef. If no BlobWorkitem still exists the
200 	 * method creates a new empty BlobWorkitem which can be saved later.
201 	 * 
202 	 * @param itemCol
203 	 *            - parent workitem where the BlobWorkitem will be attached to
204 	 * @throws Exception
205 	 */
206 	public void load(ItemCollection itemCol) throws Exception {
207 		ItemCollection ablobWorkitem = null;
208 
209 		// test if the blobworkitem could be loaded
210 		String sUniqueID = itemCol.getItemValueString("$uniqueid");
211 
212 		if (!"".equals(sUniqueID)) {
213 			// search entity...
214 			String sQuery = " SELECT lobitem FROM Entity as lobitem"
215 					+ " join lobitem.textItems as t2"
216 					+ " WHERE lobitem.type = 'workitemlob'"
217 					+ " AND t2.itemName = '$uniqueidref'"
218 					+ " AND t2.itemValue = '" + sUniqueID + "'";
219 
220 			Collection<ItemCollection> itemcol = entityService.findAllEntities(
221 					sQuery, 0, 1);
222 			// if workitem found then update blobWorkitem...
223 			if (itemcol != null && itemcol.size() > 0) {
224 				ablobWorkitem = itemcol.iterator().next();
225 				// update workitem
226 				setWorkitem(ablobWorkitem);
227 			}
228 
229 		}
230 		// if no blobWorkitem was found, create a empty itemCollection..
231 		if (ablobWorkitem == null) {
232 			clear();
233 		}
234 
235 	}
236 
237 	/*
238 	 * public Map getItem() throws Exception { return blobWorkitem.getItem(); }
239 	 * 
240 	 * 
241 	 * public Map getItemList() throws Exception { return
242 	 * blobWorkitem.getItemList(); }
243 	 * 
244 	 * 
245 	 * public Map getItemListArray() throws Exception { return
246 	 * blobWorkitem.getItemListArray(); }
247 	 */
248 
249 }