use of org.compiere.model.MRequest in project adempiere by adempiere.
the class RequestProcessor method processRequests.
// doWork
/**************************************************************************
* Process requests.
* Scheduled - are they due?
*/
private void processRequests() {
/**
* Due Requests
*/
String sql = "SELECT * FROM R_Request " + "WHERE DueType='" + MRequest.DUETYPE_Scheduled + "' AND Processed='N'" + " AND DateNextAction < SysDate" + " AND AD_Client_ID=?";
if (m_model.getR_RequestType_ID() != 0)
sql += " AND R_RequestType_ID=?";
PreparedStatement pstmt = null;
int count = 0;
int countEMails = 0;
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_model.getAD_Client_ID());
if (m_model.getR_RequestType_ID() != 0)
pstmt.setInt(2, m_model.getR_RequestType_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
MRequest request = new MRequest(getCtx(), rs, null);
request.setDueType();
if (request.isDue()) {
if (request.getRequestType().isEMailWhenDue()) {
if (sendEmail(request, "RequestDue")) {
request.setDateLastAlert();
countEMails++;
}
}
request.saveEx();
count++;
}
}
rs.close();
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
} finally {
DB.close(pstmt);
}
m_summary.append("New Due #").append(count);
if (countEMails > 0)
m_summary.append(" (").append(countEMails).append(" EMail)");
m_summary.append(" - ");
/**
* Overdue Requests.
* Due Requests - are they overdue?
*/
sql = "SELECT * FROM R_Request r " + "WHERE r.DueType='" + MRequest.DUETYPE_Due + "' AND r.Processed='N'" + " AND AD_Client_ID=?" + " AND EXISTS (SELECT * FROM R_RequestType rt " + "WHERE r.R_RequestType_ID=rt.R_RequestType_ID" + " AND (r.DateNextAction+rt.DueDateTolerance) < SysDate)";
if (m_model.getR_RequestType_ID() != 0)
sql += " AND r.R_RequestType_ID=?";
count = 0;
countEMails = 0;
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_model.getAD_Client_ID());
if (m_model.getR_RequestType_ID() != 0)
pstmt.setInt(2, m_model.getR_RequestType_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
MRequest request = new MRequest(getCtx(), rs, null);
request.setDueType();
if (request.isOverdue()) {
if (request.getRequestType().isEMailWhenOverdue() && !TimeUtil.isSameDay(request.getDateLastAlert(), null)) {
if (sendEmail(request, "RequestDue")) {
request.setDateLastAlert();
countEMails++;
}
}
request.saveEx();
count++;
}
}
rs.close();
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
} finally {
DB.close(pstmt);
}
m_summary.append("New Overdue #").append(count);
if (countEMails > 0)
m_summary.append(" (").append(countEMails).append(" EMail)");
m_summary.append(" - ");
/**
* Send (over)due alerts
*/
if (m_model.getOverdueAlertDays() > 0) {
sql = "SELECT * FROM R_Request " + "WHERE Processed='N'" + " AND AD_Client_ID=?" + " AND (DateNextAction+" + m_model.getOverdueAlertDays() + ") < SysDate" + " AND (DateLastAlert IS NULL";
if (m_model.getRemindDays() > 0)
sql += " OR (DateLastAlert+" + m_model.getRemindDays() + ") < SysDate";
sql += ")";
if (m_model.getR_RequestType_ID() != 0)
sql += " AND R_RequestType_ID=?";
count = 0;
countEMails = 0;
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_model.getAD_Client_ID());
if (m_model.getR_RequestType_ID() != 0)
pstmt.setInt(2, m_model.getR_RequestType_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
MRequest request = new MRequest(getCtx(), rs, null);
request.setDueType();
if (request.getRequestType().isEMailWhenOverdue() && (request.getDateLastAlert() == null || !TimeUtil.isSameDay(request.getDateLastAlert(), null))) {
if (sendEmail(request, "RequestAlert")) {
request.setDateLastAlert();
countEMails++;
}
}
request.saveEx();
count++;
}
rs.close();
} catch (SQLException e) {
log.log(Level.SEVERE, sql, e);
} finally {
DB.close(pstmt);
}
m_summary.append("Alerts #").append(count);
if (countEMails > 0)
m_summary.append(" (").append(countEMails).append(" EMail)");
m_summary.append(" - ");
}
/**
* Escalate
*/
if (m_model.getOverdueAssignDays() > 0) {
sql = "SELECT * FROM R_Request " + "WHERE Processed='N'" + " AND AD_Client_ID=?" + " AND IsEscalated='N'" + " AND (DateNextAction+" + m_model.getOverdueAssignDays() + ") < SysDate";
if (m_model.getR_RequestType_ID() != 0)
sql += " AND R_RequestType_ID=?";
count = 0;
countEMails = 0;
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_model.getAD_Client_ID());
if (m_model.getR_RequestType_ID() != 0)
pstmt.setInt(2, m_model.getR_RequestType_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
MRequest request = new MRequest(getCtx(), rs, null);
if (escalate(request))
count++;
}
rs.close();
} catch (SQLException e) {
log.log(Level.SEVERE, sql, e);
} finally {
DB.close(pstmt);
}
m_summary.append("Escalated #").append(count).append(" - ");
}
/**
* Send inactivity alerts
*/
if (m_model.getInactivityAlertDays() > 0) {
sql = "SELECT * FROM R_Request " + "WHERE Processed='N'" + " AND AD_Client_ID=?" + " AND (Updated+" + m_model.getInactivityAlertDays() + ") < SysDate" + " AND (DateLastAlert IS NULL";
if (m_model.getRemindDays() > 0)
sql += " OR (DateLastAlert+" + m_model.getRemindDays() + ") < SysDate";
sql += ")";
if (m_model.getR_RequestType_ID() != 0)
sql += " AND R_RequestType_ID=?";
count = 0;
countEMails = 0;
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, m_model.getAD_Client_ID());
if (m_model.getR_RequestType_ID() != 0)
pstmt.setInt(2, m_model.getR_RequestType_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
MRequest request = new MRequest(getCtx(), rs, null);
request.setDueType();
if (request.getDateLastAlert() == null || !TimeUtil.isSameDay(request.getDateLastAlert(), null)) {
if (sendEmail(request, "RequestInactive")) {
request.setDateLastAlert();
countEMails++;
}
request.saveEx();
count++;
}
}
rs.close();
} catch (SQLException e) {
log.log(Level.SEVERE, sql, e);
} finally {
DB.close(pstmt);
}
m_summary.append("Inactivity #").append(count);
if (countEMails > 0)
m_summary.append(" (").append(countEMails).append(" EMail)");
m_summary.append(" - ");
}
// Inactivity
}
use of org.compiere.model.MRequest in project adempiere by adempiere.
the class Request method createRequest.
/**
* Creates a new Request.
*
* @param request
* @param ctx
* @return new Request ID
*/
public static String createRequest(HttpServletRequest request, Properties ctx) {
String l_szTrxName = null;
String l_szReturn = null;
boolean l_bSuccess = true;
BigDecimal l_bdAmt = getParameterAsBD(request, "RequestAmt");
int l_nOrgID = getParameterAsInt(request, "AD_Org_ID");
//int l_nSalesRepID = getParameterAsInt(request, "SalesRep_ID");
int l_nRoleID = getParameterAsInt(request, "AD_Role_ID");
int l_nRReqTypeID = getParameterAsInt(request, "R_RequestType_ID");
int l_nRGroupID = getParameterAsInt(request, "R_Group_ID");
int l_nRCategoryID = getParameterAsInt(request, "R_Category_ID");
int l_nRReqRelID = getParameterAsInt(request, "R_RequestRelated_ID");
int l_nRStatusID = getParameterAsInt(request, "R_Status_ID");
int l_nRResolID = getParameterAsInt(request, "R_Resolution_ID");
int l_nBPartnerID = getParameterAsInt(request, "C_BPartner_ID");
int l_nUserID = getParameterAsInt(request, "AD_User_ID");
int l_nProjectID = getParameterAsInt(request, "C_Project_ID");
int l_nAssetID = getParameterAsInt(request, "A_Asset_ID");
int l_nOrderID = getParameterAsInt(request, "C_Order_ID");
int l_nInvoiceID = getParameterAsInt(request, "C_Invoice_ID");
int l_nProductID = getParameterAsInt(request, "M_Product_ID");
int l_nPaymentID = getParameterAsInt(request, "C_Payment_ID");
int l_nInOutID = getParameterAsInt(request, "M_InOut_ID");
int l_nRMAID = getParameterAsInt(request, "M_RMA_ID");
int l_nCampaignID = getParameterAsInt(request, "C_Campaign_ID");
/*
* Durchlauf der Parameter Werte. Dabei werden alle übergebenen Parameter
* als Columns in der MColumn gesucht und bei einem Treffer diese Werte
* dann über set_ValueOfColumn gesetzt
*/
/* MRequest l_newRequest = new MRequest(ctx, 0, l_szTrxName);
Enumeration l_eParameterNames = request.getParameterNames();
MColumn curColumn = null;
int l_nColumnID = 0;
int l_nRefID = 0;
while (l_eParameterNames.hasMoreElements()) {
String name = l_eParameterNames.nextElement().toString();
l_nColumnID = l_newRequest.get_ColumnIndex(name);
if (l_nColumnID > -1) {
curColumn = new MColumn(ctx, l_nColumnID, l_szTrxName);
l_nRefID = curColumn.getAD_Reference_ID();
if (l_nRefID == b_bool) {
l_newRequest.set_ValueOfColumn(l_nColumnID, getParameterAsBool(request, name));
} else if (l_nRefID == bd_amount || l_nRefID == bd_costsAndPrice || l_nRefID == bd_floatNumber || l_nRefID == bd_quantity) {
l_newRequest.set_ValueOfColumn(l_nColumnID, getParameterAsBD(request, name));
} else if (l_nRefID == d_date || l_nRefID == d_dateTime || l_nRefID == d_time) {
l_newRequest.set_ValueOfColumn(l_nColumnID, getParameterAsDate(request, name));
} else if (l_nRefID == i_id || l_nRefID == i_integer || l_nRefID == i_rowID || l_nRefID == i_searchField || l_nRefID == i_table || l_nRefID == i_table) {
l_newRequest.set_ValueOfColumn(l_nColumnID, getParameterAsInt(request, name));
} else {
l_newRequest.set_ValueOfColumn(l_nColumnID, getParameterAsString(request, name));
}
}
}
l_bSuccess &= l_newRequest.saveEx();
*/
MRequest newRequest = new MRequest(ctx, 0, l_szTrxName);
// values for values no fieldgroup
newRequest.setAD_Org_ID(l_nOrgID);
newRequest.setDueType(getParameterAsString(request, "DueType"));
newRequest.setR_RequestType_ID(l_nRReqTypeID);
newRequest.setR_Group_ID(l_nRGroupID);
newRequest.setR_Category_ID(l_nRCategoryID);
newRequest.setR_RequestRelated_ID(l_nRReqRelID);
newRequest.setR_Status_ID(l_nRStatusID);
newRequest.setR_Resolution_ID(l_nRResolID);
newRequest.setPriority(getParameterAsString(request, "Priority"));
newRequest.setPriorityUser(getParameterAsString(request, "PriorityUser"));
newRequest.setSummary(getParameterAsString(request, "Summary"));
newRequest.setConfidentialType(getParameterAsString(request, "ConfidentialType"));
newRequest.setIsInvoiced(getParameterAsBool(request, "IsInvoiced"));
// Mandatory values for fieldgroup Action
newRequest.setConfidentialTypeEntry(getParameterAsString(request, "ConfidentialTypeEntry"));
newRequest.setAD_Role_ID(l_nRoleID);
//newRequest.setSalesRep_ID(l_nSalesRepID);
// values for fieldgroup Reference
newRequest.setC_BPartner_ID(l_nBPartnerID);
newRequest.setAD_User_ID(l_nUserID);
newRequest.setC_Project_ID(l_nProjectID);
newRequest.setA_Asset_ID(l_nAssetID);
newRequest.setC_Order_ID(l_nOrderID);
newRequest.setC_Invoice_ID(l_nInvoiceID);
newRequest.setM_Product_ID(l_nProductID);
newRequest.setC_Payment_ID(l_nPaymentID);
newRequest.setM_InOut_ID(l_nInOutID);
newRequest.setM_RMA_ID(l_nRMAID);
newRequest.setRequestAmt(l_bdAmt);
newRequest.setC_Campaign_ID(l_nCampaignID);
l_bSuccess &= newRequest.save();
if (l_bSuccess) {
try {
DB.commit(true, l_szTrxName);
l_szReturn = "" + newRequest.get_ID();
} catch (Exception e) {
l_szReturn = e.getMessage();
}
}
return l_szReturn;
}
use of org.compiere.model.MRequest in project adempiere by adempiere.
the class RequestEMailProcessor method updateRequest.
private boolean updateRequest(int request_upd, Message msg) throws MessagingException, SQLException {
MRequest requp = new MRequest(getCtx(), request_upd, get_TrxName());
// Body as result
Address[] from = msg.getFrom();
requp.setResult("FROM: " + from[0].toString() + "\n" + getMessage(msg));
return requp.save();
}
use of org.compiere.model.MRequest in project adempiere by adempiere.
the class RequestEMailProcessor method createRequest.
// processInBox
/**
* Create request
* @param msg message
* @return
* @return Type of Message
* @throws MessagingException
*/
private boolean createRequest(Message msg) throws MessagingException, SQLException {
// Assign from variable
Address[] from = msg.getFrom();
String fromAddress;
// Carlos Ruiz <c_ruiz@myrealbox.com>
if (from[0].toString().indexOf('<') != -1 && from[0].toString().indexOf('>') != -1) {
fromAddress = from[0].toString().substring(from[0].toString().indexOf('<') + 1, from[0].toString().indexOf('>'));
log.info("fromAddress stripped: " + fromAddress);
} else {
fromAddress = from[0].toString();
}
// Message-ID as documentNo
String[] hdrs = msg.getHeader("Message-ID");
// Review if the e-mail was already created, comparing Message-ID+From+body
int retValuedup = 0;
String sqldup = "select r_request_id from r_request " + "where ad_client_id = ? " + "and documentno = ? " + "and startdate = ?";
PreparedStatement pstmtdup = null;
pstmtdup = DB.prepareStatement(sqldup, null);
pstmtdup.setInt(1, getAD_Client_ID());
pstmtdup.setString(2, hdrs[0].substring(0, 30));
pstmtdup.setTimestamp(3, new Timestamp(msg.getSentDate().getTime()));
ResultSet rsdup = pstmtdup.executeQuery();
if (rsdup.next())
retValuedup = rsdup.getInt(1);
rsdup.close();
pstmtdup.close();
pstmtdup = null;
if (retValuedup > 0) {
log.info("request already existed for msg -> " + hdrs[0]);
return true;
}
// Analyze subject if Re: find the original request by subject + e-mail and add an action
int request_upd = 0;
String sqlupd = "SELECT r_request_id " + " FROM r_request " + " WHERE ad_client_id = ? " + " AND summary LIKE 'FROM: ' || ? || '%' " + " AND ( documentno = " + " SUBSTR " + " (?, " + " INSTR " + " (?, " + " '<' " + " ) " + " ) " + " OR ( ? LIKE 'Re: %' " + " AND summary = " + " 'FROM: ' " + " || ? " + " || CHR (10) " + " || SUBSTR (?, 5) " + " ) " + " ) ";
PreparedStatement pstmtupd = null;
pstmtupd = DB.prepareStatement(sqlupd, null);
pstmtupd.setInt(1, getAD_Client_ID());
pstmtupd.setString(2, fromAddress);
pstmtupd.setString(3, msg.getSubject());
pstmtupd.setString(4, msg.getSubject());
pstmtupd.setString(5, msg.getSubject());
pstmtupd.setString(6, fromAddress);
pstmtupd.setString(7, msg.getSubject());
ResultSet rsupd = pstmtupd.executeQuery();
if (rsupd.next())
request_upd = rsupd.getInt(1);
rsupd.close();
pstmtupd.close();
pstmtupd = null;
if (request_upd > 0) {
log.info("msg -> " + hdrs[0] + " is an answer for req " + request_upd);
return updateRequest(request_upd, msg);
}
MRequest req = new MRequest(getCtx(), 0, get_TrxName());
// Subject as summary
req.setSummary("FROM: " + fromAddress + "\n" + msg.getSubject());
// Body as result
req.setResult("FROM: " + from[0].toString() + "\n" + getMessage(msg));
// Message-ID as documentNo
if (hdrs != null)
req.setDocumentNo(hdrs[0].substring(0, 30));
// Default request type for this process
if (R_RequestType_ID > 0)
req.setR_RequestType_ID(R_RequestType_ID);
else
req.setR_RequestType_ID();
// set Default sales representative
if (SalesRep_ID > 0)
req.setSalesRep_ID(SalesRep_ID);
// set Default role
if (AD_Role_ID > 0)
req.setAD_Role_ID(AD_Role_ID);
// Look for user via e-mail
if (from != null) {
int retValueu = -1;
String sqlu = "SELECT ad_user_id " + " FROM ad_user " + " WHERE UPPER (email) = UPPER (?) " + " AND ad_client_id = ?";
PreparedStatement pstmtu = null;
pstmtu = DB.prepareStatement(sqlu, null);
pstmtu.setString(1, fromAddress);
pstmtu.setInt(2, getAD_Client_ID());
ResultSet rsu = pstmtu.executeQuery();
if (rsu.next())
retValueu = rsu.getInt(1);
rsu.close();
pstmtu.close();
pstmtu = null;
if (retValueu > 0) {
req.setAD_User_ID(retValueu);
} else {
// set default user
if (AD_User_ID > 0)
req.setAD_User_ID(AD_User_ID);
}
}
// Look BP
if (req.getAD_User_ID() > 0) {
MUser us = new MUser(getCtx(), req.getAD_User_ID(), get_TrxName());
if (us.getC_BPartner_ID() > 0)
req.setC_BPartner_ID(us.getC_BPartner_ID());
}
if (req.getC_BPartner_ID() <= 0 && C_BPartner_ID > 0) {
// set default business partner
req.setC_BPartner_ID(C_BPartner_ID);
}
// Set start date as sent date of e-mail
req.setStartDate(new Timestamp(msg.getSentDate().getTime()));
// defaults priority Medium, confidentiality partner
if (p_DefaultConfidentiality != null) {
req.setConfidentialType(p_DefaultConfidentiality);
req.setConfidentialTypeEntry(p_DefaultConfidentiality);
}
if (p_DefaultPriority != null) {
req.setPriority(p_DefaultPriority);
req.setPriorityUser(p_DefaultPriority);
}
if (req.save(get_TrxName())) {
log.info("created request " + req.getR_Request_ID() + " from msg -> " + hdrs[0]);
// get simple attachments and attach to request
if (msg.isMimeType("multipart/*")) {
try {
Multipart mp = (Multipart) msg.getContent();
for (int i = 0, n = mp.getCount(); i < n; i++) {
Part part = mp.getBodyPart(i);
String disposition = part.getDisposition();
if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
MAttachment attach = req.createAttachment();
InputStream in = part.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
//1KiB buffer
final int BUF_SIZE = 1 << 8;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) > -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
byte[] bytes = out.toByteArray();
attach.addEntry(part.getFileName(), bytes);
attach.saveEx(get_TrxName());
}
}
} catch (IOException e) {
log.log(Level.FINE, "Error extracting attachments", e);
}
}
return true;
} else {
return false;
}
}
use of org.compiere.model.MRequest in project adempiere by adempiere.
the class WebInfo method getRequests.
// getRequestsAssigned
/**
* Get Requests
* @param own if true its own requests otherwise or
* @return Array of Requests
*/
public ArrayList<MRequest> getRequests(boolean own) {
m_infoMessage = null;
ArrayList<MRequest> list = new ArrayList<MRequest>();
String sql = null;
if (// All Requests
own)
sql = "SELECT * FROM R_Request r " + // #1
"WHERE r.C_BPartner_ID=?" + // #2
" AND (r.AD_User_ID=?" + " OR EXISTS (SELECT * FROM AD_User u " + // #3
"WHERE u.AD_User_ID=? AND r.C_BPartner_ID=u.C_BPartner_ID AND IsFullBPAccess='Y')" + " OR EXISTS (SELECT * FROM AD_User u INNER JOIN AD_UserBPAccess a ON (u.AD_User_ID=a.AD_User_ID) " + // #4
"WHERE u.AD_User_ID=? AND r.C_BPartner_ID=u.C_BPartner_ID" + " AND a.BPAccessType='R' AND (a.R_RequestType_ID IS NULL OR a.R_RequestType_ID=r.R_RequestType_ID)) ) " + "ORDER BY r.DocumentNo DESC";
else
// Open Requests of Sales Rep
sql = "SELECT * FROM R_Request " + "WHERE SalesRep_ID IN (SELECT AD_User_ID FROM AD_User WHERE C_BPartner_ID=?)" + " AND R_Status_ID IN (SELECT R_Status_ID FROM R_Status WHERE IsClosed='N')" + "ORDER BY DocumentNo DESC";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, getC_BPartner_ID());
if (own) {
pstmt.setInt(2, getAD_User_ID());
pstmt.setInt(3, getAD_User_ID());
pstmt.setInt(4, getAD_User_ID());
}
rs = pstmt.executeQuery();
while (rs.next()) list.add(new MRequest(m_ctx, rs, null));
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
log.fine("Own=" + own + " #" + list.size());
return list;
}
Aggregations