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.AbstractPersistentObject;
13: import com.completex.objective.components.persistency.BasicQuery;
14: import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
15: import com.completex.objective.components.persistency.AbstractParameters;
16: import com.completex.objective.components.persistency.Parameter;
17: import com.completex.objective.util.StringUtil;
18:
19: /**
20: * Creates key out of AbstractPersistentObject instance by its toKey() method
21: *
22: * @author Gennady Krizhevsky
23: */
24: public class PoKeyFactory implements OdalKeyFactory {
25: public static final String SEP = "|";
26:
27: /**
28: * Returns key created out of the value object.
29: *
30: * @param value AbstractPersistentObject object
31: * @return AbstractPersistentObject.toKey() object
32: */
33: public Object key(Object value) {
34: if (value == null) {
35: return null;
36: }
37:
38: if (value instanceof AbstractPersistentObject) {
39: return poKey(((AbstractPersistentObject) value));
40: } else if (value instanceof BasicQuery) {
41: return queryKey(((BasicQuery) value));
42: }
43: return null;
44: }
45:
46: public Object poKey(AbstractPersistentObject persistent) {
47: return persistent.toKey();
48: }
49:
50: public Object queryKey(BasicQuery query) {
51: String name = query.getName();
52: if (name == null) {
53: if (StringUtil.isEmpty(query.getSql())) {
54: throw new OdalRuntimePersistencyException(
55: "Query name is null while query.getSql() is null");
56: }
57: name = query.getSql();
58: } else {
59: name = name + resolveNull(query.getSql()).hashCode();
60: }
61:
62: StringBuffer buffer = new StringBuffer(name);
63: buffer.append(SEP).append(query.getOffset()).append(SEP)
64: .append(query.getPageSize());
65:
66: AbstractParameters parameters = query.getBaseParameters();
67: if (parameters != null && parameters.size() > 0) {
68: for (int i = 0; i < parameters.size(); i++) {
69: Parameter parameter = parameters.get(i);
70: if (i > 0) {
71: buffer.append(SEP);
72: }
73: buffer.append(resolveNull(parameter.getValue())
74: .toString());
75: }
76: }
77:
78: return buffer.toString();
79: }
80:
81: Object resolveNull(Object value) {
82: return value == null ? "null" : value;
83: }
84: }
|