01: /*
02: * Copyright 2003 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package velosurf.model;
18:
19: import java.util.List;
20:
21: import velosurf.util.StringLists;
22: import velosurf.util.Logger;
23:
24: /** An imported key (aka foreign key) attribute.
25: *
26: */
27:
28: public class ImportedKey extends Attribute {
29:
30: /**
31: * Foreign key columns.
32: */
33: private List<String> fkCols = null;
34:
35: /** Imported key constructor.
36: *
37: * @param name name of this exported key
38: * @param entity parent entity
39: * @param pkEntity primary key entity
40: * @param fkCols foreign key columns
41: */
42: public ImportedKey(String name, Entity entity, String pkEntity,
43: List<String> fkCols) {
44: super (name, entity);
45: setResultEntity(pkEntity);
46: this .fkCols = fkCols; /* may still be null at this stage */
47: setResultType(Attribute.ROW);
48: }
49:
50: /**
51: * Query getter.
52: * @return SQL query
53: */
54: protected String getQuery() {
55: if (query == null) {
56: Entity pkEntity = db.getEntity(resultEntity);
57: for (String param : fkCols) {
58: addParamName(param);
59: }
60: query = "SELECT * FROM "
61: + pkEntity.getTableName()
62: + " WHERE "
63: + StringLists.join(pkEntity.getPKCols(),
64: " = ? AND ") + " = ?";
65: // Logger.debug(getEntity().getName()+"."+getName()+" = "+query+" [ with params "+StringLists.join(fkCols,",")+" ]" );
66: }
67: return query;
68: }
69:
70: /**
71: * Foreign key columns getter.
72: * @return foreign key columns list
73: */
74: public List<String> getFKCols() {
75: return fkCols;
76: }
77:
78: /** Foreign key columns setter.
79: *
80: * @param fkCols foreign key columns list
81: */
82: public void setFKCols(List<String> fkCols) {
83: this .fkCols = fkCols;
84: }
85:
86: /** Debug method.
87: *
88: * @return the definition string of this attribute
89: */
90: public String toString() {
91: return "imported-key"
92: + (fkCols == null ? "" : " on "
93: + StringLists.join(fkCols, ","));
94: }
95:
96: }
|