Skip to content

Commit 05b8938

Browse files
author
Joerg Huber
committed
Initial submission.
1 parent 6aded60 commit 05b8938

3 files changed

Lines changed: 371 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* JobEventData.java
3+
* Created: 11 Sep 2018
4+
*
5+
* Copyright 2018 Systemic Pty Ltd
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8+
* in compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed under the License
13+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14+
* or implied. See the License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package sif3.infra.rest.provider.functional;
19+
20+
import java.io.Serializable;
21+
22+
import sif3.infra.common.model.JobType;
23+
24+
/**
25+
* This is a helper POJO to manage the publishing of Job Events.
26+
*
27+
* @author Joerg Huber
28+
*/
29+
public class JobEventData implements Serializable
30+
{
31+
private static final long serialVersionUID = -1008848372047662458L;
32+
33+
private long internalJobID = -1; // needed for updating job event table
34+
private boolean consumerEvent = true; // required to filter jobs for audit and non audit zones
35+
private JobType jobData = null; // actual job SIF Data Object.
36+
37+
public long getInternalJobID()
38+
{
39+
return internalJobID;
40+
}
41+
42+
public void setInternalJobID(long internalJobID)
43+
{
44+
this.internalJobID = internalJobID;
45+
}
46+
47+
public boolean isConsumerEvent()
48+
{
49+
return consumerEvent;
50+
}
51+
52+
public void setConsumerEvent(boolean consumerEvent)
53+
{
54+
this.consumerEvent = consumerEvent;
55+
}
56+
57+
public JobType getJobData()
58+
{
59+
return jobData;
60+
}
61+
62+
public void setJobData(JobType jobData)
63+
{
64+
this.jobData = jobData;
65+
}
66+
67+
@Override
68+
public String toString()
69+
{
70+
return "JobEventData [internalJobID=" + internalJobID + ", consumerEvent=" + consumerEvent
71+
+ ", jobData=" + jobData + "]";
72+
}
73+
}
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/*
2+
* JobEventIterator.java
3+
* Created: 4 Sep 2018
4+
*
5+
* Copyright 2018 Systemic Pty Ltd
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8+
* in compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed under the License
13+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14+
* or implied. See the License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package sif3.infra.rest.provider.functional;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import au.com.systemic.framework.utils.AdvancedProperties;
27+
import sif3.common.CommonConstants.AdapterType;
28+
import sif3.common.header.HeaderValues.EventAction;
29+
import sif3.common.interfaces.SIFEventIterator;
30+
import sif3.common.model.SIFContext;
31+
import sif3.common.model.SIFEvent;
32+
import sif3.common.model.SIFZone;
33+
import sif3.common.model.ZoneContextInfo;
34+
import sif3.common.persist.model.SIF3JobEvent;
35+
import sif3.common.persist.service.JobService;
36+
import sif3.infra.common.conversion.InfraUnmarshalFactory;
37+
import sif3.infra.common.model.JobCollectionType;
38+
import sif3.infra.common.model.JobType;
39+
40+
/**
41+
* This method is the implementation for the event iterator for functional service jobs. Because the framework
42+
* maintains events internally and the event object is known for functional services this can fully be implemented
43+
* by this framework and doesn't have to be done by individual provider classes as with object services.
44+
*
45+
* @author Joerg Huber
46+
*/
47+
public class JobEventIterator implements SIFEventIterator<JobCollectionType>
48+
{
49+
protected final Logger logger = LoggerFactory.getLogger(getClass());
50+
private InfraUnmarshalFactory infraUnmarshaller = new InfraUnmarshalFactory();
51+
52+
private int currentPos = 0;
53+
private List<SIF3JobEvent> jobEvents = new ArrayList<SIF3JobEvent>();
54+
55+
public JobEventIterator(String serviceName, boolean includeConsumeRequested, AdapterType adapterType, AdvancedProperties serviceProperties)
56+
{
57+
currentPos = 0;
58+
try
59+
{
60+
JobService service = new JobService();
61+
62+
jobEvents = service.retrieveJobEvents(serviceName, adapterType, includeConsumeRequested);
63+
}
64+
catch (Exception ex)
65+
{
66+
logger.error("Failed to retrieve Job Events for Functional Service "+serviceName+": "+ex.getMessage(), ex);
67+
jobEvents = new ArrayList<SIF3JobEvent>();
68+
}
69+
}
70+
71+
/*
72+
* We do not implement this method! For Jobs we need to do some more sophistication and since the publishing
73+
* of Job Events is under full control of the BaseEventProvider we can implement another method that is
74+
* specifically geared towards the way Job Event publishing may work. The core issue is that Job events are
75+
* published to the 'fingerprint' consumer only but also to some generic auditZones. Later the published jobs
76+
* need to be marked as published. So a job event may need to be published to a few zones and a different set of
77+
* events may need to be published to each zone. The 'fingerprint' belongs to a specific zone and indicates
78+
* the owner of the job. Most likely the consumerCreated events do not need to be published to that zone
79+
* where as provider created events do. In Audit Zones all events need to be published. To avoid too many
80+
* events to be published to the 'fingerprint' zone we need a little bit more info in the getNextEvent()
81+
* method than just a list of SIF Job Objects.
82+
*
83+
* (non-Javadoc)
84+
* @see sif3.common.interfaces.SIFEventIterator#getNextEvents(int)
85+
*/
86+
@Override
87+
public SIFEvent<JobCollectionType> getNextEvents(int maxListSize)
88+
{
89+
return null;
90+
}
91+
92+
/**
93+
* This method returns the next set of events. The max size of the list is given by the maxListSize parameter.
94+
* Note the returned list holds events of the same event type and the same zone, context and fingerprint.
95+
* The BaseFunctionalServiceProvider must use this method to get the next set of events rather than the
96+
* getNextEvents(int maxListSize) method above. If null is returned by this method then it must be assumed
97+
* that there are no more events available.
98+
*
99+
* @param maxListSize
100+
*
101+
* @return See desc.
102+
*/
103+
public JobEventWrapper getEvents(int maxListSize)
104+
{
105+
JobEventWrapper events = null;
106+
if (hasNext())
107+
{
108+
events = new JobEventWrapper();
109+
events.setEvents(new ArrayList<JobEventData>());
110+
while ((events.getEvents().size() < maxListSize) && hasNext())
111+
{
112+
SIF3JobEvent jobEvent = jobEvents.get(currentPos);
113+
currentPos++;
114+
115+
try
116+
{
117+
if (events.getEvents().size() == 0) // first record => initialise wrapper
118+
{
119+
events.setEventAction(mapEventType(jobEvent.getEventType()));
120+
events.setFingerprint(jobEvent.getFingerprint());
121+
events.setZoneContext(new ZoneContextInfo(new SIFZone(jobEvent.getZoneID()), new SIFContext(jobEvent.getContextID())));
122+
123+
addEventToWrapper(events, jobEvent);
124+
}
125+
else
126+
{
127+
EventAction eventAction = mapEventType(jobEvent.getEventType());
128+
if ((events.getEventAction() == eventAction) &&
129+
events.getFingerprint().equals(jobEvent.getFingerprint()) &&
130+
events.getZoneContext().getZone().getId().equals(jobEvent.getZoneID()) &&
131+
events.getZoneContext().getContext().getId().equals(jobEvent.getContextID()))
132+
{
133+
addEventToWrapper(events, jobEvent);
134+
}
135+
else // We are done for this set. Roll back counter as this event isn't processed yet
136+
{
137+
currentPos--;
138+
break; // and stop....
139+
}
140+
}
141+
}
142+
catch (Exception ex) // most likely failed to unmarshal event. Should not really happen!
143+
{
144+
logger.error("Failed to extract Jobe Event Data for Job Event ID = "+jobEvent.getInternalID()+": "+ex.getMessage()+". Ignore this event.", ex);
145+
}
146+
}
147+
}
148+
149+
return events;
150+
}
151+
152+
/* (non-Javadoc)
153+
* @see sif3.common.interfaces.SIFEventIterator#hasNext()
154+
*/
155+
@Override
156+
public boolean hasNext()
157+
{
158+
return (currentPos < jobEvents.size());
159+
}
160+
161+
/* (non-Javadoc)
162+
* @see sif3.common.interfaces.SIFEventIterator#releaseResources()
163+
*/
164+
@Override
165+
public void releaseResources()
166+
{
167+
jobEvents = new ArrayList<SIF3JobEvent>();
168+
currentPos = 0;
169+
}
170+
171+
/*---------------------*/
172+
/*-- Private Methods --*/
173+
/*---------------------*/
174+
private EventAction mapEventType(String jobDBEventType)
175+
{
176+
if ("U".equalsIgnoreCase(jobDBEventType))
177+
{
178+
return EventAction.UPDATE;
179+
}
180+
if ("C".equalsIgnoreCase(jobDBEventType))
181+
{
182+
return EventAction.CREATE;
183+
}
184+
if ("D".equalsIgnoreCase(jobDBEventType))
185+
{
186+
return EventAction.DELETE;
187+
}
188+
189+
return null;
190+
}
191+
192+
private void addEventToWrapper(JobEventWrapper events, SIF3JobEvent jobEvent) throws Exception
193+
{
194+
JobType job = null;
195+
JobEventData event = new JobEventData();
196+
event.setConsumerEvent(jobEvent.isConsumerRequested());
197+
event.setInternalJobID(jobEvent.getInternalID());
198+
199+
// If it is a delete event then we simply create a empty JobType with only the refID populated.
200+
if (events.getEventAction() == EventAction.DELETE)
201+
{
202+
job = new JobType();
203+
job.setId(jobEvent.getJobID());
204+
}
205+
else
206+
{
207+
job = (JobType)infraUnmarshaller.unmarshalFromXML(jobEvent.getJobXML(), JobType.class);
208+
}
209+
event.setJobData(job);
210+
211+
events.getEvents().add(event);
212+
}
213+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* JobEventWrapper.java
3+
* Created: 11 Sep 2018
4+
*
5+
* Copyright 2018 Systemic Pty Ltd
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8+
* in compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed under the License
13+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14+
* or implied. See the License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package sif3.infra.rest.provider.functional;
19+
20+
import java.io.Serializable;
21+
import java.util.ArrayList;
22+
23+
import sif3.common.header.HeaderValues.EventAction;
24+
import sif3.common.model.ZoneContextInfo;
25+
26+
/**
27+
* This is a helper POJO to manage the publishing of Job Events.
28+
*
29+
* @author Joerg Huber
30+
*/
31+
public class JobEventWrapper implements Serializable
32+
{
33+
private static final long serialVersionUID = -5053804097493083125L;
34+
35+
private EventAction eventAction;
36+
private String fingerprint;
37+
private ZoneContextInfo zoneContext;
38+
private ArrayList<JobEventData> events;
39+
40+
public EventAction getEventAction()
41+
{
42+
return eventAction;
43+
}
44+
45+
public void setEventAction(EventAction eventAction)
46+
{
47+
this.eventAction = eventAction;
48+
}
49+
50+
public String getFingerprint()
51+
{
52+
return fingerprint;
53+
}
54+
55+
public void setFingerprint(String fingerprint)
56+
{
57+
this.fingerprint = fingerprint;
58+
}
59+
60+
public ZoneContextInfo getZoneContext()
61+
{
62+
return zoneContext;
63+
}
64+
65+
public void setZoneContext(ZoneContextInfo zoneContext)
66+
{
67+
this.zoneContext = zoneContext;
68+
}
69+
70+
public ArrayList<JobEventData> getEvents()
71+
{
72+
return events;
73+
}
74+
public void setEvents(ArrayList<JobEventData> events)
75+
{
76+
this.events = events;
77+
}
78+
79+
@Override
80+
public String toString()
81+
{
82+
return "JobEventWrapper [eventAction=" + eventAction + ", fingerprint=" + fingerprint
83+
+ ", zoneContext=" + zoneContext + ", events=" + events + "]";
84+
}
85+
}

0 commit comments

Comments
 (0)