KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.cayenne.query;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.map.EntityResolver;
28 import org.apache.cayenne.map.ObjEntity;
29
30 /**
31  * @since 3.0
32  * @author Andrus Adamchik
33  */

34 class SQLTemplateMetadata extends BaseQueryMetadata {
35     
36     private SQLResultSetMapping resultSetMapping;
37     
38     void setResultSetMapping(SQLResultSetMapping resultSetMapping) {
39         this.resultSetMapping = resultSetMapping;
40     }
41     
42     public SQLResultSetMapping getResultSetMapping() {
43         return resultSetMapping;
44     }
45
46     boolean resolve(Object JavaDoc root, EntityResolver resolver, SQLTemplate query) {
47
48         if (super.resolve(root, resolver, null)) {
49
50             // generate unique cache key...
51
if (QueryMetadata.NO_CACHE.equals(getCachePolicy())) {
52
53             }
54             else if (query.getName() != null) {
55                 this.cacheKey = query.getName();
56             }
57             else {
58
59                 // create a unique key based on entity, SQL, and parameters
60

61                 StringBuffer JavaDoc key = new StringBuffer JavaDoc();
62                 ObjEntity entity = getObjEntity();
63                 if (entity != null) {
64                     key.append(entity.getName());
65                 }
66                 else if (dbEntity != null) {
67                     key.append("db:").append(dbEntity.getName());
68                 }
69
70                 if (query.getDefaultTemplate() != null) {
71                     key.append('/').append(query.getDefaultTemplate());
72                 }
73
74                 Map JavaDoc parameters = query.getParameters();
75                 if (!parameters.isEmpty()) {
76
77                     List JavaDoc keys = new ArrayList JavaDoc(parameters.keySet());
78                     Collections.sort(keys);
79
80                     Iterator JavaDoc it = keys.iterator();
81                     while (it.hasNext()) {
82                         Object JavaDoc parameterKey = it.next();
83                         key.append('/').append(parameterKey).append('=').append(
84                                 parameters.get(parameterKey));
85                     }
86                 }
87
88                 this.cacheKey = key.toString();
89             }
90
91             return true;
92         }
93
94         return false;
95     }
96 }
97
Popular Tags