001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/warehouse/api-impl/src/java/org/theospi/portfolio/warehouse/impl/BaseChildWarehouseTask.java $
003: * $Id:BaseChildWarehouseTask.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.warehouse.impl;
021:
022: import java.sql.Connection;
023: import java.sql.PreparedStatement;
024: import java.sql.SQLException;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.quartz.JobExecutionException;
031: import org.theospi.portfolio.warehouse.intf.ChildWarehouseTask;
032: import org.theospi.portfolio.warehouse.intf.ItemIndexInParentPropertyAccess;
033: import org.theospi.portfolio.warehouse.intf.ParentPropertyAccess;
034: import org.theospi.portfolio.warehouse.intf.PropertyAccess;
035:
036: /**
037: * Created by IntelliJ IDEA.
038: * User: John Ellis
039: * Date: Nov 30, 2005
040: * Time: 4:58:05 PM
041: * To change this template use File | Settings | File Templates.
042: */
043: public class BaseChildWarehouseTask implements ChildWarehouseTask {
044:
045: private List fields;
046: private String insertStmt;
047: private String clearStmt;
048: private List complexFields;
049: private int batchSize = 100;
050: private boolean isPrepared = false;
051:
052: /**
053: * This is run after prepare
054: */
055: public void execute(Object parent, Collection items,
056: Connection connection) throws JobExecutionException {
057: PreparedStatement ps = null;
058:
059: isPrepared = false;
060: try {
061: int current = 0;
062: ps = connection.prepareStatement(getInsertStmt());
063: for (Iterator i = items.iterator(); i.hasNext();) {
064: processItem(parent, i.next(), ps, current);
065: ps.addBatch();
066: current++;
067: if (current > batchSize) {
068: current = 0;
069: ps.executeBatch();
070: }
071: ps.clearParameters();
072: }
073: if (current > 0) {
074: ps.executeBatch();
075: }
076: } catch (SQLException e) {
077: throw new JobExecutionException(new Exception("query: "
078: + getInsertStmt(), e));
079: } catch (NullPointerException e) {
080: throw new JobExecutionException(
081: new Exception(
082: "The BaseChildWarehouseTask.execute method parameter items is null. query identifier: "
083: + getInsertStmt(), e));
084: } finally {
085: try {
086: ps.close();
087: } catch (Exception e) {
088: // nothing to do here.
089: }
090: }
091:
092: }
093:
094: /**
095: * This method is run before execute. It ensures that the prepare functionality is only executed once
096: * @param Connection clears the database.
097: */
098: public void prepare(Connection connection) {
099: try {
100: if (isPrepared)
101: return;
102:
103: connection.createStatement().execute(getClearStmt());
104: isPrepared = true;
105:
106: if (getComplexFields() != null) {
107: for (Iterator i = getComplexFields().iterator(); i
108: .hasNext();) {
109: ChildFieldWrapper wrapper = (ChildFieldWrapper) i
110: .next();
111: wrapper.getTask().prepare(connection);
112: }
113: }
114: } catch (SQLException e) {
115: throw new RuntimeException(e);
116: }
117: }
118:
119: protected void processItem(Object parent, Object item,
120: PreparedStatement ps, int itemIndex)
121: throws JobExecutionException {
122:
123: try {
124: int index = 1;
125: for (Iterator i = getFields().iterator(); i.hasNext();) {
126: Object o = i.next();
127: if (o instanceof PropertyAccess) {
128: PropertyAccess pa = (PropertyAccess) o;
129: ps.setObject(index, pa.getPropertyValue(item));
130: } else if (o instanceof ParentPropertyAccess) {
131: ParentPropertyAccess pa = (ParentPropertyAccess) o;
132: ps.setObject(index, pa.getPropertyValue(parent,
133: item));
134: } else if (o instanceof ItemIndexInParentPropertyAccess) {
135: ps.setInt(index, itemIndex);
136: }
137: index++;
138: }
139:
140: // now, lets look for complex fields
141: if (getComplexFields() != null) {
142: for (Iterator i = getComplexFields().iterator(); i
143: .hasNext();) {
144: ChildFieldWrapper wrapper = (ChildFieldWrapper) i
145: .next();
146:
147: Object property = wrapper.getPropertyAccess()
148: .getPropertyValue(item);
149: Collection items = null;
150:
151: //If the complex field isn't a Collection then
152: // build a collection out of the single class
153: // instance in the complex field
154: if (property instanceof Collection) {
155: items = (Collection) property;
156: } else {
157: items = new ArrayList();
158: if (property != null)
159: items.add(property);
160: }
161:
162: // item becomes the new parent, items is the complex field (Collection)
163: wrapper.getTask().execute(item, items,
164: ps.getConnection());
165: }
166: }
167: } catch (Exception e) {
168: throw new JobExecutionException("error trying to prepare '"
169: + insertStmt + "'", e, false);
170: }
171: }
172:
173: public List getFields() {
174: return fields;
175: }
176:
177: public void setFields(List fields) {
178: this .fields = fields;
179: }
180:
181: public String getInsertStmt() {
182: return insertStmt;
183: }
184:
185: public void setInsertStmt(String insertStmt) {
186: this .insertStmt = insertStmt;
187: }
188:
189: public List getComplexFields() {
190: return complexFields;
191: }
192:
193: public void setComplexFields(List complexFields) {
194: this .complexFields = complexFields;
195: }
196:
197: public int getBatchSize() {
198: return batchSize;
199: }
200:
201: public void setBatchSize(int batchSize) {
202: this .batchSize = batchSize;
203: }
204:
205: public String getClearStmt() {
206: return clearStmt;
207: }
208:
209: public void setClearStmt(String clearStmt) {
210: this.clearStmt = clearStmt;
211: }
212: }
|