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.admin.datamodel.attributetype;
22  
23  import java.sql.SQLException;
24  import java.util.List;
25  
26  import org.efaps.admin.EFapsSystemConfiguration;
27  import org.efaps.admin.KernelSettings;
28  import org.efaps.admin.datamodel.Attribute;
29  import org.efaps.admin.user.Group;
30  import org.efaps.db.Context;
31  import org.efaps.db.wrapper.AbstractSQLInsertUpdate;
32  import org.efaps.util.EFapsException;
33  
34  /**
35   * The class is the attribute type representation for the group of a business
36   * object.
37   *
38   * @author The eFaps Team
39   * @version $Id$
40   */
41  public class GroupLinkType
42      extends PersonLinkType
43  {
44      /**
45       * Needed for serialization.
46       */
47      private static final long serialVersionUID = 1L;
48  
49      /**
50       * @param _insertUpdate insert / update SQL statement
51       * @param _attribute Attribute to be prepared
52       * @param _values values for the insert or update
53       * @throws SQLException if not exact one SQL column for the attribute is
54       *             defined of the company id could not be fetched
55       */
56      @Override
57      protected void prepare(final AbstractSQLInsertUpdate<?> _insertUpdate,
58                             final Attribute _attribute,
59                             final Object... _values)
60          throws SQLException
61      {
62          checkSQLColumnSize(_attribute, 1);
63          // if a value was explicitly set the value is used, else the first
64          // group for the logged in Person will be used
65          if ((_values != null) && (_values.length > 0) && _values[0] != null) {
66              if (_values[0] instanceof Long) {
67                  _insertUpdate.column(_attribute.getSqlColNames().get(0), (Long) _values[0]);
68              } else {
69                  _insertUpdate.column(_attribute.getSqlColNames().get(0), Long.parseLong(_values[0].toString()));
70              }
71          } else {
72              try {
73                  if (Context.getThreadContext().getPerson() != null
74                                  && !Context.getThreadContext().getPerson().getGroups().isEmpty()) {
75                      _insertUpdate.column(_attribute.getSqlColNames().get(0),
76                                      Context.getThreadContext().getPerson().getGroups().iterator().next());
77  
78                  } else if (EFapsSystemConfiguration.get().getAttributeValueAsBoolean(KernelSettings.ACTIVATE_GROUPS)) {
79                      throw new SQLException("Groups are activated in Systemconfiguration but no Group set.");
80                  }
81              } catch (final EFapsException e) {
82                  throw new SQLException("could not fetch group id", e);
83              }
84          }
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public Object readValue(final Attribute _attribute,
92                              final List<Object> _objectList)
93          throws EFapsException
94      {
95          Object ret = null;
96          final Object obj = _objectList.get(0);
97          if (obj != null) {
98              long id = 0;
99              if (obj instanceof Number) {
100                 id = ((Number) obj).longValue();
101             } else if (obj != null) {
102                 id = Long.parseLong(obj.toString());
103             }
104             ret = Group.get(id);
105         }
106         return ret;
107     }
108 
109 }