KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > pm > impl > PMImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 org.apache.ws.jaxme.pm.impl;
18
19 import java.sql.Types JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Calendar JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.xml.bind.DatatypeConverter;
26 import javax.xml.bind.JAXBException;
27
28 import org.apache.ws.jaxme.JMManager;
29 import org.apache.ws.jaxme.Observer;
30 import org.apache.ws.jaxme.PM;
31 import org.apache.ws.jaxme.PMException;
32 import org.apache.ws.jaxme.PMParams;
33
34 /** <p>Abstract base class for persistence managers.</p>
35  *
36  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
37  */

38 public abstract class PMImpl implements PM {
39   private JMManager manager;
40
41   /** <p>Creates a new instance of PMImpl.java.</p>
42    */

43   public PMImpl() {
44   }
45
46   public void init(JMManager pManager) throws JAXBException {
47     manager = pManager;
48   }
49
50   public JMManager getManager() {
51     return manager;
52   }
53
54   public void select(Observer pObserver, String JavaDoc pQuery) throws JAXBException {
55     select(pObserver, pQuery, null);
56   }
57
58   public Iterator JavaDoc select(String JavaDoc pQuery) throws JAXBException {
59     return select(pQuery, null);
60   }
61
62   /** The <code>ListObserver</code> is a basic
63    * {@link Observer}, which collects result objects
64    * in a {@link List}.
65    */

66   public static class ListObserver implements Observer {
67     private List JavaDoc list;
68     /** Creates a new <code>ListObserver</code>, which writes
69      * the received objects into the {@link List}
70      * <code>pList</code>.
71      */

72     public ListObserver(List JavaDoc pList) {
73       list = pList;
74     }
75     public void notify(Object JavaDoc pObservable) {
76       list.add(pObservable);
77     }
78   }
79
80   public Iterator JavaDoc select(String JavaDoc pQuery, PMParams pPlaceHolderArgs) throws JAXBException {
81     List JavaDoc result = new ArrayList JavaDoc();
82     ListObserver observer = new ListObserver(result);
83     select(observer, pQuery, pPlaceHolderArgs);
84     return result.iterator();
85   }
86
87   public Object JavaDoc create() throws JAXBException {
88     return manager.getElementJ();
89   }
90
91   protected String JavaDoc parseQuery(String JavaDoc pQuery, PMParams pPlaceHolderArgs) throws PMException {
92     if (pPlaceHolderArgs == null) {
93       return pQuery;
94     }
95     Iterator JavaDoc iter = pPlaceHolderArgs.getParams();
96     if (!iter.hasNext()) {
97       return pQuery;
98     }
99     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
100     boolean inStr = false;
101     char delim = 0;
102     for (int i = 0; i < pQuery.length(); i++) {
103       char c = pQuery.charAt(i);
104       if (inStr) {
105         if (c == delim) {
106           inStr = false;
107         }
108         sb.append(c);
109       } else {
110         switch (c) {
111           case '\'':
112             inStr = true;
113             delim = c;
114             sb.append(c);
115             break;
116           case '?':
117             if (!iter.hasNext()) {
118               throw new PMException("Number of placeholder marks exceeds number of actual parameters");
119             }
120             PMParams.Param param = (PMParams.Param) iter.next();
121             switch (param.getType()) {
122               case Types.VARCHAR:
123                 sb.append('\'');
124                 sb.append(param.getValue());
125                 sb.append('\'');
126                 break;
127               case Types.BIGINT:
128               case Types.INTEGER:
129               case Types.SMALLINT:
130               case Types.TINYINT:
131                 sb.append(param.getValue());
132                 break;
133               case Types.TIMESTAMP:
134                 sb.append('\'');
135                 sb.append(DatatypeConverter.printDateTime((Calendar JavaDoc) param.getValue()));
136                 sb.append('\'');
137                 break;
138               case Types.DATE:
139                 sb.append('\'');
140                 sb.append(DatatypeConverter.printDate((Calendar JavaDoc) param.getValue()));
141                 sb.append('\'');
142                 break;
143               case Types.TIME:
144                 sb.append('\'');
145                 sb.append(DatatypeConverter.printTime((Calendar JavaDoc) param.getValue()));
146                 sb.append('\'');
147                 break;
148               default:
149                 throw new PMException("Invalid parameter type: " + param.getType());
150             }
151             break;
152           default:
153             sb.append(c);
154             break;
155         }
156       }
157     }
158     if (inStr) {
159       throw new PMException("Failed to parse query, expected trailing " + delim + " character: " + pQuery);
160     }
161     if (iter.hasNext()) {
162       throw new PMException("Number of actual parameters exceeds number of placeholder marks.");
163     }
164     return sb.toString();
165   }
166 }
167
Popular Tags