001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.model;
018:
019: import java.util.List;
020:
021: import velosurf.util.StringLists;
022: import velosurf.util.Logger;
023:
024: /** An exported key (aka primary key used in a foreign key) attribute.
025: *
026: */
027:
028: public class ExportedKey extends Attribute {
029:
030: /**
031: * List of foreign keys.
032: */
033: private List<String> fkCols = null;
034:
035: /**
036: * Order
037: */
038: private String order = null;
039:
040: /**
041: * Exported key constructor.
042: * @param name name of this exported key
043: * @param entity parent entity
044: * @param fkEntity foreign key entity
045: * @param fkCols foreign key columns
046: */
047: public ExportedKey(String name, Entity entity, String fkEntity,
048: List<String> fkCols) {
049: super (name, entity);
050: setResultType(Attribute.ROWSET);
051: setResultEntity(fkEntity);
052: this .fkCols = fkCols; /* may still be null at this stage */
053: }
054:
055: /**
056: * Foreign key columns getter.
057: * @return foreign key columns list
058: */
059: public List<String> getFKCols() {
060: return fkCols;
061: }
062:
063: /**
064: * Foreign key columns setter.
065: * @param fkCols foreign key columns list
066: */
067: public void setFKCols(List<String> fkCols) {
068: this .fkCols = fkCols;
069: }
070:
071: /**
072: * Set order
073: */
074: public void setOrder(String order) {
075: this .order = order;
076: }
077:
078: /**
079: * Query getter.
080: * @return the SQL query
081: */
082: protected String getQuery() {
083: if (query == null) {
084: Entity fkEntity = db.getEntity(resultEntity);
085: for (String param : getEntity().getPKCols()) {
086: addParamName(param);
087: }
088: query = "SELECT * FROM " + fkEntity.getTableName()
089: + " WHERE " + StringLists.join(fkCols, " = ? AND ")
090: + " = ?";
091: if (order != null) {
092: query += " ORDER BY " + order;
093: }
094: // Logger.debug(getEntity().getName()+"."+getName()+" = "+query+" [ with params "+StringLists.join(getEntity().getPKCols(),",")+" ]" );
095: }
096: return query;
097: }
098:
099: /** Debug method.
100: *
101: * @return the definition string of this attribute
102: */
103: public String toString() {
104: return "exported-key"
105: + (fkCols == null ? "" : " on "
106: + StringLists.join(fkCols, ","));
107: }
108:
109: }
|