KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
22
23 import org.apache.cayenne.map.EntityResolver;
24 import org.apache.cayenne.map.ObjEntity;
25
26 /**
27  * @since 3.0
28  * @author Andrus Adamchik
29  */

30 class SelectQueryMetadata extends BaseQueryMetadata {
31
32     boolean resolve(Object JavaDoc root, EntityResolver resolver, SelectQuery query) {
33
34         if (super.resolve(root, resolver, null)) {
35
36             // generate unique cache key...
37
if (QueryMetadata.NO_CACHE.equals(getCachePolicy())) {
38
39             }
40             else if (query.getName() != null) {
41                 this.cacheKey = query.getName();
42             }
43             else {
44                 // create a unique key based on entity, qualifier, ordering and fetch
45
// limit
46

47                 StringBuffer JavaDoc key = new StringBuffer JavaDoc();
48
49                 ObjEntity entity = getObjEntity();
50                 if (entity != null) {
51                     key.append(entity.getName());
52                 }
53                 else if (dbEntity != null) {
54                     key.append("db:").append(dbEntity.getName());
55                 }
56
57                 if (query.getQualifier() != null) {
58                     key.append('/').append(query.getQualifier());
59                 }
60
61                 if (!query.getOrderings().isEmpty()) {
62                     Iterator JavaDoc it = query.getOrderings().iterator();
63                     while (it.hasNext()) {
64
65                         Ordering o = (Ordering) it.next();
66                         key.append('/').append(o.getSortSpecString());
67                         if (!o.isAscending()) {
68                             key.append(":d");
69                         }
70
71                         if (o.isCaseInsensitive()) {
72                             key.append(":i");
73                         }
74                     }
75                 }
76                 
77                 if(query.getFetchLimit() > 0) {
78                     key.append('/').append(query.getFetchLimit());
79                 }
80
81                 this.cacheKey = key.toString();
82             }
83
84             return true;
85         }
86
87         return false;
88     }
89 }
90
Popular Tags