View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.oodt.xmlps.mapping;
19  
20  //JDK imports
21  import java.util.Arrays;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.TreeMap;
25  
26  /**
27   *
28   * <p>
29   * An Mapping is a {@link List} of {@link MappingField}s that define the
30   * translation of common ontological queries into queries against a local site's
31   * DBMS
32   * </p>
33   * .
34   */
35  public class Mapping {
36  
37    private final Map<String, MappingField> fields;
38  
39    private final DatabaseTableGroup tables;
40  
41    private String id;
42  
43    private String name;
44  
45    /**
46       *
47       */
48    public Mapping() {
49      this.fields = new TreeMap<String, MappingField>();
50      this.tables = new DatabaseTableGroup();
51    }
52  
53    public Mapping(Map<String, MappingField> fields, DatabaseTableGroup tables,
54        String id, String name) {
55      super();
56      this.fields = fields;
57      this.id = id;
58      this.name = name;
59      this.tables = tables;
60    }
61  
62    public void addField(String fldName, MappingField field) {
63      this.fields.put(fldName, field);
64    }
65  
66    public MappingField getFieldByLocalName(String localName) {
67      if (this.fields == null
68          || (this.fields != null && this.fields.keySet() == null)
69          || (this.fields != null && this.fields.keySet() != null && this.fields
70              .keySet().size() == 0)) {
71        return null;
72      }
73  
74      for (MappingField fld : this.fields.values()) {
75        if (fld.getLocalName().equals(localName)) {
76          return fld;
77        }
78      }
79  
80      return null;
81    }
82  
83    public MappingField getFieldByName(String name) {
84      return this.fields.get(name);
85    }
86  
87    public boolean constantField(String localName) {
88      MappingField fld = getFieldByLocalName(localName);
89  
90      if (fld == null) {
91        return true; // leave it out
92      }
93  
94      if (fld.getType() == FieldType.CONSTANT) {
95        return true;
96      } else
97        return false;
98    }
99  
100   public int getNumFields() {
101     return this.fields.keySet().size();
102   }
103 
104   public void addTable(String tblName, DatabaseTable tbl) {
105     this.tables.addTable(tblName, tbl);
106   }
107 
108   public DatabaseTable getTableByName(String name) {
109     return this.tables.getTableByName(name);
110   }
111 
112   public int getNumTables() {
113     return this.tables.getNumTables();
114   }
115 
116   public List<String> getTableNames() {
117     return this.tables.getTableNames();
118   }
119 
120   public List<String> getFieldNames() {
121     return Arrays.asList(this.fields.keySet().toArray(new String[] { "" }));
122   }
123 
124   /**
125    * @return the id
126    */
127   public String getId() {
128     return id;
129   }
130 
131   /**
132    * @param id
133    *          the id to set
134    */
135   public void setId(String id) {
136     this.id = id;
137   }
138 
139   /**
140    * @return the name
141    */
142   public String getName() {
143     return name;
144   }
145 
146   /**
147    * @param name
148    *          the name to set
149    */
150   public void setName(String name) {
151     this.name = name;
152   }
153 
154   /**
155    * @return the defaultTable
156    */
157   public String getDefaultTable() {
158     return this.tables.getDefaultTable();
159   }
160 
161   /**
162    * @param defaultTable
163    *          the defaultTable to set
164    */
165   public void setDefaultTable(String defaultTable) {
166     this.tables.setDefaultTable(defaultTable);
167   }
168 
169 }