1 package com.instantbank.component.job.ejb;
2
3 import java.util.ArrayList;
4 import java.util.Hashtable;
5 import java.util.Collection;
6
7 import java.rmi.RemoteException;
8 import javax.ejb.EntityBean;
9 import javax.ejb.EntityContext;
10 import javax.ejb.EJBException;
11 import javax.ejb.FinderException;
12 import javax.ejb.CreateException;
13 import javax.ejb.DuplicateKeyException;
14 import javax.ejb.RemoveException;
15
16 import com.instantbank.component.job.ejb.JobDAO;
17 import com.instantbank.component.job.model.JobModel;
18 import com.instantbank.component.job.model.JobSELECTelement;
19 import com.instantbank.component.job.model.JobWHEREelement;
20 import com.instantbank.component.job.model.JobORDERelement;
21 import com.instantbank.component.job.util.Alias;
22 import com.instantbank.component.job.util.Field;
23 import com.instantbank.component.job.util.ParticipantTable;
24 import com.instantbank.component.job.util.AuxGenerateSQLtext;
25 import com.instantbank.component.job.util.JoinElement;
26
27 import com.instantbank.common.utilcomponents.DAOException;
28 import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
29 import com.instantbank.common.utilcomponents.LetterTemplateExceptionMessage;
30 import com.instantbank.common.utilcomponents.Debug;
31
32
38 public class JobEJB
39 implements EntityBean {
40
41
42 private JobModel jobModel;
43
44
45
46
49 private EntityContext context;
50
51
54 private JobDAO jobDao;
55
56
59 private AuxGenerateSQLtext auxSQL;
60
61
64 private Debug debug = null;
65
66
67
68
71 public JobEJB() { }
72
73
74
75
80 public void setEntityContext(EntityContext sc) {
81 debug = new Debug();
82 debug.setDebugginOn(true);
83 debug.setPreMessage("** JobEJB: ");
84 debug.println("setEntityContext");
85 this.context = sc;
86 }
87
88
89 public void unsetEntityContext() {
90 debug.println("unsetEntityContext");
91 }
92
93
94
97 public void ejbActivate() {
98 debug.println("ejbActivate");
99 try {
100 getDao();
101 }
102 catch(DAOException se) {
103 throw new RuntimeException(se.getMessage());
104 }
105
106
107 Long jobIDcontext = (Long)context.getPrimaryKey();
108 debug.println("jobIDcontext=" + jobIDcontext);
109 jobModel.setJobId(jobIDcontext);
110 }
111
112
113
116 public void ejbPassivate() {
117 debug.println("ejbPassivate");
118 jobDao = null;
119 debug = null;
120 jobModel.setJobId(null);
121 }
122
123
124
125
126
140 public Long ejbCreate(String companyId, Long userId, String name,
141 long ftpPrimaryId, long ftpAlternateId)
142 throws CreateException {
143 debug.println("ejbCreate");
144
145 Long jobId = null;
146
147
148 jobModel = new JobModel
149 (null , companyId, name,
150 LetterTemplateGlobals.DEFAULT_JOB_FREQUENCY,
151 LetterTemplateGlobals.DEFAULT_JOB_STATUS, null ,
152 ftpPrimaryId, ftpAlternateId, userId, null ,
153 LetterTemplateGlobals.UNDEF ,
154 LetterTemplateGlobals.DEFAULT_QUEUE_TYPE, "" ,
155 "" , null ,
156
157 null, null, null);
158
159 try {
160 getDao();
161 jobDao.ejbCreate(jobModel);
162 jobModel = jobDao.getModel();
163 jobId = jobModel.getJobId();
164 }
165 catch(DAOException se) {
166 context.setRollbackOnly();
167 throw new CreateException(se.getMessage());
168 }
169 return jobId;
170 }
171
172
173
183 public void ejbPostCreate(String companyId, Long userId, String name,
184 long ftpPrimaryId, long ftpAlternateId)
185 throws CreateException {
186 debug.println("ejbPostCreate");
187 }
188
189
190
197 public Long ejbFindByPrimaryKey(Long jobId) throws FinderException {
198 debug.println("ejbFindByPrimaryKey");
199 try {
200 getDao();
201 jobModel = new JobModel();
202 debug.println("jobModel instanced with new JobModel()");
203 Long jobIdanswer = jobDao.ejbFindByPrimaryKey(jobId);
204 jobModel.setJobId(jobIdanswer);
205 debug.println("jobModel.setJobId() executed");
206 return jobIdanswer;
207 }
208 catch(DAOException se) {
209 throw new FinderException(se.getMessage());
210 }
211 }
212
213
214
222 public Long ejbFindByName(String companyId, String name) throws FinderException {
223 debug.println("ejbFindByName");
224 try {
225 getDao();
226 jobModel = new JobModel();
227 debug.println("jobModel instanced with new JobModel()");
228 Long jobIdanswer = jobDao.ejbFindByName(companyId, name);
229 jobModel.setJobId(jobIdanswer);
230 debug.println("jobModel.setJobId() executed");
231 return jobIdanswer;
232 }
233 catch(DAOException se) {
234 throw new FinderException(se.getMessage());
235 }
236 }
237
238
239
244 public void ejbRemove() throws RemoveException {
245 debug.println("ejbRemove");
246 boolean jobWasDeleted;
247
248 try {
249 getDao();
250 jobWasDeleted = jobDao.ejbRemove(jobModel.getJobId());
251 if(!jobWasDeleted) {
252 debug.println("before throw new DAOException");
253 throw new DAOException
254 (LetterTemplateExceptionMessage.PROBLEM_JOB_CANNOT_BE_DELETED);
255 }
256 }
257 catch(DAOException se) {
258
259 throw new RemoveException(se.getMessage());
260 }
261 }
262
263
264
267 public void ejbLoad() {
268 debug.println("ejbLoad");
269 try {
270 getDao();
271 jobDao.ejbLoad(jobModel.getJobId());
272 jobModel = jobDao.getModel();
273 }
274 catch(DAOException se) {
275 throw new EJBException(se.getMessage());
276 }
277 }
278
279
280
283 public void ejbStore() {
284 debug.println("ejbStore");
285 try {
286 getDao();
287 jobDao.ejbStore(jobModel);
288 jobModel = jobDao.getModel();
289 }
290 catch(DAOException se) {
291 throw new EJBException(se.getMessage());
292 }
293 }
294
295
296
297
298
303 public JobModel getState() {
304 debug.println("getState");
305 jobDao.setJobMasterchanged(false);
306 jobDao.setJobSELECTchanged(false);
307 jobDao.setJobWHEREchanged(false);
308 jobDao.setJobORDERchanged(false);
309 return jobModel;
310 }
311
312
313
318 public void setState(JobModel jobModel) {
319 debug.println("setState");
320 this.jobModel = jobModel;
321 jobDao.setJobMasterchanged(true);
322 jobDao.setJobSELECTchanged(true);
323 jobDao.setJobWHEREchanged(true);
324 jobDao.setJobORDERchanged(true);
325 }
326
327
328
334 public void setTemplate(long templateCode) {
335 debug.println("setTemplate");
336 jobModel.setTemplateCode(templateCode);
337 jobDao.setJobMasterchanged(true);
338 jobDao.setJobSELECTchanged(false);
339 jobDao.setJobWHEREchanged(false);
340 jobDao.setJobORDERchanged(false);
341 }
342
343
344
350 public void setDescription(String description) {
351 debug.println("setDescription");
352 jobModel.setDescription(description);
353 jobDao.setJobMasterchanged(true);
354 jobDao.setJobSELECTchanged(false);
355 jobDao.setJobWHEREchanged(false);
356 jobDao.setJobORDERchanged(false);
357 }
358
359
360
366 public void setJobSELECT(Collection jobSELECT) {
367 debug.println("setJobSELECT");
368 jobModel.setJobSELECT(jobSELECT);
369 jobDao.setJobMasterchanged(true);
370 jobDao.setJobSELECTchanged(true);
371 jobDao.setJobWHEREchanged(false);
372 jobDao.setJobORDERchanged(false);
373 }
374
375
376
382 public void setJobWHERE(Collection jobWHERE) {
383 debug.println("setJobWHERE");
384 jobModel.setJobWHERE(jobWHERE);
385 jobDao.setJobMasterchanged(true);
386 jobDao.setJobSELECTchanged(false);
387 jobDao.setJobWHEREchanged(true);
388 jobDao.setJobORDERchanged(false);
389 }
390
391
392
398 public void setJobORDER(Collection jobORDER) {
399 debug.println("setJobORDER");
400 jobModel.setJobORDER(jobORDER);
401 jobDao.setJobMasterchanged(true);
402 jobDao.setJobSELECTchanged(false);
403 jobDao.setJobWHEREchanged(false);
404 jobDao.setJobORDERchanged(true);
405 }
406
407
408
420 public String generateSQLtext(Hashtable systemFields,
421 Hashtable systemAlias, String rootType) {
422 debug.println("generateSQLtext");
423 String answer = "";
424 ArrayList participantTables = new ArrayList();
425
426 auxSQL = new AuxGenerateSQLtext(systemFields, systemAlias, rootType);
427
428 String strSELECT = buildSELECT();
429 String strWHERE_JOINS = buildWHERE_JOINS(participantTables);
430 String strWHERE_CONDITIONS = buildWHERE_CONDITIONS();
431 String strFROM = buildFROM(participantTables);
432 String strORDER = buildORDER();
433 String strWHERE = (!strWHERE_JOINS.equals(""))
434 ? " WHERE " + strWHERE_JOINS + " AND " + strWHERE_CONDITIONS
435 : " WHERE " + strWHERE_CONDITIONS;
436
437 answer = strSELECT
438 + strFROM
439 + strWHERE
440 + strORDER;
441
442 jobModel.setSqlText(answer);
443 jobDao.setJobMasterchanged(true);
444 jobDao.setJobSELECTchanged(false);
445 jobDao.setJobWHEREchanged(false);
446 jobDao.setJobORDERchanged(false);
447 return answer;
448 }
449
450
451
452
453
458 private String buildSELECT() {
459 debug.println("buildSELECT");
460 JobSELECTelement element;
461 String answer = "SELECT UNIQUE " + auxSQL.beginSELECT();
462 ArrayList jobSELECT = (ArrayList)jobModel.getJobSELECT();
463 int sizeSELECT = (jobSELECT != null) ? jobSELECT.size() : 0;
464 answer += (sizeSELECT > 0) ? ", " : "";
465
466 for(int i = 0; i < sizeSELECT; i++) {
467 element = (JobSELECTelement)(jobSELECT.get(i));
468 long fieldId = element.getFieldId();
469 String alias = auxSQL.alias(fieldId);
470 if(alias.equals(LetterTemplateGlobals.SYSDATE)) {
471 answer += " TRUNC(" + alias + ")";
472 }
473 else {
474 answer += " "
475 + alias
476 + "."
477 + auxSQL.fieldColumn(fieldId)
478 + " "
479 + auxSQL.rename(fieldId)
480 ;
481 }
482 answer += (i != sizeSELECT - 1) ? "," : "";
483 }
484 return answer;
485 }
486
487
488
496 private String buildWHERE_JOINS(ArrayList participantTables) {
497 debug.println("buildWHERE_JOINS");
498 ArrayList joinList = new ArrayList();
499 JoinElement joinElement = null;
500 int posInsert;
501 int posLastInsert;
502 ParticipantTable participantTable = null;
503 String alias;
504 boolean joinCheck;
505
506 String answer = "";
507
508
509
510 String aliasRoot = auxSQL.rootParticipant();
511 participantTables.add(new ParticipantTable(aliasRoot, true));
512 debug.println("participantTables.add with true: " + aliasRoot);
513
514 ArrayList jobWHERE = (ArrayList)jobModel.getJobWHERE();
515 int sizeWHERE = (jobWHERE != null) ? jobWHERE.size() : 0;
516 for(int i = 0; i < sizeWHERE; i++) {
517 JobWHEREelement element = (JobWHEREelement)(jobWHERE.get(i));
518 long fieldId = element.getFieldId();
519 alias = auxSQL.alias(fieldId);
520 if(auxSQL.findAlias(participantTables, alias) < 0) {
521 participantTables.add(new ParticipantTable(alias, false));
522 debug.println("participantTables.add with false: " + alias);
523 }
524 }
525
526 ArrayList jobSELECT = (ArrayList)jobModel.getJobSELECT();
527 int sizeSELECT = jobSELECT.size();
528 for(int i = 0; i < sizeSELECT; i++) {
529 JobSELECTelement element = (JobSELECTelement)(jobSELECT.get(i));
530 long fieldId = element.getFieldId();
531 alias = auxSQL.alias(fieldId);
532 if(!alias.equals(LetterTemplateGlobals.SYSDATE)
533 && auxSQL.findAlias(participantTables, alias) < 0) {
534 participantTables.add(new ParticipantTable(alias, false));
535 debug.println("participantTables.add with false: " + alias);
536 }
537 }
538
539
540
541 for(int i = 0; i < participantTables.size(); i++) {
542 participantTable = (ParticipantTable)(participantTables.get(i));
543 alias = participantTable.getAlias();
544 joinCheck = participantTable.getJoinCheck();
545
546 if(!joinCheck) {
547 participantTable.setJoinCheck(true);
548 joinElement = auxSQL.buildJoinElement(alias);
549 int posFound = auxSQL.findParent(joinList, alias);
550 if(posFound >= 0) {
551 joinList.add(posFound + 1, joinElement);
552 }
553 else {
554 joinList.add(joinElement);
555 posLastInsert = joinList.size() - 1;
556
557 String aliasParent = auxSQL.parentTableAlias(alias);
558 int k = i;
559 while(!aliasParent.equals(aliasRoot)) {
560 int j = auxSQL.findAlias(participantTables, aliasParent);
561 if(j < 0) {
562 participantTables.add(k + 1, new ParticipantTable(aliasParent, false));
563 debug.println("aliasParent " + aliasParent
564 + " added to participantTables with false");
565 k = k + 1;
566 }
567 else {
568 k = j;
569 }
570 participantTable = (ParticipantTable)(participantTables.get(k));
571 alias = participantTable.getAlias();
572 participantTable.setJoinCheck(true);
573 joinElement = auxSQL.buildJoinElement(alias);
574 posInsert = (posLastInsert == 0) ? 0 : (posLastInsert - 1);
575 joinList.add(posInsert, joinElement);
576 posLastInsert = posInsert;
577 aliasParent = auxSQL.parentTableAlias(alias);
578 }
579 }
580 }
581 }
582
583
584
585 int sizeJoinList = joinList.size();
586
587 for(int i = 0; i < sizeJoinList; i++) {
588 joinElement = (JoinElement)(joinList.get(i));
589 String parentAlias = joinElement.getParentAlias();
590 String parentColumn = joinElement.getParentColumn();
591 String sonAlias = joinElement.getSonAlias();
592 String sonColumn = joinElement.getSonColumn();
593 String joinType = joinElement.getJoinType();
594 answer += " ("
595 + parentAlias
596 + "."
597 + parentColumn
598 + " = "
599 + sonAlias
600 + "."
601 + sonColumn
602 + " "
603 + joinType
604 + ")"
605 ;
606 answer += (i != sizeJoinList - 1) ? " AND " : "";
607 }
608 return answer;
609 }
610
611
612
617 private String buildWHERE_CONDITIONS() {
618 debug.println("buildWHERE_CONDITIONS");
619 JobWHEREelement element;
620
621 String answer = "";
622
623 ArrayList jobWHERE = (ArrayList)jobModel.getJobWHERE();
624 int sizeWHERE = (jobWHERE != null) ? jobWHERE.size() : 0;
625
626 if(sizeWHERE > 0) {
627 answer += " (((";
628 }
629
630 for(int i = 0; i < sizeWHERE; i++) {
631 element = (JobWHEREelement)(jobWHERE.get(i));
632 long fieldId = element.getFieldId();
633 String ruleOperator = element.getRuleOperator();
634 String value = element.getValue();
635 String connector = element.getConnector();
636
637 String condition =
638 auxSQL.alias(fieldId)
639 + "."
640 + auxSQL.fieldColumn(fieldId)
641 + " "
642 + ruleOperator
643 + " "
644 + auxSQL.value(value, ruleOperator, fieldId);
645
646 if(connector.equals(LetterTemplateGlobals.OR_CONNECTOR)) {
647 answer += condition + " OR ";
648 }
649 else if(connector.equals(LetterTemplateGlobals.AND_CONNECTOR)) {
650 answer += condition + ") AND (";
651 }
652 else if(connector.equals(LetterTemplateGlobals.ORTHE_CONNECTOR)) {
653 answer += condition + ")) OR ((";
654 }
655 else if(connector.equals(LetterTemplateGlobals.EMPTY_CONNECTOR)
656 && (i != sizeWHERE - 1)) {
657 answer += condition + "))) AND (((";
658 }
659 else {
660 answer += condition + ")))";
661 }
662 }
663
664 answer += (answer.equals("")) ? "" : " AND ";
665 answer += auxSQL.endWHERE(jobModel.getCompanyId());
666 return answer;
667 }
668
669
670
678 private String buildFROM(ArrayList participantTables) {
679 debug.println("buildFROM");
680 ParticipantTable element;
681 String elementFROM;
682 String answer = " FROM ";
683
684 int sizeParticipants = participantTables.size();
685 for(int i = 0; i < sizeParticipants; i++) {
686 element = (ParticipantTable)(participantTables.get(i));
687 String alias = element.getAlias();
688 if((auxSQL.category(alias))
689 .equals(LetterTemplateGlobals.NORMAL_FIELDS)) {
690 elementFROM = " " + auxSQL.realTable(alias);
691 }
692 else if((auxSQL.category(alias))
693 .equals(LetterTemplateGlobals.TEMPORAL_FIELDS)) {
694 elementFROM = " (" + auxSQL.builtTable(alias) + ") " + alias;
695 }
696 else {
697 elementFROM = " " + auxSQL.realTable(alias) + " " + alias;
698 }
699 answer += elementFROM;
700 answer += (i != sizeParticipants - 1) ? "," : "";
701 }
702 return answer;
703 }
704
705
706
714 private String buildORDER() {
715 debug.println("buildORDER");
716 JobORDERelement element;
717 String answer = "";
718
719 ArrayList jobORDER = (ArrayList)jobModel.getJobORDER();
720 int sizeORDER = (jobORDER != null) ? jobORDER.size() : 0;
721 answer += (sizeORDER > 0) ? " ORDER BY " : "";
722
723 for(int i = 0; i < sizeORDER; i++) {
724 element = (JobORDERelement)(jobORDER.get(i));
725 long fieldId = element.getFieldId();
726 String direction = element.getDirection();
727 String alias = auxSQL.alias(fieldId);
728 if(alias.equals(LetterTemplateGlobals.SYSDATE)) {
729 answer += " " + alias + " " + auxSQL.direction(direction);
730 }
731 else {
732 answer += " "
733 + alias
734 + "."
735 + auxSQL.fieldColumn(fieldId)
736 + " "
737 + auxSQL.direction(direction)
738 ;
739 }
740 answer += (i != sizeORDER - 1) ? "," : "";
741 }
742 return answer;
743 }
744
745
746
752 private JobDAO getDao()
753 throws DAOException {
754 if(jobDao == null) {
755 jobDao = new JobDAO();
756 }
757 return jobDao;
758 }
759 }
760
761