KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > query > ProcedureQueryMetadata


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.query;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.cayenne.map.EntityResolver;
29 import org.apache.cayenne.map.Procedure;
30
31 class ProcedureQueryMetadata extends BaseQueryMetadata {
32
33     transient Procedure procedure;
34
35     public Procedure getProcedure() {
36         return procedure;
37     }
38
39     void copyFromInfo(QueryMetadata info) {
40         procedure = null;
41         super.copyFromInfo(info);
42     }
43
44     void resolve(
45             Object JavaDoc root,
46             Object JavaDoc resultRoot,
47             EntityResolver resolver,
48             ProcedureQuery query) {
49         if (super.resolve(resultRoot, resolver, null)) {
50             procedure = null;
51
52             if (root != null) {
53                 if (root instanceof String JavaDoc) {
54                     this.procedure = resolver.lookupProcedure((String JavaDoc) root);
55                 }
56                 else if (root instanceof Procedure) {
57                     this.procedure = (Procedure) root;
58                 }
59
60                 // theoretically procedure can be in one DataMap, while the Java Class
61
// - in another.
62
if (this.procedure != null && this.dataMap == null) {
63                     this.dataMap = procedure.getDataMap();
64                 }
65             }
66
67             // generate unique cache key...
68
if (QueryMetadata.NO_CACHE.equals(getCachePolicy())) {
69
70             }
71             else if (query.getName() != null) {
72                 this.cacheKey = query.getName();
73             }
74             else {
75
76                 // create a unique key based on procedure and parameters
77

78                 StringBuffer JavaDoc key = new StringBuffer JavaDoc("proc:");
79                 if (procedure != null) {
80                     key.append(procedure.getName());
81                 }
82
83                 Map JavaDoc parameters = query.getParameters();
84                 if (!parameters.isEmpty()) {
85
86                     List JavaDoc keys = new ArrayList JavaDoc(parameters.keySet());
87                     Collections.sort(keys);
88
89                     Iterator JavaDoc it = keys.iterator();
90                     while (it.hasNext()) {
91                         Object JavaDoc parameterKey = it.next();
92                         key.append('/').append(parameterKey).append('=').append(
93                                 parameters.get(parameterKey));
94                     }
95                 }
96
97                 this.cacheKey = key.toString();
98             }
99
100         }
101     }
102 }
103
Popular Tags