Skip to content

Commit dc7225e

Browse files
author
Joerg Huber
committed
Added consumerRequested flag to DAO layer and services.
1 parent 57c7d46 commit dc7225e

4 files changed

Lines changed: 185 additions & 14 deletions

File tree

SIF3InfraREST/sif3Common/src/main/java/sif3/common/persist/dao/JobDAO.java

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import sif3.common.persist.common.BasicTransaction;
4242
import sif3.common.persist.model.SIF3Job;
4343
import sif3.common.persist.model.SIF3JobEvent;
44+
import sif3.common.persist.model.SIF3JobEvent.JobEventType;
4445

4546
/**
4647
* @author Joerg Huber
@@ -605,6 +606,7 @@ public List<SIF3JobEvent> getJobEventsByUUID(BasicTransaction tx, String uuid, A
605606
*
606607
* A few values will be defaulted if they are not set.<br/>
607608
* - toFingerPrintOnly defaults to TRUE: Only the consumer with that fingerprint will receive the event.
609+
* - consumerRequested defaults to TRUE: Event was caused by consumer requested operation on job.
608610
* - fullUpdate defaults to TRUE: it is assumed that the entire Job Object is provided in case of an update event.<br/><br/>
609611
*
610612
* The following values will be ignored and defaulted. The returned object will have them populated as followed:<br/>
@@ -712,14 +714,15 @@ else if ("U".equalsIgnoreCase(jobEvent.getEventType()))
712714
* @param job The job based on which an event shall be created. If null then no event is created and null is returned.
713715
* @param eventType The event type (CREATE, UPDATE, DELETE).
714716
* @param fingerprintOnly Indicating if the event is for the 'fingerprint' consumer only.
717+
* @param consumerRequested Indicating if the event is caused by consumer requested operation on job.
715718
*
716719
* @return The newly created job event. This has now the internalID and the event date and published populated.
717720
* The published will be set to FALSE and the event date will be set to current datetime.
718721
*
719722
* @throws IllegalArgumentException If the tx is null or any of the rules listed above are not met.
720723
* @throws PersistenceException Could not access underlying data store.
721724
*/
722-
public SIF3JobEvent createJobEvent(BasicTransaction tx, SIF3Job job, EventAction eventType, boolean fingerprintOnly) throws IllegalArgumentException, PersistenceException
725+
public SIF3JobEvent createJobEvent(BasicTransaction tx, SIF3Job job, EventAction eventType, boolean fingerprintOnly, boolean consumerRequested) throws IllegalArgumentException, PersistenceException
723726
{
724727
if (tx == null)
725728
{
@@ -728,7 +731,7 @@ public SIF3JobEvent createJobEvent(BasicTransaction tx, SIF3Job job, EventAction
728731

729732
if (job != null)
730733
{
731-
return createJobEvent(tx, new SIF3JobEvent(job, eventType, fingerprintOnly));
734+
return createJobEvent(tx, new SIF3JobEvent(job, eventType, fingerprintOnly, consumerRequested));
732735
}
733736
else
734737
{
@@ -875,22 +878,91 @@ public List<SIF3JobEvent> retrieveJobEventsSince(BasicTransaction tx, Date chang
875878
criteria.addOrder(Order.asc("eventDate"));
876879

877880
@SuppressWarnings("unchecked")
878-
List<SIF3JobEvent> jobEventss = criteria.list();
881+
List<SIF3JobEvent> jobEvents = criteria.list();
879882

880883
// Just in case test for null....
881-
if (jobEventss == null)
884+
if (jobEvents == null)
882885
{
883-
jobEventss = new ArrayList<SIF3JobEvent>();
886+
jobEvents = new ArrayList<SIF3JobEvent>();
884887
}
885888

886-
return jobEventss;
889+
return jobEvents;
887890
}
888891
catch (HibernateException e)
889892
{
890893
throw new PersistenceException("Unable to retrieve Job Events since "+DateUtils.dateToString(changesSince, DateUtils.DISP_DATE_TIME_SEC)+" for fingerprint = '"+ fingerprint + ", zoneID = " + zoneID + ", contextID = "+ contextID + ", adapterType = '" + adapterType + "' and pagingInfo = "+pagingInfo+".", e);
891894
}
892-
893895
}
894896

895-
897+
/*
898+
*
899+
*/
900+
public List<SIF3JobEvent> retrieveJobEvents(BasicTransaction tx,
901+
Date eventsBefore,
902+
String serviceName,
903+
AdapterType adapterType,
904+
JobEventType eventType,
905+
boolean includeConsumeRequested) throws IllegalArgumentException, PersistenceException
906+
{
907+
if (tx == null)
908+
{
909+
throw new IllegalArgumentException("Current transaction is null.");
910+
}
911+
if (adapterType == null)
912+
{
913+
throw new IllegalArgumentException("adapterType is null.");
914+
}
915+
if (StringUtils.isEmpty(serviceName))
916+
{
917+
throw new IllegalArgumentException("serviceName is null or empty.");
918+
}
919+
if (eventsBefore == null)
920+
{
921+
throw new IllegalArgumentException("eventsBefore is null.");
922+
}
923+
if (eventType == null)
924+
{
925+
throw new IllegalArgumentException("eventType is null.");
926+
}
927+
928+
try
929+
{
930+
Criteria criteria = tx.getSession().createCriteria(SIF3JobEvent.class);
931+
932+
criteria = criteria.add(Restrictions.le("eventDate", eventsBefore));
933+
criteria = criteria.add(Restrictions.eq("adapterType", adapterType.name()));
934+
criteria = criteria.add(Restrictions.eq("serviceName", serviceName));
935+
criteria = criteria.add(Restrictions.eq("eventType", eventType.name()));
936+
937+
if (!includeConsumeRequested) // only get provider initiated events
938+
{
939+
criteria = criteria.add(Restrictions.eq("consumerRequested", false));
940+
}
941+
942+
criteria = criteria.add(Restrictions.eq("published", false));
943+
944+
945+
//Add orderBy to ensure consistent results
946+
criteria.addOrder(Order.asc("zoneID")).addOrder(Order.asc("contextID")).addOrder(Order.asc("fingerprint")).addOrder(Order.asc("internalID"));
947+
948+
@SuppressWarnings("unchecked")
949+
List<SIF3JobEvent> jobEvents = criteria.list();
950+
951+
// Just in case test for null....
952+
if (jobEvents == null)
953+
{
954+
jobEvents = new ArrayList<SIF3JobEvent>();
955+
}
956+
957+
return jobEvents;
958+
}
959+
catch (HibernateException e)
960+
{
961+
throw new PersistenceException("Unable to retrieve Job Events for events before "+
962+
DateUtils.dateToString(eventsBefore, DateUtils.DISP_DATE_TIME_SEC)+
963+
" for adapterType = '" + adapterType +
964+
"', serviceName = '" + serviceName +
965+
"', eventType = '" + eventType + "'.", e);
966+
}
967+
}
896968
}

SIF3InfraREST/sif3Common/src/main/java/sif3/common/persist/model/SIF3Infra.hbm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
<property name="eventType" column="EVENT_TYPE" type="string" length="1"/>
185185
<property name="fullUpdate" column="FULL_UPDATE" type="boolean"/>
186186
<property name="toFingerPrintOnly" column="TO_FINGERPRINT_ONLY" type="boolean"/>
187+
<property name="consumerRequested" column="CONSUMER_REQUESTED" type="boolean"/>
187188
<property name="published" column="EVENT_PUBLISHED" type="boolean"/>
188189
<property name="publishedDate" column="PUBLISHED_DATETIME" type="timestamp"/>
189190
<property name="jobXML" column="JOB_XML" type="text"/>

SIF3InfraREST/sif3Common/src/main/java/sif3/common/persist/model/SIF3JobEvent.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static enum JobEventType {U, C, D};
3636
private String eventType = null; // U=Update, C=Create, D=Delete
3737
private boolean fullUpdate = Boolean.TRUE; // TRUE = Update event is full update; FALSE = Update is partial
3838
private boolean toFingerPrintOnly = Boolean.TRUE; // TRUE = Include fingerprint in event; FALSE = May not include fingerprint
39+
private boolean consumerRequested = Boolean.TRUE; // TRUE = consumer requested an operation on the job
3940
private boolean published = Boolean.FALSE; // TRUE = Event is already published; FALSE = Event isn't published, yet.
4041
private Date publishedDate = null; // Date when event has been sent/published to event end-point.
4142

@@ -44,7 +45,16 @@ public SIF3JobEvent()
4445
super();
4546
}
4647

47-
public SIF3JobEvent(SIF3Job job, EventAction eventType, boolean fingerprintOnly)
48+
/**
49+
* Creates a Job Event object for the given event type. It will set the fingerprint only and consumerRequested
50+
* flags as given.
51+
*
52+
* @param job Create an event with the data of the job.
53+
* @param eventType The event type for this job event.
54+
* @param fingerprintOnly TRUE then job event will be sent to fingerprint only (not yet used)
55+
* @param consumerRequested Event was due to a consumer requested action on the job object.
56+
*/
57+
public SIF3JobEvent(SIF3Job job, EventAction eventType, boolean fingerprintOnly, boolean consumerRequested)
4858
{
4959
if (job != null)
5060
{
@@ -57,6 +67,7 @@ public SIF3JobEvent(SIF3Job job, EventAction eventType, boolean fingerprintOnly)
5767
setJobXML(job.getJobXML());
5868
setFingerprint(job.getFingerprint());
5969
setToFingerPrintOnly(fingerprintOnly);
70+
setConsumerRequested(consumerRequested);
6071
setEventDate(new Date());
6172
setEventType(eventType.name().substring(0, 1));
6273
setFullUpdate(true);
@@ -109,6 +120,16 @@ public void setToFingerPrintOnly(boolean toFingerPrintOnly)
109120
this.toFingerPrintOnly = toFingerPrintOnly;
110121
}
111122

123+
public boolean isConsumerRequested()
124+
{
125+
return consumerRequested;
126+
}
127+
128+
public void setConsumerRequested(boolean consumerRequested)
129+
{
130+
this.consumerRequested = consumerRequested;
131+
}
132+
112133
public boolean isPublished()
113134
{
114135
return published;
@@ -133,8 +154,8 @@ public void setPublishedDate(Date publishedDate)
133154
public String toString()
134155
{
135156
return "SIF3JobEvent [eventDate=" + eventDate + ", eventType=" + eventType + ", fullUpdate="
136-
+ fullUpdate + ", toFingerPrintOnly=" + toFingerPrintOnly + ", published="
137-
+ published + ", publishedDate=" + publishedDate + ", toString()="
138-
+ super.toString() + "]";
157+
+ fullUpdate + ", toFingerPrintOnly=" + toFingerPrintOnly + ", consumerRequested="
158+
+ consumerRequested + ", published=" + published + ", publishedDate="
159+
+ publishedDate + ", toString()=" + super.toString() + "]";
139160
}
140161
}

SIF3InfraREST/sif3Common/src/main/java/sif3/common/persist/service/JobService.java

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package sif3.common.persist.service;
1919

20+
import java.util.ArrayList;
2021
import java.util.Date;
2122
import java.util.List;
2223

@@ -31,6 +32,7 @@
3132
import sif3.common.persist.model.JobTemplate;
3233
import sif3.common.persist.model.SIF3Job;
3334
import sif3.common.persist.model.SIF3JobEvent;
35+
import sif3.common.persist.model.SIF3JobEvent.JobEventType;
3436

3537
/**
3638
* @author Joerg Huber
@@ -442,22 +444,23 @@ public SIF3JobEvent createJobEvent(SIF3JobEvent jobEvent) throws IllegalArgument
442444
* @param job The job based on which an event shall be created. If null then no event is created and null is returned.
443445
* @param eventType The event type (CREATE, UPDATE, DELETE).
444446
* @param fingerprintOnly Indicating if the event is for the 'fingerprint' consumer only.
447+
* @param consumerRequested Indicating if the event is caused by consumer requested operation on job.
445448
*
446449
* @return The newly created job event. This has now the internalID and the event date and published populated.
447450
* The published will be set to FALSE and the event date will be set to current datetime.
448451
*
449452
* @throws IllegalArgumentException If the tx is null or any of the rules listed above are not met.
450453
* @throws PersistenceException Could not access underlying data store.
451454
*/
452-
public SIF3JobEvent createJobEvent(SIF3Job job, EventAction eventType, boolean fingerprintOnly) throws IllegalArgumentException, PersistenceException
455+
public SIF3JobEvent createJobEvent(SIF3Job job, EventAction eventType, boolean fingerprintOnly, boolean consumerRequested) throws IllegalArgumentException, PersistenceException
453456
{
454457
SIF3JobEvent row = null;
455458
BasicTransaction tx = null;
456459

457460
try
458461
{
459462
tx = startTransaction();
460-
row = jobDAO.createJobEvent(tx, job, eventType, fingerprintOnly);
463+
row = jobDAO.createJobEvent(tx, job, eventType, fingerprintOnly, consumerRequested);
461464
tx.commit();
462465
}
463466
catch (Exception ex)
@@ -561,4 +564,78 @@ public List<SIF3JobEvent> retrieveJobEventsSince(Date changesSince, String servi
561564

562565
return jobEvents;
563566
}
567+
568+
/**
569+
* This method returns all events in the SIF3_JOB_EVENT table that match the given parameters. If the 'includeConsumeRequested'
570+
* is set to TRUE then all events initiated by the provider and consumer are returned. If 'includeConsumeRequested' is set
571+
* to false only events caused by changes from the provider are returned. This behaviour is useful as in most cases the
572+
* consumer, who requested an operation on a job, already has the response of the request and therefore has no need to receive
573+
* an event about that action. The consumer only wants to know about events it has not initiated, meaning provider caused events.
574+
*
575+
* @param eventsBefore Return events before this date.
576+
* @param serviceName Events for the service to be returned.
577+
* @param adapterType The adapter type which is ENVIRONMENT_PROVIDER for a direct environment or PROVIDER for a brokered
578+
* environment.
579+
* @param eventType The event type (Create, Update, Change) to be retrieved.
580+
* @param includeConsumeRequested Include consumer caused events.
581+
*
582+
* @return A list of events that match the criteria given by the parameters. The returned list is ordered by ZoneId, ContextId,
583+
* fingerprint & eventID (in this order).
584+
*
585+
* @throws IllegalArgumentException Any of the input parameters is null.
586+
* @throws PersistenceException Failed to retrieve events due to a DB issue.
587+
*/
588+
public List<SIF3JobEvent> retrieveJobEvents(Date eventsBefore,
589+
String serviceName,
590+
AdapterType adapterType,
591+
JobEventType eventType,
592+
boolean includeConsumeRequested) throws IllegalArgumentException, PersistenceException
593+
{
594+
List<SIF3JobEvent> jobEvents = null;
595+
BasicTransaction tx = null;
596+
try
597+
{
598+
tx = startTransaction();
599+
jobEvents = jobDAO.retrieveJobEvents(tx, eventsBefore, serviceName, adapterType, eventType, includeConsumeRequested);
600+
tx.commit();
601+
}
602+
catch (Exception ex)
603+
{
604+
rollback(tx);
605+
exceptionMapper(ex, ex.getMessage(), false, false);
606+
}
607+
608+
return jobEvents;
609+
}
610+
611+
/**
612+
* This is a convenience method that is used to get a full list of events in the order of Create, Update and Delete before the
613+
* current date and time. The final returned list has all Create events before the Update Events and then the Delete events.
614+
* Within each event type the order is by ZoneId, ContextId, fingerprint & eventID. This list is intended to be used by the
615+
* event publisher as it has the events already lined up as they should be published.
616+
*
617+
* @param serviceName Events for the service to be returned.
618+
* @param adapterType The adapter type which is ENVIRONMENT_PROVIDER for a direct environment or PROVIDER for a brokered
619+
* environment.
620+
* @param includeConsumeRequested Include consumer caused events.
621+
*
622+
* @return A list of events that match the criteria given by the parameters. Ordering of the returned list is outlined in the
623+
* description.
624+
*
625+
* @throws IllegalArgumentException Any of the input parameters is null.
626+
* @throws PersistenceException Failed to retrieve events due to a DB issue.
627+
*/
628+
public List<SIF3JobEvent> retrieveJobEvents(String serviceName, AdapterType adapterType, boolean includeConsumeRequested) throws IllegalArgumentException, PersistenceException
629+
{
630+
List<SIF3JobEvent> finalJobEvents = new ArrayList<SIF3JobEvent>();
631+
Date now = new Date();
632+
633+
// Get all Create Events
634+
finalJobEvents.addAll(retrieveJobEvents(now, serviceName, adapterType, JobEventType.C, includeConsumeRequested));
635+
finalJobEvents.addAll(retrieveJobEvents(now, serviceName, adapterType, JobEventType.U, includeConsumeRequested));
636+
finalJobEvents.addAll(retrieveJobEvents(now, serviceName, adapterType, JobEventType.D, includeConsumeRequested));
637+
638+
return finalJobEvents;
639+
}
640+
564641
}

0 commit comments

Comments
 (0)