1   /*
2    * Copyright 2003 - 2014 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.value;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.efaps.admin.program.esjp.EFapsClassLoader;
27  import org.efaps.db.Instance;
28  import org.efaps.db.print.OneSelect;
29  import org.efaps.eql.IEsjpSelect;
30  import org.efaps.util.EFapsException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * TODO comment!
37   *
38   * @author The eFaps Team
39   * @version $Id$
40   */
41  public class EsjpValueSelect
42      extends InstanceValueSelect
43  {
44      /**
45       * Logging instance used in this class.
46       */
47      private static final Logger LOG = LoggerFactory.getLogger(EsjpValueSelect.class);
48  
49      /**
50       * Name of the esjp class.
51       */
52      private String className;
53  
54      /**
55       * Esjp to be invoked.
56       */
57      private IEsjpSelect esjp;
58  
59      /**
60       * List of parameters.
61       */
62      private final List<String> parameters = new ArrayList<>();
63  
64      /**
65       * @param _oneSelect
66       */
67      public EsjpValueSelect(final OneSelect _oneSelect,
68                             final String _esjp)
69      {
70          super(_oneSelect);
71          LOG.debug("instanciated EsjpValueSelect with: '{}'", _esjp);
72          final String[] paraAr = _esjp.split("(?<!\\\\),");
73          if (paraAr == null || paraAr.length == 0) {
74              LOG.error("Invalid esjp Value select: '{]'", _esjp);
75          } else {
76              this.className = paraAr[0];
77              for (int i = 1; i < paraAr.length; i++) {
78                  final String string = paraAr[i];
79                  if (string.startsWith("\"")) {
80                      this.parameters.add(string.substring(1, string.length() - 2));
81                  } else {
82                      this.parameters.add(string);
83                  }
84              };
85          }
86      }
87  
88      /**
89       * Method to get the value for the current object.
90       *
91       * @param _object current object
92       * @throws EFapsException on error
93       * @return object
94       */
95      @Override
96      public Object getValue(final Object _object)
97          throws EFapsException
98      {
99          final Instance inst = (Instance) super.getValue(_object);
100         if (this.esjp == null) {
101             try {
102                 final Class<?> clazz = Class.forName(this.className, false, EFapsClassLoader.getInstance());
103                 this.esjp = (IEsjpSelect) clazz.newInstance();
104                 final List<Instance> instances = new ArrayList<>();
105                 for (final Object obj : getOneSelect().getObjectList()) {
106                     instances.add((Instance) super.getValue(obj));
107                 }
108                 if (this.parameters.isEmpty()) {
109                     this.esjp.initialize(instances);
110                 } else {
111                     this.esjp.initialize(instances, this.parameters.toArray(new String[this.parameters.size()]));
112                 }
113             } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
114                 // TODO Auto-generated catch block
115                 e.printStackTrace();
116             }
117         }
118         return this.esjp.getValue(inst);
119     }
120 
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public String getValueType()
127     {
128         return "esjp";
129     }
130 
131 }