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.update.event;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.efaps.admin.datamodel.Type;
27 import org.efaps.admin.event.EventType;
28 import org.efaps.ci.CIAdminProgram;
29 import org.efaps.db.Insert;
30 import org.efaps.db.Instance;
31 import org.efaps.db.InstanceQuery;
32 import org.efaps.db.QueryBuilder;
33 import org.efaps.db.Update;
34 import org.efaps.util.EFapsException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39 * This class defines a Event to be connected with an update.
40 *
41 * @author The eFaps Team
42 * @version $Id$
43 */
44 public class Event
45 {
46 /**
47 * Logging instance used to give logging information of this class.
48 */
49 private static final Logger LOG = LoggerFactory.getLogger(Event.class);
50
51 /**
52 * Property value depending on the property name for this trigger.
53 *
54 * @see #addProperty(String, String)
55 */
56 private final Map<String, String> properties = new HashMap<String, String>();
57
58 /**
59 * Event as defined in {@link EventType}.
60 */
61 private final EventType event;
62
63 /**
64 * Name of the program invoked in this trigger.
65 */
66 private final String program;
67
68 /**
69 * Name of the method to be invoked by this trigger.
70 */
71 private final String method;
72
73 /**
74 * Index of the trigger.
75 */
76 private final long index;
77
78 /**
79 * Name of the trigger.
80 */
81 private final String name;
82
83 /**
84 * Constructor of event for a trigger setting all instance variables.
85 *
86 * @param _name name of the event (if <code>null</code>, the event
87 * itself is used as name)
88 * @param _event event as defined in {@link EventType}
89 * @param _program name of the program invoked in this trigger
90 * @param _method name of the method to be invoked by this trigger (if
91 * <code>null</code>, method name <code>execute</code> is
92 * used)
93 * @param _index index of the trigger
94 */
95 public Event(final String _name,
96 final EventType _event,
97 final String _program,
98 final String _method,
99 final String _index)
100 {
101 this.name = (_name == null) ? _event.getName() : _name;
102 this.event = _event;
103 this.program = _program;
104 this.method = (_method == null) ? "execute" : _method;
105 if (_index == null) {
106 this.index = 0;
107 } else {
108 this.index = Long.valueOf(_index);
109 }
110 }
111
112 /**
113 * For given type defined with the instance parameter, this trigger is
114 * searched by typeID and index position. If the trigger exists, the
115 * trigger is updated. Otherwise the trigger is created.
116 *
117 * @param _instance type instance to update with this attribute
118 * @param _typeName name of the type to update
119 * @return Instance of the updated or inserted Trigger, null in case of
120 * error
121 */
122 public Instance updateInDB(final Instance _instance,
123 final String _typeName)
124 {
125 Instance ret = null;
126 try {
127 final long typeID = _instance.getId();
128 final long progID = getProgID(_typeName);
129
130 final QueryBuilder queryBldr = new QueryBuilder(Type.get(this.event.getName()));
131 queryBldr.addWhereAttrEqValue("Abstract", typeID);
132 queryBldr.addWhereAttrEqValue("Name", this.name);
133 final InstanceQuery query = queryBldr.getQuery();
134 query.executeWithoutAccessCheck();
135
136 final Update update;
137 if (query.next()) {
138 update = new Update(query.getCurrentValue());
139 } else {
140 update = new Insert(this.event.getName());
141 update.add("Abstract", typeID);
142 update.add("IndexPosition", this.index);
143 update.add("Name", this.name);
144 }
145 update.add("JavaProg", progID);
146 update.add("Method", this.method);
147 update.executeWithoutAccessCheck();
148
149 ret = update.getInstance();
150 update.close();
151 } catch (final EFapsException e) {
152 Event.LOG.error("updateInDB(Instance, String)", e);
153 //CHECKSTYLE:OFF
154 } catch (final Exception e) {
155 //CHECKSTYLE:ON
156 Event.LOG.error("updateInDB(Instance, String)", e);
157 }
158 return ret;
159 }
160
161 /**
162 * Get the id of the program.
163 *
164 * @param _typeName name of the type
165 * @return id of the program, 0 if not found
166 * @throws EFapsException if id of the program could not be fetched
167 */
168 private long getProgID(final String _typeName)
169 throws EFapsException
170 {
171 long id = 0;
172 final QueryBuilder queryBldr = new QueryBuilder(CIAdminProgram.Java);
173 queryBldr.addWhereAttrEqValue(CIAdminProgram.Java.Name, this.program);
174 final InstanceQuery query = queryBldr.getQuery();
175 query.executeWithoutAccessCheck();
176 if (query.next()) {
177 id = query.getCurrentValue().getId();
178 } else {
179 Event.LOG.error("type[" + _typeName + "]." + "Program [" + this.program + "]: " + "' not found");
180 }
181 return id;
182 }
183
184 /**
185 * Adds a property to this event.
186 *
187 * @param _name name of the property
188 * @param _value value of the property
189 * @see #properties
190 */
191 public void addProperty(final String _name,
192 final String _value)
193 {
194 this.properties.put(_name, _value);
195 }
196
197 /**
198 * Returns all properties of this event.
199 *
200 * @return Map containing the properties
201 * @see #properties
202 */
203 public Map<String, String> getProperties()
204 {
205 return this.properties;
206 }
207 }