📄 resourceutil.java.svn-base
字号:
package com.nsi.components.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.nsi.components.resource.ResourceInfo;
import com.nsi.components.resource.ResourceProfileInfo;
import com.nsi.components.util.info.RsDropInfo;
import com.nsi.constants.AppConstants;
import com.nsi.control.exceptions.NsiEventException;
import com.nsi.persistence.DataSrcUtil;
import com.nsi.persistence.IsqlDataSource;
import com.nsi.util.ValHelper;
import com.nsi.util.tsdate.TsDateUtil;
public final class ResourceUtil
{
private static Log log = LogFactory.getLog(ResourceUtil.class);
/**
* private constructor of ResourceUtil, prevent instantiation
*/
private ResourceUtil()
{
}
private static class ResourceUtilHolder
{
static final ResourceUtil resourceUtil = new ResourceUtil();
}
/**
* @return an instance of ResourceUtil
*/
public static ResourceUtil getInstance()
{
return ResourceUtilHolder.resourceUtil;
}
public RsDropInfo getSinglerecourd( String resourceid )
{
RsDropInfo info = new RsDropInfo();
Map<String,String> result = new HashMap<String,String>();
if(resourceid.equals(""))
{
resourceid = "0";
}
String sSql = "select resourceid, firstname, lastname, middlename, " +
"to_char(startdate, 'MON-DD-YYYY') as hiringdate, to_char(enddate, 'MON-DD-YYYY') as termdate " +
"from t_resource " +
"where resourceid ="+ resourceid + "";
try
{
IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
Connection conn = null;
try
{
conn = src.getConnection();
result = src.retrieveSingleRow(conn, sSql);
}
catch( SQLException se )
{
log.error( "getSinglerecourd() caught SQLException: " + se );
}
catch( Exception ex )
{
log.error( "getSinglerecourd() caught Exception: " + ex );
}
finally
{
src.closeConn(conn);
}
if(!result.isEmpty())
{
info.setResourceid(ValHelper.getInstance().getValue(result, "resourceid"));
info.setFirstname(ValHelper.getInstance().getValue(result, "firstname"));
info.setLastname(ValHelper.getInstance().getValue(result, "lastname"));
info.setMiddlename(ValHelper.getInstance().getValue(result, "middlename"));
info.setHiringdate(ValHelper.getInstance().getValue(result, "hiringdate"));
info.setTermdate(ValHelper.getInstance().getValue(result, "termdate"));
}
}
catch( NsiEventException nsiex )
{
log.error( "getSinglerecourd() caught NsiEventException: " + nsiex );
}
return info;
}
public List<RsDropInfo> getResourcelist()
{
List<RsDropInfo> list = new ArrayList<RsDropInfo>();
List<Map<String,String>> result = new ArrayList<Map<String,String>>();
String sSql = "select resourceid, firstname, lastname, middlename, " +
"to_char(startdate, 'MON-DD-YYYY') as hiringdate, to_char(enddate, 'MON-DD-YYYY') as termdate " +
"from t_resource " +
"order by firstname, middlename, lastname";
try
{
IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
Connection conn = null;
try
{
conn = src.getConnection();
result = src.executeRetrieve(conn, sSql);
}
catch( SQLException se )
{
log.error( "getResourceList() caught SQLException: " + se );
}
catch( Exception ex )
{
log.error( "getResourceList() caught Exception: " + ex );
}
finally
{
src.closeConn(conn);
}
if(!result.isEmpty())
{
int size = result.size();
for (int i = 0; i < size; i++)
{
Map<String,String> resultmap = result.get(i);
RsDropInfo info = new RsDropInfo();
info.setResourceid(ValHelper.getInstance().getValue(resultmap, "resourceid"));
info.setFirstname(ValHelper.getInstance().getValue(resultmap, "firstname"));
info.setLastname(ValHelper.getInstance().getValue(resultmap, "lastname"));
info.setMiddlename(ValHelper.getInstance().getValue(resultmap, "middlename"));
info.setHiringdate(ValHelper.getInstance().getValue(resultmap, "hiringdate"));
info.setTermdate(ValHelper.getInstance().getValue(resultmap, "termdate"));
list.add(info);
}
}
}
catch( NsiEventException nsiex )
{
log.error( "getResourceList() caught NsiEventException: " + nsiex );
}
return list;
}
public List<RsDropInfo> getActiveResourceList() throws NsiEventException
{
List<RsDropInfo> list = new ArrayList<RsDropInfo>();
List<Map<String,String>> result = new ArrayList<Map<String,String>>();
String sSql = "select r.resourceid, r.firstname, r.lastname, r.middlename, " +
"to_char(r.startdate, 'MON-DD-YYYY') as hiringdate, to_char(r.enddate, 'MON-DD-YYYY') as termdate " +
"from t_resource r, t_user u " +
"where u.bactive='T' and u.resourceid=r.resourceid " +
"order by firstname, middlename, lastname";
IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
Connection conn = null;
try
{
conn = src.getConnection();
result = src.executeRetrieve(conn, sSql);
}
catch( SQLException se )
{
log.error( "getCollection() caught SQLException: " + se );
}
catch( Exception ex )
{
log.error( "getCollection() caught Exception: " + ex );
}
finally
{
src.closeConn(conn);
}
if(!result.isEmpty())
{
int size = result.size();
for (int i = 0; i < size; i++)
{
Map<String,String> resultmap = result.get(i);
RsDropInfo info = new RsDropInfo();
info.setResourceid(ValHelper.getInstance().getValue(resultmap, "resourceid"));
info.setFirstname(ValHelper.getInstance().getValue(resultmap, "firstname"));
info.setLastname(ValHelper.getInstance().getValue(resultmap, "lastname"));
info.setMiddlename(ValHelper.getInstance().getValue(resultmap, "middlename"));
info.setHiringdate(ValHelper.getInstance().getValue(resultmap, "hiringdate"));
info.setTermdate(ValHelper.getInstance().getValue(resultmap, "termdate"));
list.add(info);
}
}
return list;
}
public List<RsDropInfo> getInActiveResourceList() throws NsiEventException
{
List<RsDropInfo> list = new ArrayList<RsDropInfo>();
List<Map<String,String>> result = new ArrayList<Map<String,String>>();
String sSql = "select resourceid, firstname, lastname, middlename, " +
"to_char(startdate, 'MON-DD-YYYY') as hiringdate, to_char(enddate, 'MON-DD-YYYY') as termdate " +
"from t_resource r, t_user u " +
"where u.bactive='F' and u.resourceid=r.resourceid " +
"by firstname, middlename, lastname";
IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
Connection conn = null;
try
{
conn = src.getConnection();
result = src.executeRetrieve(conn, sSql);
}
catch( SQLException se )
{
log.error( "getCollection() caught SQLException: " + se );
}
catch( Exception ex )
{
log.error( "getCollection() caught Exception: " + ex );
}
finally
{
src.closeConn(conn);
}
if(!result.isEmpty())
{
int size = result.size();
for (int i = 0; i < size; i++)
{
Map<String,String> resultmap = result.get(i);
RsDropInfo info = new RsDropInfo();
info.setResourceid(ValHelper.getInstance().getValue(resultmap, "resourceid"));
info.setFirstname(ValHelper.getInstance().getValue(resultmap, "firstname"));
info.setLastname(ValHelper.getInstance().getValue(resultmap, "lastname"));
info.setMiddlename(ValHelper.getInstance().getValue(resultmap, "middlename"));
info.setHiringdate(ValHelper.getInstance().getValue(resultmap, "hiringdate"));
info.setTermdate(ValHelper.getInstance().getValue(resultmap, "termdate"));
list.add(info);
}
}
return list;
}
public void mailling(ResourceInfo info) throws NsiEventException
{
try
{
Properties properties = System.getProperties();
properties.put(AppConstants.MAILPROVIDER, AppConstants.SMTPHOST);
Session session = Session.getInstance(properties, null);
MimeMessage mimemessage = new MimeMessage(session);
InternetAddress internetaddress = new InternetAddress(AppConstants.FROM_ADMIN);
mimemessage.setFrom(internetaddress);
Map<String, String> hrRecord = getHRRecord("RESPROF");
String email = getHREmail(hrRecord);
String name = getHRName(hrRecord);
mimemessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email));
mimemessage.setSubject("Resource Creation Notification");
mimemessage.setSentDate(new Date());
String text2 = name + ": \n\n";
text2 = text2 + "A new resource has been created in Resource Management System (RMS). \n\n";
text2 = text2 + "Resource Name: " + info.getFirstname() + " " + info.getLastname() + "\n\n";
text2 = text2 + "Start Date: " + info.getStartdate() + "\n\n";
text2 = text2 + "End Date: " + info.getEnddate() + "\n\n";
text2 = text2 + "Employee ID: " + (info.getEmployeenum() == "null" ? "" : info.getEmployeenum()) + "\n\n";
text2 = text2 + "Employee Type: " + getEmpTypeDesc(info.getEmpltypeid()) + "\n\n";
text2 = text2 + "Address: " + info.getAddress() + "\n\n";
text2 = text2 + " " + info.getCity() + " " + info.getProvince() + "\n\n";
text2 = text2 + " " + info.getPostalcode() + "\n\n";
text2 = text2 + "Home Phone: " + info.getHomephone() + "\n\n";
text2 = text2 + "Mobile Phone: " + info.getMobilephone() + "\n\n";
text2 = text2 + "Remarks: " + info.getRemarks() + "\n\n";
text2 = text2 + "\n\n";
text2 = text2 + "Regards,\n\n";
text2 = text2 + "RMS Admin\n\n";
mimemessage.setText(text2);
System.out.println("Resource Email:" + text2);
Transport.send(mimemessage);
}
catch(AddressException addressexception)
{
log.error("mailling() caught AddressException: ", addressexception);
throw new NsiEventException("mailling() caught AddressException: ", addressexception);
}
catch(SendFailedException sendFailedException)
{
log.error("mailling() caught SendFailedException: ", sendFailedException);
throw new NsiEventException("mailling() caught SendFailedException: ", sendFailedException);
}
catch(MessagingException messagingexception)
{
log.error("mailling() caught MessagingException: ", messagingexception);
throw new NsiEventException("mailling() caught MessagingException: ", messagingexception);
}
}
private String getEmpTypeDesc(String emplTypeID) throws NsiEventException
{
return EmployeeTypeUtil.getInstance().getEmployeeTypeDesc(emplTypeID);
}
public Map<String, String> getHRRecord(String refString) throws NsiEventException
{
Map<String, String> result = new HashMap<String, String>();
String sSql = "select lastname, firstname, email from ct_param where ftn_nam ='" + refString + "'";
IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
Connection conn = null;
try
{
conn = src.getConnection();
result = src.retrieveSingleRow(conn, sSql);
}
catch(SQLException se)
{
log.error("getHRRecord() caught SQLException: " , se);
throw new NsiEventException("getHRRecord() caught Exception: ", se);
}
catch(Exception ex)
{
log.error("getHRRecord() caught Exception: " , ex);
throw new NsiEventException("getHRRecord() caught Exception: ", ex);
}
finally
{
src.closeConn(conn);
}
return result;
}
public String getHRName(Map<String, String> result) throws NsiEventException
{
String name = AppConstants.EMPTY_STRING;
try
{
if(!result.isEmpty())
{
name = result.get("firstname") + " " + result.get("lastname");
}
}
catch(Exception ex)
{
throw new NsiEventException("getHRName() caught Exception: ", ex);
}
return name;
}
public String getHREmail(Map<String, String> result) throws NsiEventException
{
String email = AppConstants.EMPTY_STRING;
try
{
if(!result.isEmpty())
{
email = result.get("email");
}
}
catch(Exception ex)
{
throw new NsiEventException("getHREmail() caught Exception: ", ex);
}
return email;
}
public boolean isLoginNameExist(String loginname) throws NsiEventException
{
String sSql = "select loginname from t_user where loginname ='" + loginname + "'";
boolean existed = false;
IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
Connection conn = null;
try
{
conn = src.getConnection();
List<Map<String,String>> result = src.executeRetrieve(conn, sSql);
src.closeConn(conn);
if(!result.isEmpty())
{
existed = true;
}
}
catch( SQLException se )
{
log.error( "isLoginNameExist() caught SQLException: " , se );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -