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