001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
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: package org.outerj.daisy.workflow.serverimpl.query;
017:
018: import org.outerj.daisy.workflow.WfValueType;
019: import org.outerj.daisy.workflow.WfVersionKey;
020: import org.outerj.daisy.workflow.WfUserKey;
021: import org.outerj.daisy.workflow.serverimpl.WfActorKeyToStringConverter;
022: import org.outerj.daisy.workflow.serverimpl.WfVersionKeyToStringConverter;
023: import org.hibernate.Query;
024:
025: import java.util.*;
026:
027: /**
028: * A class collecting all values to be bound to a hibernate query.
029: */
030: public class Binder {
031: private Map<String, BindEntry> entries = new HashMap<String, BindEntry>();
032: private int counter = 0;
033:
034: /**
035: *
036: * @param bindName obtained through {@link #getUniqueBindName()}.
037: */
038: public void addBind(String bindName, WfValueType type, Object value) {
039: if (entries.containsKey(bindName))
040: throw new RuntimeException("Tried to set bind for "
041: + bindName + " a second time.");
042: entries.put(bindName, new BindEntry(type, value, false));
043: }
044:
045: public void addBindList(String bindName, Collection collection) {
046: if (entries.containsKey(bindName))
047: throw new RuntimeException("Tried to set bind for "
048: + bindName + " a second time.");
049: entries.put(bindName, new BindEntry(null, collection, true));
050: }
051:
052: public void bind(Query query) {
053: for (Map.Entry<String, BindEntry> mapEntry : entries.entrySet()) {
054: BindEntry entry = mapEntry.getValue();
055: bindValue(query, mapEntry.getKey(), entry.type,
056: entry.value, entry.isList);
057: }
058: }
059:
060: public String getUniqueBindName() {
061: return "x" + counter++;
062: }
063:
064: private void bindValue(Query query, String name,
065: WfValueType valueType, Object value, boolean isList) {
066: if (isList) {
067: query.setParameterList(name, (Collection) value);
068: } else {
069: switch (valueType) {
070: case STRING:
071: query.setString(name, (String) value);
072: break;
073: case LONG:
074: query.setLong(name, (Long) value);
075: break;
076: case DATE:
077: query.setDate(name, (Date) value);
078: break;
079: case DATETIME:
080: query.setTimestamp(name, (Date) value);
081: break;
082: case ACTOR:
083: query.setString(name,
084: (String) new WfActorKeyToStringConverter()
085: .convert(value));
086: break;
087: case BOOLEAN:
088: query.setBoolean(name, (Boolean) value);
089: break;
090: case DAISY_LINK:
091: WfVersionKey versionKey = (WfVersionKey) value;
092: String searchVal;
093: if (versionKey.getVersion() != null) {
094: searchVal = (String) new WfVersionKeyToStringConverter()
095: .convert(versionKey);
096: } else {
097: searchVal = versionKey.getDocumentId() + "@"
098: + versionKey.getBranchId() + ":"
099: + versionKey.getLanguageId() + ":%";
100: }
101: query.setString(name, searchVal);
102: break;
103: case USER:
104: query.setLong(name, ((WfUserKey) value).getId());
105: break;
106: case ID:
107: long parsedId;
108: try {
109: parsedId = Long.parseLong((String) value);
110: } catch (NumberFormatException e) {
111: throw new RuntimeException("Invalid ID: " + value);
112: }
113: query.setLong(name, parsedId);
114: break;
115: default:
116: throw new RuntimeException(
117: "Unsupported value type for query binding: "
118: + valueType);
119: }
120: }
121: }
122:
123: private static final class BindEntry {
124: WfValueType type;
125: Object value;
126: boolean isList;
127:
128: public BindEntry(WfValueType type, Object value, boolean isList) {
129: this.type = type;
130: this.value = value;
131: this.isList = isList;
132: }
133: }
134: }
|