001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.event;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.Vector;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.base.util.UtilTimer;
025: import org.ofbiz.entity.GenericDelegator;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericValue;
028: import org.ofbiz.entity.model.ModelEntity;
029:
030: /**
031: * DOCUMENT ME!
032: *
033: */
034: public class DataBuffer {
035: public static final String module = DataBuffer.class.getName();
036: private static final boolean TIMER = false;
037: protected ArrayList contents = new ArrayList();
038: protected GenericDelegator delegator = null;
039: protected DataMatrix parentDataMatrix = null;
040: protected int rowCount = 0;
041:
042: public DataBuffer(GenericDelegator delegator,
043: DataMatrix parentDataMatrix) {
044: setDelegator(delegator);
045: setParentDataMatrix(parentDataMatrix);
046: }
047:
048: /**
049: * DOCUMENT ME!
050: *
051: * @return
052: */
053: public ArrayList getContents() {
054: return contents;
055: }
056:
057: /**
058: * DOCUMENT ME!
059: *
060: * @param row
061: *
062: * @return
063: */
064: public Vector getContentsRow(int row) {
065: // Returns one row from the buffer. The row is a vector of generic values. There will be one generic value
066: // in the vector for each entity for one row of data.
067: Vector contentsRow = null;
068:
069: try {
070: contentsRow = (Vector) (contents.get(row));
071: } catch (IndexOutOfBoundsException e) {
072: Debug.logWarning("[DataBuffer.getContentsRow()]: Row "
073: + String.valueOf(row)
074: + " does not exist in the buffer.", module);
075: } catch (Exception e) {
076: e.printStackTrace();
077: }
078:
079: return contentsRow;
080: }
081:
082: /**
083: * DOCUMENT ME!
084: *
085: * @param genericValueVector
086: */
087: public void addContentsRow(Vector genericValueVector) {
088: UtilTimer utilTimer = new UtilTimer();
089:
090: if (TIMER) {
091: utilTimer.timerString(5,
092: "[DataBuffer.addContentsRow] Start");
093: }
094:
095: contents.add(genericValueVector);
096:
097: if (TIMER) {
098: utilTimer.timerString(5,
099: "[DataBuffer.addContentsRow] Finish");
100: }
101:
102: setRowCount(getRowCount() + 1);
103: }
104:
105: public void setContentsRow(int row, Vector genericValueVector) {
106: int numRows = getRowCount();
107:
108: if (row < numRows)
109: contents.set(row, genericValueVector);
110: else
111: addContentsRow(genericValueVector);
112: }
113:
114: /**
115: * DOCUMENT ME!
116: *
117: * @param row
118: * @param entityNumber
119: *
120: * @return
121: */
122: public GenericValue getGenericValue(int row, int entityNumber) {
123: UtilTimer utilTimer = new UtilTimer();
124:
125: if (TIMER) {
126: utilTimer
127: .timerString(5,
128: "[DataBuffer.getGenericValue(row, entityNumber)] Start");
129: }
130:
131: GenericValue genericValue = (GenericValue) (getContentsRow(row)
132: .get(entityNumber));
133:
134: if (TIMER) {
135: utilTimer
136: .timerString(5,
137: "[DataBuffer.getGenericValue(row, entityNumber)] Finished");
138: }
139:
140: return genericValue;
141: }
142:
143: /**
144: * DOCUMENT ME!
145: *
146: * @param row
147: * @param entityName
148: * @param isMandatory
149: *
150: * @return
151: *
152: * @throws GenericEntityException
153: */
154: public GenericValue getGenericValue(int row, String entityName,
155: boolean isMandatory) throws GenericEntityException {
156: // THIS IS SLOW. AVOID USING IT! Try to use getGenericValue(int row, int entityNumber) instead.
157: UtilTimer utilTimer = new UtilTimer();
158:
159: if (TIMER) {
160: utilTimer
161: .timerString(5,
162: "[DataBuffer.getGenericValue(row, entityName)] Start");
163: }
164:
165: Vector contentsRow = getContentsRow(row);
166:
167: if (contentsRow == null) {
168: throw new GenericEntityException("Row "
169: + String.valueOf(row)
170: + " does not exist in the data buffer.");
171: }
172:
173: Iterator gVI = getContentsRow(row).iterator();
174:
175: while (gVI.hasNext()) {
176: GenericValue testGV = (GenericValue) gVI.next();
177:
178: if (testGV.getEntityName().equals(entityName)) {
179: if (TIMER) {
180: utilTimer
181: .timerString(5,
182: "[DataBuffer.getGenericValue(row, entityName)] Finished");
183: }
184:
185: return testGV;
186: }
187: }
188:
189: if (isMandatory) {
190: throw new GenericEntityException(
191: "No generic value was found with name "
192: + entityName);
193: } else {
194: return null;
195: }
196: }
197:
198: /**
199: * DOCUMENT ME!
200: *
201: * @param row
202: * @param entityName
203: *
204: * @return
205: *
206: * @throws GenericEntityException
207: */
208: public GenericValue getGenericValue(int row, String entityName)
209: throws GenericEntityException {
210: return getGenericValue(row, entityName, true);
211: }
212:
213: /**
214: * DOCUMENT ME!
215: *
216: * @param entityName
217: * @param contentsRow
218: *
219: * @return
220: *
221: * @throws GenericEntityException
222: */
223: public GenericValue getGenericValue(String entityName,
224: Vector contentsRow) throws GenericEntityException {
225: // THIS IS SLOW. AVOID USING IT! Try to use getGenericValue(int row, int entityNumber) instead.
226: UtilTimer utilTimer = new UtilTimer();
227:
228: if (TIMER) {
229: utilTimer
230: .timerString(5,
231: "[DataBuffer.getGenericValue(entityName, contentsRow)] Start");
232: }
233:
234: // Find the correct generic value for the specified entity.
235: GenericValue gV = null;
236: Iterator contentsRowI = contentsRow.iterator();
237:
238: while (contentsRowI.hasNext()) {
239: GenericValue testEntity = (GenericValue) contentsRowI
240: .next();
241:
242: if (testEntity.getEntityName().equals(entityName)) {
243: if (TIMER) {
244: utilTimer
245: .timerString(5,
246: "[DataBuffer.getGenericValue(entityName, contentsRow)] Finished");
247: }
248:
249: return testEntity;
250: }
251: }
252:
253: throw new GenericEntityException(
254: "No generic value found with entity name " + entityName);
255: }
256:
257: /**
258: * DOCUMENT ME!
259: *
260: * @return
261: */
262: public GenericDelegator getDelegator() {
263: return delegator;
264: }
265:
266: /**
267: * DOCUMENT ME!
268: *
269: * @param delegator_
270: */
271: public void setDelegator(GenericDelegator delegator_) {
272: delegator = delegator_;
273: }
274:
275: /**
276: * DOCUMENT ME!
277: *
278: * @return
279: */
280: public DataMatrix getParentDataMatrix() {
281: return parentDataMatrix;
282: }
283:
284: /**
285: * DOCUMENT ME!
286: *
287: * @param parentDataMatrix_
288: */
289: public void setParentDataMatrix(DataMatrix parentDataMatrix_) {
290: parentDataMatrix = parentDataMatrix_;
291: }
292:
293: /**
294: * DOCUMENT ME!
295: *
296: * @return
297: */
298: public int getRowCount() {
299: return rowCount;
300: }
301:
302: /**
303: * DOCUMENT ME!
304: *
305: * @param rowCount_
306: */
307: public void setRowCount(int rowCount_) {
308: rowCount = rowCount_;
309: }
310:
311: //-------------------------------------------------------------------------
312: // Put values from the screen into the data buffer
313: //-------------------------------------------------------------------------
314:
315: /*
316: // This works, but it is not used because it is slow to do the buffers separately.
317: public int fillFromHtml(
318: HttpServletRequest request,
319: String htmlNamePrefix,
320: UIScreenSection uiScreenSection)
321: throws GenericEntityException {
322:
323: UtilTimer utilTimer = new UtilTimer();
324: if (TIMER) utilTimer.timerString(5, "[DataBuffer.fillFromHtml] Start");
325:
326: // Get the row count.
327: if (request.getParameter("rowCount")==null)
328: throw new GenericEntityException("rowCount parameter not found in request object.");
329: if (request.getParameter("rowCount").equals(""))
330: throw new GenericEntityException("rowCount parameter is empty in request object.");
331: try {
332: setRowCount(Integer.valueOf(request.getParameter("rowCount")).intValue());
333: } catch (NumberFormatException e) {
334: throw new GenericEntityException("rowCount parameter in request object does not contain a number.");
335: }
336:
337: for (int row = 0; row < getRowCount(); row++) {
338: // Create an array list of empty generic values to hold the values from the screen for one row.
339: addContentsRow(getRowFromHTML(row, request, htmlNamePrefix, uiScreenSection));
340: }
341: if (TIMER) utilTimer.timerString(5, "[DataBuffer.fillFromHtml] Finished");
342: return getRowCount();
343: }
344:
345: //-------------------------------------------------------------------------
346: // Get an array list containing the values from the screen by entity.
347: //-------------------------------------------------------------------------
348:
349: // This works, but it is not used because it is slow to do the buffers separately.
350: public Vector getRowFromHTML(
351: int row,
352: HttpServletRequest request,
353: String htmlNamePrefix,
354: UIScreenSection uiScreenSection)
355: throws GenericEntityException {
356:
357: UtilTimer utilTimer = new UtilTimer();
358: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start");
359: Vector contentsRow = createEmptyRow();
360:
361: // Loop through the entities and attributes for the current screen section, and get the value
362: // from the request object for each one, and store it in the contents.
363: for (int entityNbr = 0; entityNbr < getParentDataMatrix().getEntityParamVector().size(); entityNbr++) {
364: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start processing entity " + String.valueOf(entityNbr));
365: String entityName = getParentDataMatrix().getEntityName(entityNbr);
366: GenericValue gV = getGenericValue(entityName, contentsRow);
367: for (int attributeNbr = 0; attributeNbr < getParentDataMatrix().getAttributeVector(entityNbr).size(); attributeNbr++) {
368: String attributeName = getParentDataMatrix().getAttributeName(entityNbr, attributeNbr);
369: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start processing attribute " + attributeName);
370: String paramName = UIWebUtility.getParamName(htmlNamePrefix, entityName, attributeName, row);
371:
372: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start getting UIFieldInfo");
373: UIFieldInfo uiFieldInfo = uiScreenSection.getUiField(entityName, attributeName);
374: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished getting UIFieldInfo");
375: if (uiFieldInfo != null && uiFieldInfo.getDisplayOrder() > 0) {
376: // This field was put into the form (as visible or hidden). Get the display object.
377: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start getting UIDisplayObject");
378: // UIDisplayObject uiDisplayObject = new UIDisplayObject(uiFieldInfo.getDisplayObjectId(), getDelegator());
379: UIDisplayObject uiDisplayObject = uiFieldInfo.getUiDisplayObject();
380: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished getting UIDisplayObject");
381:
382: // Find out if this field is a check box. If so, need to use special handling.
383: boolean isCheckbox = false;
384: if (uiDisplayObject.getDisplayTypeId().equals(uiDisplayObject.DISPLAY_TYPE_CHECKBOX)) {
385: isCheckbox = true;
386: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start getting UIDisplayObject attributes");
387: uiDisplayObject.loadAttributes();
388: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished getting UIDisplayObject attributes");
389: }
390: if (request.getParameter(paramName)==null) {
391: // Attribute does not appear in the parameters.
392: if (isCheckbox) {
393: // Check box. Get the unchecked value, and store it in the buffer.
394: EventUtility.storeCorrectDataType(gV, attributeName, uiDisplayObject.getAttribUncheckedValue(), getDelegator());
395: } else {
396: // Skip this attribute. It is not displayed on the screen section.
397: }
398: } else {
399: if (isCheckbox) {
400: // Check box. Get the checked value, and store it in the buffer.
401: EventUtility.storeCorrectDataType(gV, attributeName, uiDisplayObject.getAttribCheckedValue(), getDelegator());
402: } else {
403: // Non-checkbox field. Just store the value.
404: String attributeValue = request.getParameter(paramName);
405: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Calling storeCorrectDataType");
406: EventUtility.storeCorrectDataType(gV, attributeName, attributeValue, getDelegator());
407: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished storeCorrectDataType");
408: }
409: }
410: }
411: }
412: }
413: if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished");
414: return contentsRow;
415: }
416: */
417: public Vector createEmptyRow() throws GenericEntityException {
418: UtilTimer utilTimer = new UtilTimer();
419:
420: if (TIMER) {
421: utilTimer.timerString(5,
422: "[DataBuffer.createEmptyRow] Start");
423: }
424:
425: Vector entityNameVector = getParentDataMatrix()
426: .getEntityNameVector();
427: Vector contentsRow = new Vector(entityNameVector.size());
428: Iterator entityIterator = entityNameVector.iterator();
429:
430: while (entityIterator.hasNext()) {
431: String entityName = (String) entityIterator.next();
432:
433: ModelEntity entityME = getDelegator().getModelEntity(
434: entityName);
435: GenericValue blankGV = new GenericValue(entityME);
436: blankGV.setDelegator(getDelegator());
437: contentsRow.add(blankGV);
438: }
439:
440: if (TIMER) {
441: utilTimer.timerString(5,
442: "[DataBuffer.createEmptyRow] Finished");
443: }
444:
445: return contentsRow;
446: }
447:
448: /**
449: * DOCUMENT ME!
450: *
451: * @throws GenericEntityException
452: */
453: public void addEmptyRow() throws GenericEntityException {
454: UtilTimer utilTimer = new UtilTimer();
455:
456: if (TIMER) {
457: utilTimer.timerString(5, "[DataBuffer.addEmptyRow] Start");
458: }
459:
460: addContentsRow(createEmptyRow());
461:
462: if (TIMER) {
463: utilTimer.timerString(5,
464: "[DataBuffer.addEmptyRow] Finished");
465: }
466:
467: return;
468: }
469: }
|