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.profile;
19  
20  //OODT imports
21  import org.apache.oodt.xmlps.mapping.DatabaseTable;
22  import org.apache.oodt.xmlps.mapping.MappingReader;
23  import org.apache.oodt.xmlps.product.XMLPSProductHandler;
24  import org.apache.oodt.xmlps.profile.DBMSExecutor;
25  import org.apache.oodt.xmlps.queryparser.Expression;
26  import org.apache.oodt.xmlps.queryparser.HandlerQueryParser;
27  
28  //JDK imports
29  import java.io.FileInputStream;
30  import java.io.FileNotFoundException;
31  import java.io.IOException;
32  import java.sql.SQLException;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Stack;
36  import java.util.logging.Level;
37  import java.util.logging.Logger;
38  
39  //OODT imports
40  import org.apache.oodt.profile.Profile;
41  import org.apache.oodt.profile.ProfileException;
42  import org.apache.oodt.profile.handlers.ProfileHandler;
43  import org.apache.oodt.xmlquery.QueryElement;
44  import org.apache.oodt.xmlquery.XMLQuery;
45  
46  /**
47   * 
48   * <p>
49   * An implementation of a {@link ProfileHandler} that extends the capabilities
50   * of the {@link XMLPSProductHandler}, and uses the XML Specification defined
51   * by the mapping file to represent its field mapping information.
52   * </p>.
53   */
54  public class XMLPSProfileHandler extends XMLPSProductHandler implements
55          ProfileHandler {
56  
57      private DBMSExecutor executor;
58  
59      /* our log stream */
60      private static final Logger LOG = Logger.getLogger(XMLPSProfileHandler.class
61              .getName());
62  
63      private String resLocationSpec;
64  
65      public XMLPSProfileHandler() throws InstantiationException {
66          super(null);
67          String mappingFilePath = System
68                  .getProperty("org.apache.oodt.xmlps.profile.xml.mapFilePath");
69  
70          if (mappingFilePath == null) {
71              throw new InstantiationException(
72                      "Need to specify path to xml mapping file!");
73          }
74  
75          try {
76              mapping = MappingReader.getMapping(mappingFilePath);
77          } catch (Exception e) {
78              throw new InstantiationException(
79                      "Unable to parse profile mapping xml file: ["
80                              + mappingFilePath + "]: reason: "
81                              + e.getMessage());
82          }
83  
84          // load the db properties file
85                  // if one exists: otherwise, don't bother and just print out the SQL to
86          // the console.
87          // 
88          String dbPropFilePath = System
89                  .getProperty("org.apache.oodt.xmlps.profile.xml.dbPropFilePath");
90          if (dbPropFilePath != null) {
91              try {
92                  System.getProperties()
93                          .load(new FileInputStream(dbPropFilePath));
94              } catch (FileNotFoundException e) {
95                  // TODO Auto-generated catch block
96                  e.printStackTrace();
97              } catch (IOException e) {
98                  e.printStackTrace();
99                  throw new InstantiationException(e.getMessage());
100             }
101 
102             executor = new DBMSExecutor();
103         }
104 
105         this.resLocationSpec = System
106                 .getProperty("org.apache.oodt.xmlps.profile.xml.resLocationSpec");
107     }
108 
109     /*
110      * (non-Javadoc)
111      * 
112      * @see org.apache.oodt.profile.handlers.ProfileHandler#findProfiles(org.apache.oodt.xmlquery.XMLQuery)
113      */
114     public List<Profile> findProfiles(XMLQuery query) throws ProfileException {
115         List<QueryElement> whereSet = query.getWhereElementSet();
116         List<QueryElement> selectSet = query.getSelectElementSet();
117         try {
118             translateToDomain(selectSet, true);
119             translateToDomain(whereSet, false);
120         } catch (Exception e) {
121             e.printStackTrace();
122             throw new ProfileException(e.getMessage());
123         }
124         List<Profile> profs = queryAndPackageProfiles(query);
125         return profs;
126     }
127 
128     /*
129      * (non-Javadoc)
130      * 
131      * @see org.apache.oodt.profile.handlers.ProfileHandler#get(java.lang.String)
132      */
133     public Profile get(String id) throws ProfileException {
134         throw new ProfileException("Method not implemented!");
135     }
136 
137     /*
138      * (non-Javadoc)
139      * 
140      * @see org.apache.oodt.profile.handlers.ProfileHandler#getID()
141      */
142     public String getID() {
143         return mapping.getId();
144     }
145 
146     protected List<Profile> queryAndPackageProfiles(XMLQuery query) {
147         Stack<QueryElement> queryStack = HandlerQueryParser
148                 .createQueryStack(query.getWhereElementSet());
149         Expression parsedQuery = HandlerQueryParser.parse(queryStack,
150                 this.mapping);
151         List<Profile> profs = null;
152 
153         StringBuffer sqlBuf = new StringBuffer("SELECT *");
154         sqlBuf.append(" FROM ");
155         sqlBuf.append(mapping.getDefaultTable());
156         sqlBuf.append(" ");
157 
158         if (mapping.getNumTables() > 0) {
159             for (Iterator<String> i = mapping.getTableNames().iterator(); i
160                     .hasNext();) {
161                 String tableName = i.next();
162                 if(tableName.equals(mapping.getDefaultTable())) continue;
163                 DatabaseTable tbl = mapping.getTableByName(tableName);
164                 sqlBuf.append("INNER JOIN ");
165                 sqlBuf.append(tbl.getName());
166                 sqlBuf.append(" ON ");
167                 sqlBuf.append(tbl.getName());
168                 sqlBuf.append(".");
169                 sqlBuf.append(tbl.getJoinFieldName());
170                 sqlBuf.append(" = ");
171                 sqlBuf.append(tbl.getDefaultTableJoin());
172                 sqlBuf.append(".");
173                 sqlBuf.append(tbl.getDefaultTableJoinFieldName());
174                 sqlBuf.append(" ");
175             }
176         }
177 
178         if (parsedQuery != null) {
179             sqlBuf.append(" WHERE ");
180             sqlBuf.append(parsedQuery.evaluate());
181         }
182 
183         LOG.log(Level.INFO, sqlBuf.toString());
184 
185         if (executor != null) {
186             try {
187                 profs = executor.executeLocalQuery(this.mapping, sqlBuf
188                         .toString(), this.resLocationSpec);
189 
190             } catch (SQLException e) {
191                 e.printStackTrace();
192                 LOG.log(Level.WARNING, "Error executing sql: ["
193                         + sqlBuf.toString() + "]: Message: " + e.getMessage());
194             }
195         }
196 
197         return profs;
198     }
199 
200     protected void translateToDomain(List<QueryElement> elemSet,
201             boolean selectSet) throws Exception {
202         super.translateToDomain(elemSet, selectSet);
203     }
204 
205 }