001: /**
002: *
003: */package com.bostechcorp.cbesb.common.util.generators;
004:
005: import java.util.Iterator;
006: import java.util.List;
007: import org.jdom.Element;
008: import org.jdom.Namespace;
009:
010: import com.bostechcorp.cbesb.common.sa.service.AutoRetryScheduleRecord;
011: import com.bostechcorp.cbesb.common.sa.service.StandardScheduleRecord;
012:
013: /**
014: * @author LPS
015: *
016: */
017:
018: public class ComponentScheduleXMLGenerator extends AbstractDOMGenerator {
019: private int namespacePrefixIndex = 0;
020:
021: private String namespacePrefixtext = "nspf";
022:
023: private static String COMPONENT_SCHEDULE_T = "componentSchedule";
024:
025: private static String STANDARD_SCHEDULE_T = "standardSchedule";
026:
027: private static String SECONDS_T = "seconds";
028:
029: private static String MINUTES_T = "minutes";
030:
031: private static String HOURS_T = "hours";
032:
033: private static String DAY_OF_MONTH_T = "dayOfMonth";
034:
035: private static String MONTH_T = "month";
036:
037: private static String DAY_OF_WEEK_T = "dayOfWeek";
038:
039: private static String HOLIDAY_SCHEDULE_T = "holidaySchedule";
040:
041: private static String AUTO_RETRY_SCHEDULE_T = "autoRetrySchedule";
042:
043: private static String START_TIME_T = "startTime";
044:
045: private static String END_TIME_T = "endTime";
046:
047: private static String RETRY_INTERVAL_T = "retryInterval";
048:
049: private static String FAILURE_NOTIFICATION_EP_T = "failureNotificationEP";
050:
051: private static String SERVICE_NAME_T = "serviceName";
052:
053: private static String ENDPOINT_NAME_T = "endpointName";
054:
055: private static String SUCCESS_NOTIFICATION_EP_T = "successNotificationEP";
056:
057: private static String URI_CONSTANT_STRING = "http://bostechcorp.com/SU/";
058:
059: //
060: private String saName = "";
061:
062: private String suName = "";
063:
064: private String fileNameEnding = "schedule.xml";
065:
066: public ComponentScheduleXMLGenerator(String path, String saName,
067: String suName) {
068: super (path);
069: this .saName = saName;
070: this .suName = suName;
071: }
072:
073: /* (non-Javadoc)
074: * @see com.bostechcorp.cbesb.common.util.AbstractDOMGenerator#buildDocumentStructure()
075: */
076: @Override
077: protected void buildDocumentStructure() {
078: rootElement = jdomFactory.element(COMPONENT_SCHEDULE_T);
079: rootElement
080: .setNamespace(Namespace
081: .getNamespace("http://cbesb.bostechcorp.com/scheduler/1.0"));
082: }
083:
084: public void setSchedulues(
085: List<StandardScheduleRecord> standardSchedules,
086: List<AutoRetryScheduleRecord> autoRetrySchedules) {
087: setStandardSchedules(standardSchedules);
088: setAutoRetrySchedules(autoRetrySchedules);
089: }
090:
091: private void setAutoRetrySchedules(
092: List<AutoRetryScheduleRecord> autoRetrySchedules) {
093: for (Object object : autoRetrySchedules) {
094: if (object instanceof AutoRetryScheduleRecord) {
095: AutoRetryScheduleRecord autoRetrySchedule = (AutoRetryScheduleRecord) object;
096: //adding node to the XML schema
097: Element autoRetryElement = jdomFactory
098: .element(AUTO_RETRY_SCHEDULE_T);
099: rootElement.addContent(autoRetryElement);
100: autoRetryElement.addContent(jdomFactory.element(
101: START_TIME_T).addContent(
102: autoRetrySchedule.getStartTime()));
103: autoRetryElement.addContent(jdomFactory.element(
104: END_TIME_T).addContent(
105: autoRetrySchedule.getEndTime()));
106: autoRetryElement.addContent(jdomFactory.element(
107: RETRY_INTERVAL_T).addContent(
108: autoRetrySchedule.getRetryInterval()));
109: autoRetryElement.addContent(jdomFactory.element(
110: DAY_OF_MONTH_T).addContent(
111: autoRetrySchedule.getDayOfMonth()));
112: autoRetryElement.addContent(jdomFactory
113: .element(MONTH_T).addContent(
114: autoRetrySchedule.getMonth()));
115: autoRetryElement.addContent(jdomFactory.element(
116: DAY_OF_WEEK_T).addContent(
117: autoRetrySchedule.getDayOfWeek()));
118:
119: if (!autoRetrySchedule.getHoliday().contains("None")) {
120: autoRetryElement.addContent(jdomFactory.element(
121: HOLIDAY_SCHEDULE_T).addContent(
122: autoRetrySchedule.getHoliday()));
123: }
124: if (autoRetrySchedule
125: .getSuccessNotificationServiceName() != null
126: && autoRetrySchedule
127: .getSuccessNotificationServiceName()
128: .indexOf("none") < 0) {
129: Namespace ns = getNamespaceDeclaration(autoRetrySchedule
130: .getSuccessNotificationSuName());
131: Element messagElement = jdomFactory
132: .element(SUCCESS_NOTIFICATION_EP_T);
133:
134: messagElement
135: .addContent(jdomFactory
136: .element(SERVICE_NAME_T)
137: .addContent(
138: ns.getPrefix()
139: + ":"
140: + autoRetrySchedule
141: .getSuccessNotificationServiceName()));
142: messagElement
143: .addContent(jdomFactory
144: .element(ENDPOINT_NAME_T)
145: .addContent(
146: autoRetrySchedule
147: .getSuccessNotificationEndpointName()));
148: //also setting the Namespace
149:
150: if (rootElement.getNamespace(ns.getPrefix()) == null) {
151: rootElement.addNamespaceDeclaration(ns);
152: }
153:
154: autoRetryElement.addContent(messagElement);
155:
156: }
157: if (autoRetrySchedule
158: .getFailureNotificationServiceName() != null
159: && autoRetrySchedule
160: .getFailureNotificationServiceName()
161: .indexOf("none") < 0) {
162: Namespace ns = getNamespaceDeclaration(autoRetrySchedule
163: .getFailureNotificationSuName());
164:
165: Element messagElement = jdomFactory
166: .element(FAILURE_NOTIFICATION_EP_T);
167:
168: messagElement
169: .addContent(jdomFactory
170: .element(SERVICE_NAME_T)
171: .addContent(
172: ns.getPrefix()
173: + ":"
174: + autoRetrySchedule
175: .getFailureNotificationServiceName()));
176: messagElement
177: .addContent(jdomFactory
178: .element(ENDPOINT_NAME_T)
179: .addContent(
180: autoRetrySchedule
181: .getFailureNotificationEndpointName()));
182: //also setting the Namespace
183:
184: if (rootElement.getNamespace(ns.getPrefix()) == null) {
185: rootElement.addNamespaceDeclaration(ns);
186: }
187: autoRetryElement.addContent(messagElement);
188:
189: }
190: }
191: }
192: }
193:
194: private Namespace getNamespaceDeclaration(String suName) {
195: String prefixString = "";
196: // test wether the namespace exist
197: List l = rootElement.getAdditionalNamespaces();
198: for (Iterator nsIter = l.iterator(); nsIter.hasNext();) {
199: Object nsObject = nsIter.next();
200: if (nsObject instanceof Namespace) {
201: Namespace nsElement = (Namespace) nsObject;
202: if (nsElement.getURI().equals(
203: URI_CONSTANT_STRING + suName)) {
204: return nsElement;
205: }
206: }
207: }
208: prefixString = namespacePrefixtext + namespacePrefixIndex;
209: namespacePrefixIndex++;
210: String uriString = URI_CONSTANT_STRING + suName;
211: return Namespace.getNamespace(prefixString, uriString);
212: }
213:
214: private void setStandardSchedules(
215: List<StandardScheduleRecord> standardSchedules) {
216: for (Object object : standardSchedules) {
217: if (object instanceof StandardScheduleRecord) {
218: StandardScheduleRecord autoRetrySchedule = (StandardScheduleRecord) object;
219: // adding node to the XML schema
220: Element standardScheduleElement = jdomFactory
221: .element(STANDARD_SCHEDULE_T);
222: rootElement.addContent(standardScheduleElement);
223: standardScheduleElement.addContent(jdomFactory.element(
224: SECONDS_T).addContent(
225: autoRetrySchedule.getSeconds()));
226: standardScheduleElement.addContent(jdomFactory.element(
227: MINUTES_T).addContent(
228: autoRetrySchedule.getMinutes()));
229: standardScheduleElement.addContent(jdomFactory.element(
230: HOURS_T).addContent(
231: autoRetrySchedule.getHours()));
232: standardScheduleElement.addContent(jdomFactory.element(
233: DAY_OF_MONTH_T).addContent(
234: autoRetrySchedule.getDayOfMonth()));
235: standardScheduleElement.addContent(jdomFactory.element(
236: MONTH_T).addContent(
237: autoRetrySchedule.getMonth()));
238: standardScheduleElement.addContent(jdomFactory.element(
239: DAY_OF_WEEK_T).addContent(
240: autoRetrySchedule.getDayOfWeek()));
241: if (!autoRetrySchedule.getHoliday().contains("None")) {
242: standardScheduleElement.addContent(jdomFactory
243: .element(HOLIDAY_SCHEDULE_T).addContent(
244: autoRetrySchedule.getHoliday()));
245: }
246: }
247: }
248: }
249:
250: /* (non-Javadoc)
251: * @see com.bostechcorp.cbesb.common.util.AbstractDOMGenerator#getFileOut()
252: */
253: @Override
254: protected String getFileOut() {
255: if (this .filePath.endsWith("/")) {
256: return filePath + saName + "_" + suName + "_"
257: + fileNameEnding;
258: } else {
259: return filePath + "/" + saName + "_" + suName + "_"
260: + fileNameEnding;
261: }
262: }
263:
264: }
|