001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.ibatis;
018:
019: import java.sql.SQLException;
020: import java.util.Iterator;
021:
022: import org.compass.core.CompassSession;
023: import org.compass.gps.CompassGpsException;
024: import org.compass.gps.device.AbstractGpsDevice;
025:
026: import com.ibatis.common.util.PaginatedList;
027: import com.ibatis.sqlmap.client.SqlMapClient;
028: import com.ibatis.sqlmap.client.SqlMapSession;
029:
030: /**
031: * A <code>SqlMapClient</code> device, provides support for iBatis 2 and the
032: * <code>index</code> operation. The device holds a list of iBatis select
033: * statements ids, executes them, and index the result.
034: * <p/>
035: * The device must be initialized with a <code>SqlMapClient</code> instance.
036: * When indexing the data, a <code>SqlMapSession</code> will be opened, and a
037: * transaction will be started. The device will then execute the select
038: * statement id, and use the iBatis <code>PaginatedList</code> to index the
039: * data.
040: * <p/>
041: * The page size for the <code>PaginatedList</code> can be controlled using
042: * the <code>pageSize</code> property.
043: * <p/>
044: * The select statment can have a parameter object associated with it. If one of
045: * the select statements requires a parameter object, than the
046: * <code>statementsParameterObjects</code> property must be set. It must have
047: * the same size as the <code>selectStatementsIds</code>, and the matching
048: * index of the <code>selectStatementsIds</code> should be set at the
049: * <code>statementsParameterObjects</code> property.
050: *
051: * @author kimchy
052: */
053: public class SqlMapClientGpsDevice extends AbstractGpsDevice {
054:
055: private SqlMapClient sqlMapClient;
056:
057: private String[] selectStatementsIds;
058:
059: private Object[] statementsParameterObjects;
060:
061: private int pageSize = 200;
062:
063: public SqlMapClientGpsDevice() {
064:
065: }
066:
067: public SqlMapClientGpsDevice(String deviceName,
068: SqlMapClient sqlMapClient, String[] selectStatementsIds) {
069: this (deviceName, sqlMapClient, selectStatementsIds, null);
070: }
071:
072: public SqlMapClientGpsDevice(String deviceName,
073: SqlMapClient sqlMapClient, String[] selectStatementsIds,
074: Object[] statementsParameterObjects) {
075: setName(deviceName);
076: this .sqlMapClient = sqlMapClient;
077: this .selectStatementsIds = selectStatementsIds;
078: this .statementsParameterObjects = statementsParameterObjects;
079: }
080:
081: protected void doStart() throws CompassGpsException {
082: if (sqlMapClient == null) {
083: throw new IllegalArgumentException(
084: buildMessage("Must set sqlMapClaient property"));
085: }
086: if (selectStatementsIds == null) {
087: throw new IllegalArgumentException(
088: buildMessage("Must set selectStatementsIds property"));
089: }
090: if (selectStatementsIds.length == 0) {
091: throw new IllegalArgumentException(
092: buildMessage("selectStatementsIds property must have at least one entry"));
093: }
094: if (statementsParameterObjects != null
095: && statementsParameterObjects.length != selectStatementsIds.length) {
096: throw new IllegalArgumentException(
097: buildMessage("Once the statementsParameterObjects property is set, it must have the same length as the selectStatementsIds property"));
098: }
099: }
100:
101: public SqlMapClient getSqlMapClient() {
102: return sqlMapClient;
103: }
104:
105: public void setSqlMapClient(SqlMapClient sqlMapClient) {
106: this .sqlMapClient = sqlMapClient;
107: }
108:
109: protected void doIndex(CompassSession session)
110: throws CompassGpsException {
111: if (log.isInfoEnabled()) {
112: log
113: .info(buildMessage("Indexing the database with page size ["
114: + pageSize + "]"));
115: }
116:
117: for (int i = 0; i < selectStatementsIds.length; i++) {
118: SqlMapSession sqlMapSession = sqlMapClient.openSession();
119: try {
120: sqlMapSession.startTransaction();
121: Object parameterObject = null;
122: if (statementsParameterObjects != null) {
123: parameterObject = statementsParameterObjects[i];
124: }
125: PaginatedList paginatedList = sqlMapSession
126: .queryForPaginatedList(selectStatementsIds[i],
127: parameterObject, pageSize);
128: do {
129: if (log.isDebugEnabled()) {
130: log
131: .debug(buildMessage("Indexing select statement id ["
132: + selectStatementsIds[i]
133: + "] page ["
134: + paginatedList.getPageIndex()
135: + "]"));
136: }
137: for (Iterator it = paginatedList.iterator(); it
138: .hasNext();) {
139: session.create(it.next());
140: }
141: session.evictAll();
142: } while (paginatedList.nextPage());
143: sqlMapSession.commitTransaction();
144: } catch (SQLException e) {
145: throw new SqlMapGpsDeviceException(
146: "Failed to fetch paginated list for statement ["
147: + selectStatementsIds[i] + "]", e);
148: } finally {
149: try {
150: try {
151: sqlMapSession.endTransaction();
152: } catch (Exception e) {
153: log
154: .warn(
155: buildMessage("Failed to close sqlMap session, ignoring"),
156: e);
157: }
158: } finally {
159: sqlMapSession.close();
160: }
161: }
162: }
163:
164: if (log.isInfoEnabled()) {
165: log.info(buildMessage("Finished indexing the database"));
166: }
167: }
168:
169: public String[] getSelectStatementsIds() {
170: return selectStatementsIds;
171: }
172:
173: public void setSelectStatementsIds(String[] statementsNames) {
174: this .selectStatementsIds = statementsNames;
175: }
176:
177: public int getPageSize() {
178: return pageSize;
179: }
180:
181: public void setPageSize(int pageSize) {
182: this .pageSize = pageSize;
183: }
184:
185: public Object[] getStatementsParameterObjects() {
186: return statementsParameterObjects;
187: }
188:
189: public void setStatementsParameterObjects(
190: Object[] statementsParameterObjects) {
191: this.statementsParameterObjects = statementsParameterObjects;
192: }
193:
194: }
|