1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
36
37
38
39
40
41 public class GroupLinkType
42 extends PersonLinkType
43 {
44
45
46
47 private static final long serialVersionUID = 1L;
48
49
50
51
52
53
54
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
64
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
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 }