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  package org.efaps.db.print;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.efaps.admin.datamodel.ui.FieldValue;
27  import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
28  import org.efaps.beans.ValueList;
29  import org.efaps.beans.ValueList.Token;
30  import org.efaps.db.Instance;
31  import org.efaps.util.EFapsException;
32  
33  /**
34   * Class is used as a wraper for a series of OneSelects as part of one phrase.
35   *
36   * @author The eFaps Team
37   * @version $Id$
38   */
39  public class Phrase
40  {
41  
42      /**
43       * Key for this Phrase.
44       */
45      private final String key;
46  
47      /**
48       * Phrase statement for this Phrase.
49       */
50      private final String phraseStmt;
51  
52      /**
53       * Mapping of Select statements to OneSelect.
54       */
55      private final Map<String, OneSelect> selectStmt2OneSelect = new HashMap<String, OneSelect>();
56  
57      /**
58       * ValueList to access the parser.
59       */
60      private final ValueList valueList;
61  
62      /**
63       * @param _key Key for this Phrase
64       * @param _phraseStmt Phrase statement for this Phrase
65       * @param _valueList ValueList to access the parser.
66       */
67      public Phrase(final String _key,
68                    final String _phraseStmt,
69                    final ValueList _valueList)
70      {
71          this.key = _key;
72          this.phraseStmt = _phraseStmt;
73          this.valueList = _valueList;
74      }
75  
76      /**
77       * Method to get the parsed value for this phrase.
78       *
79       * @param _instance Instance the phrase is build on
80       * @return parsed value
81       * @throws EFapsException on error
82       */
83      public String getPhraseValue(final Instance _instance)
84          throws EFapsException
85      {
86          final StringBuilder buf = new StringBuilder();
87  
88          for (final Token token : this.valueList.getTokens()) {
89              switch (token.getType()) {
90                  case EXPRESSION:
91                      final OneSelect oneselect = this.selectStmt2OneSelect.get(token.getValue());
92                      final Object value = oneselect.getObject();
93                      if (oneselect.getAttribute() != null) {
94                          buf.append(new FieldValue(null, oneselect.getAttribute(), value, _instance, null)
95                                          .getStringValue(TargetMode.VIEW));
96                      } else if (value != null) {
97                          buf.append(value);
98                      }
99                      break;
100                 case TEXT:
101                     buf.append(token.getValue());
102                     break;
103                 default:
104                     break;
105             }
106         }
107         return buf.toString();
108     }
109 
110     /**
111      * Add a oneselect to this Phrase.
112      *
113      * @param _oneselect OneSelect to add
114      */
115     public void addSelect(final OneSelect _oneselect)
116     {
117         this.selectStmt2OneSelect.put(_oneselect.getSelectStmt(), _oneselect);
118     }
119 
120     /**
121      * Getter method for instance variable {@link #key}.
122      *
123      * @return value of instance variable {@link #key}
124      */
125     public String getKey()
126     {
127         return this.key;
128     }
129 
130     /**
131      * Getter method for instance variable {@link #phraseStmt}.
132      *
133      * @return value of instance variable {@link #phraseStmt}
134      */
135     public String getPhraseStmt()
136     {
137         return this.phraseStmt;
138     }
139 }