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.io.StringReader;
24 import java.io.StringWriter;
25 import java.sql.SQLException;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import javax.xml.bind.JAXBContext;
30 import javax.xml.bind.JAXBException;
31 import javax.xml.bind.Marshaller;
32 import javax.xml.bind.Unmarshaller;
33
34 import org.efaps.admin.datamodel.Attribute;
35 import org.efaps.admin.datamodel.IJaxb;
36 import org.efaps.admin.program.esjp.EFapsClassLoader;
37 import org.efaps.db.wrapper.AbstractSQLInsertUpdate;
38 import org.efaps.util.EFapsException;
39
40
41
42
43
44
45
46 public class JaxbType
47 extends AbstractType
48 {
49
50
51
52
53 private static final long serialVersionUID = 1L;
54
55 @Override
56 public Object readValue(final Attribute _attribute,
57 final List<Object> _objectList)
58 throws EFapsException
59 {
60 Object ret = null;
61 if (_objectList.size() < 1) {
62 ret = null;
63 } else {
64 final List<Object> list = new ArrayList<Object>();
65 for (final Object object : _objectList) {
66 String str = null;
67 if (object instanceof String) {
68 str = (String) object;
69 } else if (object != null) {
70 str = object.toString();
71 }
72 if (str != null) {
73 list.add(getObject4String(_attribute, str));
74 }
75 }
76 ret = list.isEmpty() ? null : (list.size() > 1 ? list : list.get(0));
77 }
78 return ret;
79 }
80
81 @Override
82 protected void prepare(final AbstractSQLInsertUpdate<?> _insertUpdate,
83 final Attribute _attribute,
84 final Object... _values)
85 throws SQLException
86 {
87 checkSQLColumnSize(_attribute, 1);
88 _insertUpdate.column(_attribute.getSqlColNames().get(0), eval(_attribute, _values));
89 }
90
91
92
93
94
95
96
97 protected String eval(final Attribute _attribute,
98 final Object[] _value)
99 throws SQLException
100 {
101 String ret = null;
102 if (_value == null) {
103 ret = null;
104 } else if ((_value[0] instanceof String) && (((String) _value[0]).length() > 0)) {
105 ret = (String) _value[0];
106 } else {
107 try {
108 final Object object = _value[0];
109 if (object != null) {
110 final Class<?> clazz = Class.forName(_attribute.getClassName(), false,
111 EFapsClassLoader.getInstance());
112 final IJaxb jaxb = (IJaxb) clazz.newInstance();
113 final JAXBContext jc = JAXBContext.newInstance(jaxb.getClasses());
114 final Marshaller marshaller = jc.createMarshaller();
115 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
116 final StringWriter writer = new StringWriter();
117 marshaller.marshal(_value[0], writer);
118 ret = writer.toString();
119 }
120 } catch (final ClassNotFoundException e) {
121 throw new SQLException("ClassNotFoundException", e);
122 } catch (final InstantiationException e) {
123 throw new SQLException("InstantiationException", e);
124 } catch (final IllegalAccessException e) {
125 throw new SQLException("IllegalAccessException", e);
126 } catch (final JAXBException e) {
127 throw new SQLException("JAXBException", e);
128 }
129 }
130 return ret;
131 }
132
133
134
135
136
137
138
139 protected Object getObject4String(final Attribute _attribute,
140 final String _str)
141 throws EFapsException
142 {
143 Object ret = null;
144 try {
145 if (_str != null && !_str.isEmpty()) {
146 final Class<?> clazz = Class.forName(_attribute.getClassName(), false, EFapsClassLoader.getInstance());
147 final IJaxb jaxb = (IJaxb) clazz.newInstance();
148 final JAXBContext jc = JAXBContext.newInstance(jaxb.getClasses());
149 final Unmarshaller unmarshaller = jc.createUnmarshaller();
150 final StringReader reader = new StringReader(_str);
151 ret = unmarshaller.unmarshal(reader);
152 }
153 } catch (final ClassNotFoundException e) {
154 throw new EFapsException("ClassNotFoundException", e);
155 } catch (final InstantiationException e) {
156 throw new EFapsException("InstantiationException", e);
157 } catch (final IllegalAccessException e) {
158 throw new EFapsException("IllegalAccessException", e);
159 } catch (final JAXBException e) {
160 throw new EFapsException("JAXBException", e);
161 }
162 return ret;
163 }
164 }