1   /*
2    * Copyright 2003 - 2013 The eFaps Team
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   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  
22  package org.efaps.db;
23  
24  import java.util.UUID;
25  
26  import org.efaps.admin.datamodel.Type;
27  import org.efaps.ci.CIAttribute;
28  import org.efaps.ci.CIType;
29  import org.efaps.util.cache.CacheReloadException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  
35  /**
36   * TODO comment!
37   *
38   * @author The eFaps Team
39   * @version $Id$
40   */
41  public class SelectBuilder
42  {
43      /**
44       * Logging instance used in this class.
45       */
46      private static final Logger LOG = LoggerFactory.getLogger(SelectBuilder.class);
47  
48      /**
49       * StringBuilder use as the base for building the String.
50       */
51      private final StringBuilder bldr = new StringBuilder();
52  
53      /**
54       * Standard constructor.
55       */
56      public SelectBuilder()
57      {
58      }
59  
60      /**
61       * Constructor setting a SelectBuilder as bases for this SelectBuilder.
62       * @param _selectBuilder SelectBuilder to be used
63       */
64      public SelectBuilder(final SelectBuilder _selectBuilder)
65      {
66          this.bldr.append(_selectBuilder.toString());
67      }
68  
69      /**
70       * Constructor setting a string as bases for this SelectBuilder.
71       * @param _selectBuilder SelectBuilder to be used
72       */
73      public SelectBuilder(final String _select)
74      {
75          this.bldr.append(_select);
76      }
77  
78      /**
79       * @param _attribute attribute to linkto
80       * @return this
81       */
82      public SelectBuilder linkto(final CIAttribute _attribute)
83      {
84          return linkto(_attribute.name);
85      }
86  
87      /**
88       * @param _attribute attribute to linkto
89       * @return this
90       */
91      public SelectBuilder linkto(final String _attribute)
92      {
93          addPoint();
94          this.bldr.append("linkto[").append(_attribute).append("]");
95          return this;
96      }
97  
98      /**
99       * @param _attribute    attribute to link from
100      * @return this
101      */
102     public SelectBuilder linkfrom(final CIAttribute _attribute)
103     {
104         return linkfrom(_attribute.ciType, _attribute);
105     }
106 
107     /**
108      * @param _type         type to link from
109      * @param _attribute    attribute to link from
110      * @return this
111      */
112     public SelectBuilder linkfrom(final CIType _type,
113                                   final CIAttribute _attribute)
114     {
115         return linkfrom(_type.getType().getName(), _attribute.name);
116     }
117 
118     /**
119      * @param _typeUUID     UUID of the type to link from
120      * @param _attribute    attribute to link from
121      * @return this
122      */
123     public SelectBuilder linkfrom(final UUID _typeUUID,
124                                   final CIAttribute _attribute)
125     {
126         String typeName = "";
127         try {
128             typeName = Type.get(_typeUUID).getName();
129         } catch (final CacheReloadException e) {
130             SelectBuilder.LOG.error("Could not read type from Cache for uuid: {}", _typeUUID);
131         }
132         return linkfrom(typeName, _attribute.name);
133     }
134 
135     /**
136      * @param _type         type to link from
137      * @param _attribute    attribute to linkto
138      * @return this
139      */
140     public SelectBuilder linkfrom(final String _type,
141                                   final String _attribute)
142     {
143         addPoint();
144         this.bldr.append("linkfrom[").append(_type).append("#").append(_attribute).append("]");
145         return this;
146     }
147 
148     /**
149      * @param _attribute attribute to be added
150      * @return this
151      */
152     public SelectBuilder attribute(final CIAttribute _attribute)
153     {
154         return attribute(_attribute.name);
155     }
156 
157     /**
158      * @param _attribute attribute to be added
159      * @return this
160      */
161     public SelectBuilder attribute(final String _attribute)
162     {
163         addPoint();
164         this.bldr.append("attribute[").append(_attribute).append("]");
165         return this;
166     }
167 
168 
169     /**
170      * add a point.
171      */
172     private void addPoint()
173     {
174         if (this.bldr.length() > 0) {
175             this.bldr.append(".");
176         }
177     }
178 
179     /**
180      * Add oid select part.
181      *
182      * @return this
183      */
184     public SelectBuilder oid()
185     {
186         addPoint();
187         this.bldr.append("oid");
188         return this;
189     }
190 
191     /**
192      * Add oid select part.
193      *
194      * @return this
195      */
196     public SelectBuilder instance()
197     {
198         addPoint();
199         this.bldr.append("instance");
200         return this;
201     }
202 
203     /**
204      * Add type select part.
205      *
206      * @return this
207      */
208     public SelectBuilder type()
209     {
210         addPoint();
211         this.bldr.append("type");
212         return this;
213     }
214 
215     /**
216      * Add type select part.
217      *
218      * @return this
219      */
220     public SelectBuilder value()
221     {
222         addPoint();
223         this.bldr.append("value");
224         return this;
225     }
226 
227     /**
228      * Add label select part.
229      *
230      * @return this
231      */
232     public SelectBuilder label()
233     {
234         addPoint();
235         this.bldr.append("label");
236         return this;
237     }
238 
239     /**
240      * Add id select part.
241      *
242      * @return this
243      */
244     public SelectBuilder id()
245     {
246         addPoint();
247         this.bldr.append("id");
248         return this;
249     }
250 
251     /**
252      * Add uuid select part.
253      *
254      * @return this
255      */
256     public SelectBuilder uuid()
257     {
258         addPoint();
259         this.bldr.append("uuid");
260         return this;
261     }
262 
263     /**
264      * Add name select part.
265      *
266      * @return this
267      */
268     public SelectBuilder name()
269     {
270         addPoint();
271         this.bldr.append("name");
272         return this;
273     }
274 
275     /**
276      * Add name select part.
277      *
278      * @return this
279      */
280     public SelectBuilder key()
281     {
282         addPoint();
283         this.bldr.append("key");
284         return this;
285     }
286 
287     /**
288      * Add name select part.
289      *
290      * @return this
291      */
292     public SelectBuilder status()
293     {
294         addPoint();
295         this.bldr.append("status");
296         return this;
297     }
298     /**
299      * Add file select part.
300      *
301      * @return this
302      */
303     public SelectBuilder file()
304     {
305         addPoint();
306         this.bldr.append("file");
307         return this;
308     }
309 
310     /**
311      * Add length select part.
312      *
313      * @return this
314      */
315     public SelectBuilder length()
316     {
317         addPoint();
318         this.bldr.append("length");
319         return this;
320     }
321 
322     /**
323      * Add base select part.
324      *
325      * @return this
326      */
327     public SelectBuilder base()
328     {
329         addPoint();
330         this.bldr.append("base");
331         return this;
332     }
333 
334     /**
335      * Add uom select part.
336      *
337      * @return this
338      */
339     public SelectBuilder uom()
340     {
341         addPoint();
342         this.bldr.append("uom");
343         return this;
344     }
345 
346     /**
347      * Add uuid select part.
348      *
349      * @return this
350      */
351     public SelectBuilder clazz()
352     {
353         addPoint();
354         this.bldr.append("class");
355         return this;
356     }
357 
358     /**
359      * @param _class attribute to be added
360      * @return this
361      */
362     public SelectBuilder clazz(final CIType _class)
363     {
364         return clazz(_class.getType().getName());
365     }
366 
367     /**
368      * @param _typeUUID UUID of the classification type
369      * @return this
370      */
371     public SelectBuilder clazz(final UUID _typeUUID)
372     {
373         String typeName = "";
374         try {
375             typeName = Type.get(_typeUUID).getName();
376         } catch (final CacheReloadException e) {
377             SelectBuilder.LOG.error("Could not read type from Cache for uuid: {}", _typeUUID);
378         }
379         return clazz(typeName);
380     }
381 
382     /**
383      * @param _class attribute to be added
384      * @return this
385      */
386     public SelectBuilder clazz(final String _class)
387     {
388         addPoint();
389         this.bldr.append("class[").append(_class).append("]");
390         return this;
391     }
392 
393     /**
394      * @param _attribute attribute to be added
395      * @return this
396      */
397     public SelectBuilder attributeset(final CIAttribute _attribute)
398     {
399         return attributeset(_attribute.name);
400     }
401 
402     /**
403      * @param _attribute attribute to be added
404      * @return this
405      */
406     public SelectBuilder attributeset(final String _attribute)
407     {
408         addPoint();
409         this.bldr.append("attributeset[").append(_attribute).append("]");
410         return this;
411     }
412 
413     /**
414      * Add format select part.
415      * @param _pattern pattern to be applied
416      *
417      * @return this
418      */
419     public SelectBuilder format(final String _pattern)
420     {
421         addPoint();
422         this.bldr.append("format[").append(_pattern).append("]");
423         return this;
424     }
425 
426     /**
427      * @return new SelectBuilder
428      */
429     public static SelectBuilder get()
430     {
431         return new SelectBuilder();
432     }
433 
434     /**
435      * Return the string of the StringBuilder.
436      * @return StringBuilder.toString()
437      */
438     @Override
439     public String toString()
440     {
441         return this.bldr.toString();
442     }
443 
444 }