IDEMPIERE-840 Improvement to Request model class - Using OSGi event instead of model validator
This commit is contained in:
parent
7a8378e716
commit
b63c3777e1
|
@ -282,6 +282,7 @@ Import-Package: com.sun.mail.auth;version="1.4.5",
|
|||
org.eclipse.osgi.framework.console;version="1.1.0",
|
||||
org.eclipse.osgi.service.datalocation,
|
||||
org.osgi.framework,
|
||||
org.osgi.service.cm;version="1.3.0",
|
||||
org.osgi.service.component;version="1.1.0",
|
||||
org.osgi.service.event;version="1.2.0",
|
||||
org.osgi.util.tracker;version="1.5.0",
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0">
|
||||
|
||||
<OCD description="Request Service" name="Request Service" id="org.adempiere.base.event.request.service">
|
||||
<AD name="Ignore Request Types" id="ignoreRequestTypes" required="true" type="String" default=""/>
|
||||
</OCD>
|
||||
|
||||
<Designate pid="org.adempiere.base.event.request.service">
|
||||
<Object ocdref="org.adempiere.base.event.request.service"/>
|
||||
</Designate>
|
||||
|
||||
</metatype:MetaData>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.adempiere.base.event.RequestPropertyService">
|
||||
<implementation class="org.adempiere.base.event.RequestPropertyService"/>
|
||||
<reference bind="bindConfigurationAdmin" cardinality="1..1" interface="org.osgi.service.cm.ConfigurationAdmin" name="ConfigurationAdmin" policy="static" unbind="unbindConfigurationAdmin"/>
|
||||
</scr:component>
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" configuration-policy="require" name="org.adempiere.base.event.request.service">
|
||||
<implementation class="org.adempiere.base.event.RequestEventHandler"/>
|
||||
<property name="service.ranking" type="Integer" value="1"/>
|
||||
<service>
|
||||
<provide interface="org.osgi.service.cm.ManagedService"/>
|
||||
<provide interface="org.adempiere.base.event.RequestEventHandler"/>
|
||||
</service>
|
||||
<property name="service.pid" type="String" value="org.adempiere.base.event.request.service"/>
|
||||
</scr:component>
|
|
@ -29,6 +29,8 @@ bin.includes = META-INF/,\
|
|||
OSGI-INF/defaultpaymentprocessorfactory.xml,\
|
||||
OSGI-INF/broadcastutil.xml,\
|
||||
OSGI-INF/requesteventhandler.xml,\
|
||||
OSGI-INF/requestservice.xml,\
|
||||
OSGI-INF/requestpropertyservice.xml,\
|
||||
schema/
|
||||
output.base.jar = build/
|
||||
src.includes = schema/
|
||||
|
|
|
@ -20,6 +20,8 @@ import java.sql.PreparedStatement;
|
|||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Dictionary;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.adempiere.exceptions.DBException;
|
||||
|
@ -28,6 +30,7 @@ import org.compiere.model.MClient;
|
|||
import org.compiere.model.MNote;
|
||||
import org.compiere.model.MRequest;
|
||||
import org.compiere.model.MRequestAction;
|
||||
import org.compiere.model.MRequestType;
|
||||
import org.compiere.model.MRequestUpdate;
|
||||
import org.compiere.model.MUser;
|
||||
import org.compiere.model.PO;
|
||||
|
@ -37,6 +40,9 @@ import org.compiere.util.CLogger;
|
|||
import org.compiere.util.DB;
|
||||
import org.compiere.util.Env;
|
||||
import org.compiere.util.Msg;
|
||||
import org.compiere.util.Util;
|
||||
import org.osgi.service.cm.ConfigurationException;
|
||||
import org.osgi.service.cm.ManagedService;
|
||||
import org.osgi.service.event.Event;
|
||||
|
||||
/**
|
||||
|
@ -44,7 +50,7 @@ import org.osgi.service.event.Event;
|
|||
* @author Nur Yasmin
|
||||
*
|
||||
*/
|
||||
public class RequestEventHandler extends AbstractEventHandler
|
||||
public class RequestEventHandler extends AbstractEventHandler implements ManagedService
|
||||
{
|
||||
private static CLogger s_log = CLogger.getCLogger (RequestEventHandler.class);
|
||||
|
||||
|
@ -71,6 +77,11 @@ public class RequestEventHandler extends AbstractEventHandler
|
|||
if (po.get_TableName().equals(I_R_Request.Table_Name))
|
||||
{
|
||||
MRequest r = (MRequest) po;
|
||||
|
||||
MRequestType rt = r.getRequestType();
|
||||
if (ignoreRequestTypes.contains(rt.getName()))
|
||||
return;
|
||||
|
||||
if (topic.equals(IEventTopics.PO_BEFORE_NEW) || topic.equals(IEventTopics.PO_BEFORE_CHANGE))
|
||||
beforeSaveRequest(r, topic.equals(IEventTopics.PO_BEFORE_NEW));
|
||||
else if (topic.equals(IEventTopics.PO_AFTER_NEW) || topic.equals(IEventTopics.PO_AFTER_CHANGE))
|
||||
|
@ -89,7 +100,7 @@ public class RequestEventHandler extends AbstractEventHandler
|
|||
registerTableEvent(IEventTopics.PO_AFTER_CHANGE, I_R_Request.Table_Name);
|
||||
}
|
||||
|
||||
public static String beforeSaveRequest(MRequest r, boolean newRecord)
|
||||
private String beforeSaveRequest(MRequest r, boolean newRecord)
|
||||
{
|
||||
// New
|
||||
if (newRecord)
|
||||
|
@ -208,7 +219,7 @@ public class RequestEventHandler extends AbstractEventHandler
|
|||
return null;
|
||||
}
|
||||
|
||||
public static String afterSaveRequest(MRequest r, boolean newRecord)
|
||||
private String afterSaveRequest(MRequest r, boolean newRecord)
|
||||
{
|
||||
// Initial Mail
|
||||
if (newRecord)
|
||||
|
@ -223,7 +234,7 @@ public class RequestEventHandler extends AbstractEventHandler
|
|||
* @param columnName column
|
||||
* @return true if changes
|
||||
*/
|
||||
public static boolean checkChange (MRequest r, MRequestAction ra, String columnName)
|
||||
public boolean checkChange (MRequest r, MRequestAction ra, String columnName)
|
||||
{
|
||||
if (r.is_ValueChanged(columnName))
|
||||
{
|
||||
|
@ -242,7 +253,7 @@ public class RequestEventHandler extends AbstractEventHandler
|
|||
* Send Update EMail/Notices
|
||||
* @param list list of changes
|
||||
*/
|
||||
public static void sendNotices(MRequest r, ArrayList<String> list)
|
||||
private void sendNotices(MRequest r, ArrayList<String> list)
|
||||
{
|
||||
// Subject
|
||||
String subject = Msg.translate(r.getCtx(), "R_Request_ID")
|
||||
|
@ -393,7 +404,7 @@ public class RequestEventHandler extends AbstractEventHandler
|
|||
* @param serverAddress server address
|
||||
* @return Mail Trailer
|
||||
*/
|
||||
public static String getMailTrailer(MRequest r, String serverAddress)
|
||||
private String getMailTrailer(MRequest r, String serverAddress)
|
||||
{
|
||||
StringBuffer sb = new StringBuffer("\n").append(MRequest.SEPARATOR)
|
||||
.append(Msg.translate(r.getCtx(), "R_Request_ID"))
|
||||
|
@ -404,4 +415,22 @@ public class RequestEventHandler extends AbstractEventHandler
|
|||
sb.append(" from ").append(serverAddress);
|
||||
return sb.toString();
|
||||
} // getMailTrailer
|
||||
|
||||
public static final String IGNORE_REQUEST_TYPES = "ignoreRequestTypes";
|
||||
private static ArrayList<String> ignoreRequestTypes = new ArrayList<String>();
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public void updated(Dictionary properties) throws ConfigurationException {
|
||||
if (properties != null) {
|
||||
String p = (String) properties.get(IGNORE_REQUEST_TYPES);
|
||||
if (!Util.isEmpty(p)) {
|
||||
ignoreRequestTypes.clear();
|
||||
|
||||
StringTokenizer st = new StringTokenizer(p, ";");
|
||||
while (st.hasMoreTokens())
|
||||
ignoreRequestTypes.add(st.nextToken().trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
/******************************************************************************
|
||||
* Copyright (C) 2013 Elaine *
|
||||
* Copyright (C) 2013 Trek Global
|
||||
* This program is free software; you can redistribute it and/or modify it *
|
||||
* under the terms version 2 of the GNU General Public License as published *
|
||||
* by the Free Software Foundation. This program is distributed in the hope *
|
||||
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
||||
* See the GNU General Public License for more details. *
|
||||
* You should have received a copy of the GNU General Public License along *
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., *
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
|
||||
*****************************************************************************/
|
||||
|
||||
package org.adempiere.base.event;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Dictionary;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.adempiere.base.event.RequestEventHandler;
|
||||
import org.compiere.util.CLogger;
|
||||
import org.compiere.util.Ini;
|
||||
import org.compiere.util.Util;
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.FrameworkUtil;
|
||||
import org.osgi.service.cm.Configuration;
|
||||
import org.osgi.service.cm.ConfigurationAdmin;
|
||||
|
||||
/**
|
||||
* Request property service
|
||||
* @author Elaine
|
||||
*
|
||||
*/
|
||||
public class RequestPropertyService {
|
||||
|
||||
private static final String REQUESTEVENTHANDLER_PROPERTIES = "requesteventhandler.properties";
|
||||
|
||||
private static final CLogger logger = CLogger.getCLogger(RequestPropertyService.class);
|
||||
|
||||
public RequestPropertyService() {
|
||||
}
|
||||
|
||||
public void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
|
||||
readProperties(configurationAdmin);
|
||||
}
|
||||
|
||||
public void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
|
||||
|
||||
}
|
||||
|
||||
private void readProperties(ConfigurationAdmin service) {
|
||||
File file = new File(Ini.getAdempiereHome(), REQUESTEVENTHANDLER_PROPERTIES);
|
||||
if (file.exists()) {
|
||||
Properties p = new Properties();
|
||||
FileInputStream is = null;
|
||||
try {
|
||||
is = new FileInputStream(file);
|
||||
p.load(is);
|
||||
String ignoreRequesTypes = p.getProperty(RequestEventHandler.IGNORE_REQUEST_TYPES);
|
||||
|
||||
if (!Util.isEmpty(ignoreRequesTypes)) {
|
||||
Configuration configuration = service.getConfiguration("org.adempiere.base.event.request.service");
|
||||
if (configuration.getProperties() == null) {
|
||||
Dictionary<String, Object> map = new Hashtable<String, Object>();
|
||||
map.put(RequestEventHandler.IGNORE_REQUEST_TYPES, ignoreRequesTypes);
|
||||
configuration.update(map);
|
||||
} else {
|
||||
Bundle bundle = FrameworkUtil.getBundle(RequestEventHandler.class);
|
||||
String bundleLocation = bundle.getLocation();
|
||||
String configLocation = configuration.getBundleLocation();
|
||||
if (!bundleLocation.equals(configLocation)) {
|
||||
configuration.setBundleLocation(bundleLocation);
|
||||
configuration.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.log(Level.WARNING, REQUESTEVENTHANDLER_PROPERTIES + " not found.", e);
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Error reading " + REQUESTEVENTHANDLER_PROPERTIES, e);
|
||||
} finally {
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (Exception ex) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue