|
| 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 | +} |
0 commit comments