01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.persistency.ocache.impl;
10:
11: import com.completex.objective.components.ocache.OdalKeyFactory;
12: import com.completex.objective.components.persistency.AbstractParameters;
13: import com.completex.objective.components.persistency.BasicQuery;
14: import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
15: import com.completex.objective.components.persistency.Parameter;
16: import com.completex.objective.util.StringUtil;
17:
18: /**
19: * @author Gennady Krizhevsky
20: */
21: public class BasicQueryKeyFactory implements OdalKeyFactory {
22: public static final String SEP = "|";
23:
24: public BasicQueryKeyFactory() {
25: }
26:
27: /**
28: * Returns key created out of the value object.
29: *
30: * @param value BasicQuery object
31: * @return BasicQuery name object
32: */
33: public Object key(Object value) {
34: if (value == null) {
35: return null;
36: }
37:
38: BasicQuery query = (BasicQuery) value;
39: String name = query.getName();
40: if (name == null) {
41: if (StringUtil.isEmpty(query.getSql())) {
42: throw new OdalRuntimePersistencyException(
43: "Query name is null while query.getSql() is null null");
44: }
45: name = query.getSql();
46: } else {
47: name = name + resolveNull(query.getSql()).hashCode();
48: }
49:
50: StringBuffer buffer = new StringBuffer(name);
51: buffer.append(SEP).append(query.getOffset()).append(SEP)
52: .append(query.getPageSize());
53:
54: AbstractParameters parameters = query.getBaseParameters();
55: if (parameters != null && parameters.size() > 0) {
56: for (int i = 0; i < parameters.size(); i++) {
57: Parameter parameter = parameters.get(i);
58: if (i > 0) {
59: buffer.append(SEP);
60: }
61: buffer.append(resolveNull(parameter.getValue())
62: .toString());
63: }
64: }
65:
66: return buffer.toString();
67: }
68:
69: Object resolveNull(Object value) {
70: return value == null ? "null" : value;
71: }
72: }
|