PageRenderTime 9101ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Common_Classes/lwt/src/za/co/ucs/accsys/webmodule/WebProcess_FamilyRequisition.java

https://gitlab.com/accsys/netbeansprojects
Java | 617 lines | 384 code | 61 blank | 172 comment | 78 complexity | d60710afb3823f089a97729598775058 MD5 | raw file
  1. package za.co.ucs.accsys.webmodule;
  2. import za.co.ucs.accsys.peopleware.*;
  3. import za.co.ucs.lwt.db.*;
  4. import java.sql.*;
  5. /**
  6. * <p>
  7. * A WebProcess_FamilyRequisition is an extension of WebProcess that specializes
  8. * in Family Detail Requests
  9. * </p>
  10. */
  11. public final class WebProcess_FamilyRequisition extends WebProcess {
  12. /**
  13. * To change Family information, FamilyRequisition requires
  14. *
  15. * @param processDefinition - details of reporting structure, etc
  16. * @param owner - who created/is responsible for this process?
  17. * @param employee - which employee is requesting leave?
  18. * @param familyID - which family member? if Null, a new family member will
  19. * be created
  20. * @param firstName - String
  21. * @param secondName - String
  22. * @param surname
  23. * @param gender - String ('M' / 'F')
  24. * @param relation - String
  25. * @param birthDate - String ('yyyy/mm/dd')
  26. * @param medAidDependent - String ('Y','N');
  27. * @param commitType - ctInsert, ctUpdate, ctDelete
  28. * @param telHome - String
  29. * @param telWork - String
  30. * @param telCell - String
  31. * @param EMail - String
  32. */
  33. public WebProcess_FamilyRequisition(WebProcessDefinition processDefinition, Employee owner,
  34. Employee employee, Integer familyID, String firstName, String secondName, String surname,
  35. String gender, String relation, String birthDate, String telHome, String telWork,
  36. String telCell, String EMail, String medAidDependent, int commitType) {
  37. super(processDefinition, owner, employee);
  38. this.familyID = familyID;
  39. this.firstName = firstName;
  40. this.secondName = secondName;
  41. this.surname = surname;
  42. setGender(gender);
  43. this.relation = relation;
  44. this.birthDate = birthDate;
  45. this.medAidDependent = medAidDependent;
  46. this.telHome = telHome;
  47. this.telWork = telWork;
  48. this.telCell = telCell;
  49. this.EMail = EMail;
  50. this.commitType = commitType;
  51. // Is this a valid leave request?
  52. ValidationResult valid = validate();
  53. if (!valid.isValid()) {
  54. this.cancelProcess(valid.getInvalidReason());
  55. } else {
  56. this.escalateProcess(et_INITIAL, "");
  57. }
  58. }
  59. /**
  60. * This method should be implemented by each and every descendent of
  61. * WebProcess. It returns an instance of ValidationResult which contains
  62. * both the isValid() property as well as a reason in case of a non-valid
  63. * result.
  64. */
  65. @Override
  66. public ValidationResult validate() {
  67. if (isActive()) {
  68. Connection con = null;
  69. try {
  70. con = DatabaseObject.getNewConnectionFromPool();
  71. // If this is an update or delete, make sure the familyID is not null
  72. if ((commitType == ctUpdate) || (commitType == ctDelete)) {
  73. if (familyID == null) {
  74. DatabaseObject.releaseConnection(con);
  75. return (new ValidationResult(false, "Cannot update/delete family member. No member_id provided."));
  76. }
  77. }
  78. // If this is an update or delete, make sure there is a familyID and that it exists
  79. // in the database
  80. if (((commitType == ctUpdate) || (commitType == ctDelete)) && (familyID != null)) {
  81. ResultSet rs1 = DatabaseObject.openSQL(getFamilyMemberExistsStatement(), con);
  82. if (rs1.next()) {
  83. if (rs1.getInt(1) <= 0) {
  84. rs1.close();
  85. DatabaseObject.releaseConnection(con);
  86. return (new ValidationResult(false, "Family Member (member_id=" + familyID + ") does not exist in database."));
  87. }
  88. }
  89. rs1.close();
  90. }
  91. // If this is an insert, make sure the familyID has not yet been used
  92. // in the database
  93. if ((commitType == ctInsert) && (familyID != null)) {
  94. ResultSet rs1 = DatabaseObject.openSQL(getFamilyMemberExistsStatement(), con);
  95. if (rs1.next()) {
  96. if (rs1.getInt(1) > 0) {
  97. rs1.close();
  98. DatabaseObject.releaseConnection(con);
  99. return (new ValidationResult(false, "Family Member (member_id=" + familyID + ") already exist in database."));
  100. }
  101. }
  102. rs1.close();
  103. }
  104. } catch (SQLException e) {
  105. e.printStackTrace();
  106. } finally {
  107. DatabaseObject.releaseConnection(con);
  108. }
  109. }
  110. return (new ValidationResult(true, ""));
  111. }
  112. @Override
  113. public String toString() {
  114. StringBuilder result = new StringBuilder();
  115. if (commitType == ctDelete) {
  116. result.append("Delete Family Member: ").append(getSurname()).append(", ").append(getFirstName());
  117. }
  118. if (commitType == ctUpdate) {
  119. // Explanation of what changes were requested
  120. String detail = getChanges(new Family(getEmployee(), familyID), false);
  121. result.append("Change Detail Of Family Member ").append(getSurname()).append(", ").append(getFirstName()).append(detail);
  122. }
  123. if (commitType == ctInsert) {
  124. result.append("Add Family Member: ").append(getSurname()).append(", ").append(getFirstName());
  125. }
  126. return result.toString();
  127. }
  128. @Override
  129. public String toHTMLString() {
  130. StringBuilder result = new StringBuilder();
  131. result.append("Process Number: [").append(this.hashCode()).append("]<br>");
  132. if (commitType == ctDelete) {
  133. result.append("Delete Family Member: ").append(getSurname()).append(", ").append(getFirstName());
  134. }
  135. if (commitType == ctUpdate) {
  136. // Explanation of what changes were requested
  137. String detail = getChanges(new Family(getEmployee(), familyID), true);
  138. result.append("Change Detail Of Family Member ").append(getSurname()).append(", ").append(getFirstName()).append("<br>").append(detail);
  139. }
  140. if (commitType == ctInsert) {
  141. // Explanation of what changes were requested
  142. String detail = getChanges(new Family(getEmployee(), familyID), true);
  143. result.append("Add Family Member: ").append(getSurname()).append(", ").append(getFirstName()).append("<br>").append(detail);
  144. }
  145. return result.toString();
  146. }
  147. /**
  148. * Returns a String representation of the differences between the instance
  149. * of Family and the current properties in this WebProcess
  150. */
  151. private String getChanges(Family family, boolean formatInHTML) {
  152. StringBuffer result = new StringBuffer();
  153. if (formatInHTML) {
  154. try {
  155. if (isDifferent(getSurname(), family.getSurname())) {
  156. result.append("<tr><td>Surname:</td><td>From '").append(family.getSurname()).append("'</td><td>To '").append(getSurname()).append("'</td></tr>");
  157. }
  158. if (isDifferent(getFirstName(), family.getFirstName())) {
  159. result.append("<tr><td>First Name:</td><td>From '").append(family.getFirstName()).append("'</td><td>To '").append(getFirstName()).append("'</td></tr>");
  160. }
  161. if (isDifferent(getSecondName(), family.getSecondName())) {
  162. result.append("<tr><td>Second Name:</td><td>From '").append(family.getSecondName()).append("'</td><td>To '").append(getSecondName()).append("'</td></tr>");
  163. }
  164. if (isDifferent(getBirthDate(), family.getBirthDate())) {
  165. result.append("<tr><td>Birth Date:</td><td>From '").append(family.getBirthDate()).append("'</td><td>To '").append(getBirthDate()).append("'</td></tr>");
  166. }
  167. if (isDifferent(getGender(), family.getGender())) {
  168. String displayGender = "Unselected";
  169. String familyDisplayGender = "Unselected";
  170. if (!getGender().equals("")) {
  171. displayGender = getGender();
  172. }
  173. if (!family.getGender().equals("")) {
  174. familyDisplayGender = family.getGender();
  175. }
  176. result.append("<tr><td>Gender:</td><td>From '").append(familyDisplayGender).append("'</td><td>To '").append(displayGender).append("'</td></tr>");
  177. }
  178. if (isDifferent(getMedAidDependent(), family.getMedDependent())) {
  179. result.append("<tr><td>Med. Aid Dependent:</td><td>From '").append(family.getMedDependent()).append("'</td><td>To '").append(getMedAidDependent()).append("'</td></tr>");
  180. }
  181. if (isDifferent(getRelation(), family.getRelation())) {
  182. result.append("<tr><td>Relation:</td><td>From '").append(family.getRelation()).append("'</td><td>To '").append(getRelation()).append("'</td></tr>");
  183. }
  184. if (isDifferent(getTelHome(), family.getTelHome())) {
  185. result.append("<tr><td>Tel Home:</td><td>From '").append(family.getTelHome()).append("'</td><td>To '").append(getTelHome()).append("'</td></tr>");
  186. }
  187. if (isDifferent(getTelWork(), family.getTelWork())) {
  188. result.append("<tr><td>Tel Work:</td><td>From '").append(family.getTelWork()).append("'</td><td>To '").append(getTelWork()).append("'</td></tr>");
  189. }
  190. if (isDifferent(getTelCell(), family.getTelCell())) {
  191. result.append("<tr><td>Cell:</td><td>From '").append(family.getTelCell()).append("'</td><td>To '").append(getTelCell()).append("'</td></tr>");
  192. }
  193. if (isDifferent(getEMail(), family.getEMail())) {
  194. result.append("<tr><td>E-mail:</td><td>From '").append(family.getEMail()).append("'</td><td>To '").append(getEMail()).append("'</td></tr>");
  195. }
  196. } catch (java.lang.NullPointerException e) {
  197. result = new StringBuffer();
  198. result.append("<tr><td>The system is unable to determine the detail of this process.</td></tr>");
  199. }
  200. } else {
  201. try {
  202. String lineBreak = "\n";
  203. if (isDifferent(getSurname(), family.getSurname())) {
  204. result.append(lineBreak).append("Surname: From '").append(family.getSurname()).append("' To '").append(getSurname()).append("'");
  205. }
  206. if (isDifferent(getFirstName(), family.getFirstName())) {
  207. result.append(lineBreak).append("First Name: From '").append(family.getFirstName()).append("' To '").append(getFirstName()).append("'");
  208. }
  209. if (isDifferent(getSecondName(), family.getSecondName())) {
  210. result.append(lineBreak).append("Second Name: From '").append(family.getSecondName()).append("' To '").append(getSecondName()).append("'");
  211. }
  212. if (isDifferent(getBirthDate(), family.getBirthDate())) {
  213. result.append(lineBreak).append("Birth Date: From '").append(family.getBirthDate()).append("' To '").append(getBirthDate()).append("'");
  214. }
  215. if (isDifferent(getGender(), family.getGender())) {
  216. String displayGender = "Unselected";
  217. String familyDisplayGender = "Unselected";
  218. if (!getGender().equals("")) {
  219. displayGender = getGender();
  220. }
  221. if (!family.getGender().equals("")) {
  222. familyDisplayGender = family.getGender();
  223. }
  224. result.append(lineBreak).append("Gender: From '").append(familyDisplayGender).append("' To '").append(displayGender).append("'");
  225. }
  226. if (isDifferent(getMedAidDependent(), family.getMedDependent())) {
  227. result.append(lineBreak).append("Med. Aid Dependent: From '").append(family.getMedDependent()).append("' To '").append(getMedAidDependent()).append("'");
  228. }
  229. if (isDifferent(getRelation(), family.getRelation())) {
  230. result.append(lineBreak).append("Relation: From '").append(family.getRelation()).append("' To '").append(getRelation()).append("'");
  231. }
  232. if (isDifferent(getTelHome(), family.getTelHome())) {
  233. result.append(lineBreak).append("Tel Home: From '").append(family.getTelHome()).append("' To '").append(getTelHome()).append("'");
  234. }
  235. if (isDifferent(getTelWork(), family.getTelWork())) {
  236. result.append(lineBreak).append("Tel Work: From '").append(family.getTelWork()).append("' To '").append(getTelWork()).append("'");
  237. }
  238. if (isDifferent(getTelCell(), family.getTelCell())) {
  239. result.append(lineBreak).append("Cell: From '").append(family.getTelCell()).append("' To '").append(getTelCell()).append("'");
  240. }
  241. if (isDifferent(getEMail(), family.getEMail())) {
  242. result.append(lineBreak).append("E-mail: From '").append(family.getEMail()).append("' To '").append(getEMail()).append("'");
  243. }
  244. } catch (java.lang.NullPointerException e) {
  245. result = new StringBuffer();
  246. result.append("The system is unable to determine the detail of this process.");
  247. }
  248. }
  249. if (formatInHTML) {
  250. result.insert(0, "<table class=process_detail><font size='-3'>");
  251. result.append("</font></table>");
  252. }
  253. return result.toString();
  254. }
  255. private boolean isMedDependent() {
  256. return (getMedAidDependent().compareTo("Y") == 0);
  257. }
  258. /**
  259. * This statement gets executed when the process is to be written to the
  260. * database.
  261. */
  262. @Override
  263. String getCommitStatement() {
  264. String sqlString = "";
  265. if (commitType == ctInsert) {
  266. sqlString = getCommitAddCONSTANTStatement();
  267. }
  268. if (commitType == ctDelete) {
  269. sqlString = getCommitDeleteCONSTANTStatement();
  270. }
  271. if (commitType == ctUpdate) {
  272. sqlString = getCommitEditCONSTANTStatement();
  273. }
  274. return (prepareStatement(sqlString));
  275. }
  276. /**
  277. * Performs a ReplaceAll call without generating exceptions on NULL values
  278. *
  279. * @param regex - The regex search string
  280. * @param replacement - replacement string
  281. * @param string
  282. * @return
  283. */
  284. private String replaceAll(String regex, String replacement, String string) {
  285. if (string == null) {
  286. return ("");
  287. } else {
  288. return (string.replaceAll(regex, replacement));
  289. }
  290. }
  291. /**
  292. * Internal method to perform parameter replacements
  293. */
  294. private String prepareStatement(String aStatementWithParameters) {
  295. String sqlString = aStatementWithParameters;
  296. sqlString = sqlString.replaceAll(":companyID", getEmployee().getCompany().getCompanyID().toString());
  297. sqlString = sqlString.replaceAll(":employeeID", getEmployee().getEmployeeID().toString());
  298. sqlString = sqlString.replaceAll(":familyID", familyID.toString());
  299. sqlString = sqlString.replaceAll(":firstname", replaceAll("'", "`", firstName));
  300. sqlString = sqlString.replaceAll(":secondname", replaceAll("'", "`", secondName));
  301. sqlString = sqlString.replaceAll(":surname", replaceAll("'", "`", surname));
  302. if (birthDate.trim().length() == 0) {
  303. sqlString = sqlString.replaceAll(":birthdate", "null");
  304. } else {
  305. sqlString = sqlString.replaceAll(":birthdate", "'" + birthDate + "'");
  306. }
  307. sqlString = sqlString.replaceAll(":relation", replaceAll("'", "`", relation));
  308. sqlString = sqlString.replaceAll(":gender", replaceAll("'", "`", gender));
  309. sqlString = sqlString.replaceAll(":med_dependent_yn", medAidDependent);
  310. sqlString = sqlString.replaceAll(":telHome", replaceAll("'", "`", telHome));
  311. sqlString = sqlString.replaceAll(":telWork", replaceAll("'", "`", telWork));
  312. sqlString = sqlString.replaceAll(":telCell", replaceAll("'", "`", telCell));
  313. sqlString = sqlString.replaceAll(":EMail", replaceAll("'", "`", EMail));
  314. return sqlString;
  315. }
  316. /**
  317. * SQL Statement to be used to check weather the process is still valid.
  318. * These statements contain replacement characters that will be 'replaced'
  319. * before being fed to the DatabaseComponent.
  320. *
  321. * @COMPANY_ID, @EMPLOYEE_ID, etc.
  322. */
  323. private String getFamilyMemberExistsStatement() {
  324. return prepareStatement(getFamilyMemberExistsCONSTANTStatement());
  325. }
  326. /**
  327. * Getter for property birthDate.
  328. *
  329. * @return Value of property birthDate.
  330. *
  331. */
  332. public java.lang.String getBirthDate() {
  333. return birthDate;
  334. }
  335. /**
  336. * Setter for property birthDate.
  337. *
  338. * @param birthDate New value of property birthDate.
  339. *
  340. */
  341. public void setBirthDate(java.lang.String birthDate) {
  342. this.birthDate = birthDate;
  343. }
  344. /**
  345. * Getter for property familyID.
  346. *
  347. * @return Value of property familyID.
  348. *
  349. */
  350. public java.lang.Integer getFamilyID() {
  351. return familyID;
  352. }
  353. /**
  354. * Setter for property familyID.
  355. *
  356. * @param familyID New value of property familyID.
  357. *
  358. */
  359. public void setFamilyID(java.lang.Integer familyID) {
  360. this.familyID = familyID;
  361. }
  362. /**
  363. * Getter for property firstName.
  364. *
  365. * @return Value of property firstName.
  366. *
  367. */
  368. public java.lang.String getFirstName() {
  369. return firstName;
  370. }
  371. /**
  372. * Setter for property firstName.
  373. *
  374. * @param firstName New value of property firstName.
  375. *
  376. */
  377. public void setFirstName(java.lang.String firstName) {
  378. this.firstName = firstName;
  379. }
  380. /**
  381. * Getter for property gender.
  382. *
  383. * @return Value of property gender.
  384. *
  385. */
  386. public java.lang.String getGender() {
  387. return gender;
  388. }
  389. /**
  390. * Setter for property gender.
  391. *
  392. * @param gender New value of property gender.
  393. *
  394. */
  395. public void setGender(java.lang.String gender) {
  396. if (gender.contains("Unselected")) {
  397. gender = "";
  398. }
  399. this.gender = gender;
  400. }
  401. /**
  402. * Getter for property medAidDependent.
  403. *
  404. * @return Value of property medAidDependent.
  405. *
  406. */
  407. public java.lang.String getMedAidDependent() {
  408. return medAidDependent;
  409. }
  410. /**
  411. * Setter for property medAidDependent.
  412. *
  413. * @param medAidDependent New value of property medAidDependent.
  414. *
  415. */
  416. public void setMedAidDependent(java.lang.String medAidDependent) {
  417. this.medAidDependent = medAidDependent;
  418. }
  419. /**
  420. * Getter for property relation.
  421. *
  422. * @return Value of property relation.
  423. *
  424. */
  425. public java.lang.String getRelation() {
  426. return relation;
  427. }
  428. /**
  429. * Setter for property relation.
  430. *
  431. * @param relation New value of property relation.
  432. *
  433. */
  434. public void setRelation(java.lang.String relation) {
  435. this.relation = relation;
  436. }
  437. /**
  438. * Getter for property secondName.
  439. *
  440. * @return Value of property secondName.
  441. *
  442. */
  443. public java.lang.String getSecondName() {
  444. return secondName;
  445. }
  446. /**
  447. * Setter for property secondName.
  448. *
  449. * @param secondName New value of property secondName.
  450. *
  451. */
  452. public void setSecondName(java.lang.String secondName) {
  453. this.secondName = secondName;
  454. }
  455. /**
  456. * Getter for property surname.
  457. *
  458. * @return Value of property surname.
  459. *
  460. */
  461. public java.lang.String getSurname() {
  462. return surname;
  463. }
  464. /**
  465. * Setter for property surname.
  466. *
  467. * @param surname New value of property surname.
  468. *
  469. */
  470. public void setSurname(java.lang.String surname) {
  471. this.surname = surname;
  472. }
  473. public String getTelHome() {
  474. return telHome;
  475. }
  476. public void setTelHome(String telHome) {
  477. this.telHome = telHome;
  478. }
  479. public String getTelWork() {
  480. return telWork;
  481. }
  482. public void setTelWork(String telWork) {
  483. this.telWork = telWork;
  484. }
  485. public String getTelCell() {
  486. return telCell;
  487. }
  488. public void setTelCell(String telCell) {
  489. this.telCell = telCell;
  490. }
  491. public String getEMail() {
  492. return EMail;
  493. }
  494. public void setEMail(String EMail) {
  495. this.EMail = EMail;
  496. }
  497. // Fingerprint of previously compatible version
  498. /* In order to extract this UID, run the following command in the
  499. * folder where the current jwt.jar file resides BEFORE changing
  500. * the contents of the class.
  501. C:\Apache5\shared\lib>c:\j2sdk1.4.2_03\bin\serialver -classpath lwt.jar za.co.ucs.accsys.webmodule.WebProcess_FamilyRequisition
  502. za.co.ucs.accsys.webmodule.WebProcess_EmployeeContact: static final long serialVersionUID = 9094426540099516746L;
  503. */
  504. static final long serialVersionUID = -4053465466338838752L;
  505. private Integer familyID;
  506. private String firstName = "";
  507. private String secondName = "";
  508. private String surname = "";
  509. private String gender = "";
  510. private String relation = "";
  511. private String birthDate = "";
  512. private String medAidDependent = "N";
  513. private String telHome = "";
  514. private String telWork = "";
  515. private String telCell = "";
  516. private String EMail = "";
  517. public static int ctInsert = 0;
  518. public static int ctUpdate = 1;
  519. public static int ctDelete = 2;
  520. private int commitType = ctInsert;
  521. private EmployeeSelection initialEmployeeSelection;
  522. private String getFamilyMemberExistsCONSTANTStatement() {
  523. return ("select count(*) from e_family with (NOLOCK) where company_id=:companyID "
  524. + "and employee_id=:employeeID and family_id=:familyID");
  525. }
  526. private String getNextFamilyIDCONSTANTStatement() {
  527. return ("if (select count(*) from e_family with (NOLOCK) where company_id=:companyID "
  528. + "and employee_id=:employeeID )>0 then "
  529. + "select max(family_id)+1 from e_family where company_id=:companyID "
  530. + "and employee_id=:employeeID else select 1; end if; ");
  531. }
  532. private String getCommitAddCONSTANTStatement() {
  533. return ("if not exists(select * from e_family where company_id=:companyID "
  534. + "and employee_id=:employeeID and family_id=:familyID) then"
  535. + " insert into e_family (company_id, employee_id, family_id, firstname, secondname,"
  536. + " surname, gender, relation, birthdate, med_dependent_yn, Tel_Home, Tel_Work, Tel_Cell, E_Mail)"
  537. + " values (:companyID,:employeeID,:familyID,':firstname',':secondname',"
  538. + " ':surname',':gender',':relation',:birthdate,':med_dependent_yn', ':telHome', ':telWork', ':telCell', ':EMail');"
  539. + " end if;");
  540. }
  541. private String getCommitEditCONSTANTStatement() {
  542. return (" update e_family set firstname=':firstname', secondname=':secondname', "
  543. + " surname=':surname', gender=':gender',relation=':relation', "
  544. + " birthdate=:birthdate, med_dependent_yn=':med_dependent_yn', Tel_Home=':telHome', Tel_Work=':telWork', Tel_Cell=':telCell', E_Mail=':EMail' "
  545. + " where company_id=:companyID and employee_id=:employeeID and family_id=:familyID");
  546. }
  547. private String getCommitDeleteCONSTANTStatement() {
  548. return (" delete from e_family where company_id=:companyID and employee_id=:employeeID "
  549. + " and family_id=:familyID");
  550. }
  551. @Override
  552. public String getProcessName() {
  553. return "Family Detail Request";
  554. }
  555. } // end LeaveRequisitionProcess