use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class XMLGenerateDatesStep method createStepNodeValue.
@Override
protected void createStepNodeValue(Document doc, Element stepNode) throws EngineException {
boolean bInvalid = false;
try {
if (startDefinition.isEmpty() || stopDefinition.isEmpty() || daysDefinition.isEmpty()) {
bInvalid = true;
Engine.logBeans.warn("Skipping GenerateDates step \"" + getName() + "\" : definitions are empty");
} else {
NodeList startList = getStartSource().getContextValues();
NodeList stopList = getStopSource().getContextValues();
NodeList dayList = getDaysSource().getContextValues();
String start = getNodeValue(startList.item(0));
String stop = getNodeValue(stopList.item(0));
String[] days = getNodeValue(dayList.item(0)).split(",");
List<Integer> v = new ArrayList<Integer>(days.length);
for (int i = 0; i < days.length; i++) {
int day = Integer.parseInt(days[i], 10);
if (!calendarCompatibility) {
day = day + 1;
day = ((day == 8) ? 1 : day);
}
if ((1 <= day) && (day <= 7)) {
v.add(Integer.valueOf(day));
}
}
/* Fix: #1085 - Treats input days as non ordered */
Collections.sort(v);
if (v.get(0).equals(1)) {
v.remove(0);
v.add(1);
}
/* End fix */
DateFormat dfInput = new SimpleDateFormat(inputFormat, DateUtils.getLocale(inputLocale));
dfInput.setTimeZone(TimeZone.getDefault());
Date dateStart = dfInput.parse(start);
Date dateStop = dfInput.parse(stop);
Calendar c1 = Calendar.getInstance(TimeZone.getDefault());
c1.setTime(dateStart);
Calendar c2 = Calendar.getInstance(TimeZone.getDefault());
c2.setTime(dateStop);
if (c2.before(c1))
Engine.logBeans.warn("Skipping GenerateDates step \"" + getName() + "\" : end date lower than start date");
c2.add(Calendar.DATE, 1);
DateFormat dfOutput = new SimpleDateFormat(outputFormat, DateUtils.getLocale(outputLocale));
dfOutput.setTimeZone(TimeZone.getDefault());
if (v.isEmpty()) {
bInvalid = true;
} else {
Integer first = (Integer) v.get(0);
Integer last = (Integer) v.get(v.size() - 1);
while (c1.before(c2)) {
int i = c1.get(Calendar.DAY_OF_WEEK);
Integer day = Integer.valueOf(i);
if (v.contains(day)) {
Element element = doc.createElement("date");
if (split) {
Element dow = (Element) element.appendChild(doc.createElement("dayOfWeek"));
int idow = c1.get(Calendar.DAY_OF_WEEK);
idow = (calendarCompatibility ? idow : idow - 1);
idow = (idow == 0) ? 7 : idow;
dow.appendChild(doc.createTextNode(String.valueOf(idow)));
Element dom = (Element) element.appendChild(doc.createElement("day"));
dom.appendChild(doc.createTextNode(String.valueOf(c1.get(Calendar.DATE))));
Element month = (Element) element.appendChild(doc.createElement("month"));
int imonth = c1.get(Calendar.MONTH);
imonth = (calendarCompatibility ? imonth : imonth + 1);
String smonth = String.valueOf(imonth);
smonth = (!calendarCompatibility && (smonth.length() == 1)) ? "0" + smonth : smonth;
month.appendChild(doc.createTextNode(smonth));
Element year = (Element) element.appendChild(doc.createElement("year"));
year.appendChild(doc.createTextNode(String.valueOf(c1.get(Calendar.YEAR))));
stepNode.appendChild(element);
} else {
Date date = c1.getTime();
String dateValue = dfOutput.format(date);
element.appendChild(doc.createTextNode(dateValue));
stepNode.appendChild(element);
}
int amount = 1;
int size = v.size();
if (size == 1) {
amount = 7;
} else {
if (day.equals(last)) {
amount = Math.abs(last.intValue() - first.intValue());
} else {
Integer next = (Integer) v.get(v.indexOf(day) + 1);
amount = next.intValue() - day.intValue();
if (amount < 0)
amount = 7 + amount;
}
}
c1.add(Calendar.DATE, amount);
} else {
c1.add(Calendar.DATE, 1);
}
}
}
}
} catch (Exception e) {
bInvalid = true;
Engine.logBeans.error("Skipping GenerateDates step \"" + getName() + "\"", e);
} finally {
if (bInvalid) {
String nodeValue = getGenerateValue();
stepNode.appendChild(doc.createTextNode(nodeValue));
}
}
}
use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class SmtpStep method stepExecute.
@Override
protected boolean stepExecute(Context javascriptContext, Scriptable scope) throws EngineException {
if (isEnabled()) {
if (super.stepExecute(javascriptContext, scope)) {
try {
evaluate(javascriptContext, scope, this.smtpSubject, "smtpSubject", false);
sSubject = evaluated instanceof Undefined ? "" : evaluated.toString();
evaluate(javascriptContext, scope, this.smtpRecipients, "smtpRecipients", false);
sRecipients = evaluated instanceof Undefined ? "" : evaluated.toString();
evaluate(javascriptContext, scope, this.smtpSender, "smtpSender", false);
sSender = evaluated instanceof Undefined ? "" : evaluated.toString();
evaluate(javascriptContext, scope, this.xslFilepath, "xslFilepath", false);
String xslFilepath = evaluated instanceof Undefined ? "" : evaluated.toString();
evaluate(javascriptContext, scope, contentType, "contentType", false);
sContentType = evaluated instanceof Undefined ? "" : evaluated.toString();
File fileXSL = null;
if (xslFilepath.length() > 0) {
fileXSL = new File(Engine.theApp.filePropertyManager.getFilepathFromProperty(xslFilepath, getProject().getName()));
if (!fileXSL.exists()) {
throw new EngineException("The defined xslFilepath doesn't exist : " + fileXSL.getAbsolutePath());
}
}
if (sContentType.length() == 0) {
sContentType = fileXSL != null ? "text/html; charset=UTF-8" : "text/plain; charset=UTF-8";
}
Transformer transformer = fileXSL != null ? XMLUtils.getNewTransformer(new StreamSource(fileXSL)) : XMLUtils.getNewTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter sw = new StringWriter();
// make a clean copy, without step_id and copy_step attributes
Document workingDoc = XMLUtils.getDefaultDocumentBuilder().newDocument();
Element root = (Element) workingDoc.appendChild(workingDoc.createElement("root"));
XMLCopyStep.createCopy(this, workingDoc, root);
NodeList list = root.getChildNodes();
if (list != null && list.getLength() > 0) {
int nbNodes = list.getLength();
for (int i = 0; i < nbNodes; i++) {
Node currentNode = list.item(i);
switch(currentNode.getNodeType()) {
case Node.ATTRIBUTE_NODE:
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
case Node.COMMENT_NODE:
if (fileXSL == null) {
sw.write(currentNode.getNodeValue());
}
break;
case Node.ELEMENT_NODE:
transformer.transform(new DOMSource(currentNode), new StreamResult(sw));
break;
default:
transformer.transform(new DOMSource(currentNode), new StreamResult(sw));
}
}
} else {
transformer.transform(new DOMSource(sequence.context.outputDocument), new StreamResult(sw));
}
sMessageText = sw.toString();
bodyParts.clear();
for (XMLVector<String> attachment : attachments) {
String filepath = attachment.get(0);
try {
evaluate(javascriptContext, scope, filepath, "filepath", false);
filepath = evaluated instanceof Undefined ? "" : evaluated.toString();
if (filepath.length() > 0) {
File file = new File(Engine.theApp.filePropertyManager.getFilepathFromProperty(filepath, getProject().getName()));
if (file.exists()) {
if (file.isFile()) {
String filename = attachment.get(1);
evaluate(javascriptContext, scope, filename, "filename", false);
filename = evaluated instanceof Undefined ? file.getName() : evaluated.toString();
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.attachFile(file);
bodyPart.setFileName(filename);
bodyParts.add(bodyPart);
} else {
Engine.logBeans.info("Unable attach a directory : " + file.getAbsolutePath());
}
} else {
Engine.logBeans.info("Unable attach an unexisting file : " + file.getAbsolutePath());
}
} else {
Engine.logBeans.info("Unable attach an empty filepath !!");
}
} catch (Exception e) {
Engine.logBeans.info("Unable attach " + filepath, e);
}
}
sendMess();
return true;
} catch (EngineException e) {
throw e;
} catch (Exception e) {
throw new EngineException(e.getClass().getSimpleName() + " during the mail creation", e);
}
}
}
return false;
}
use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class WriteFileStep method configure.
@Override
public void configure(Element element) throws Exception {
super.configure(element);
String version = element.getAttribute("version");
if (version == null) {
String s = XMLUtils.prettyPrintDOM(element);
EngineException ee = new EngineException("Unable to find version number for the database object \"" + getName() + "\".\n" + "XML data: " + s);
throw ee;
}
if (VersionUtils.compareMigrationVersion(version, ".m003") < 0) {
if (!dataFile.equals(""))
dataFile = "'" + dataFile + "'";
hasChanged = true;
Engine.logBeans.warn("[WriteFileStep] The object \"" + getName() + "\" has been updated to .m003 version");
}
}
use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class WriteFileStep method stepExecute.
@Override
protected boolean stepExecute(Context javascriptContext, Scriptable scope) throws EngineException {
if (isEnabled()) {
filePath = null;
try {
StepSource stepSource = getSource();
if (!stepSource.inError()) {
filePath = evaluateDataFileName(javascriptContext, scope);
// Retrieve a view based on context nodes (prevent to modify context nodes, output usefull nodes only)
OutputFilter outputFilter = sequence.new OutputFilter(writeOutputFalse ? OutputOption.UsefullOnly : OutputOption.VisibleOnly);
NodeList nl = Sequence.ouputDomView(stepSource.getContextOutputNodes(), outputFilter);
writeFile(filePath, nl);
filePath = getAbsoluteFilePath(filePath);
}
} catch (Exception e) {
setErrorStatus(true);
Engine.logBeans.error("An error occured while writing to file", e);
}
return super.stepExecute(javascriptContext, scope);
}
return false;
}
use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class WriteJSONStep method writeFile.
protected void writeFile(String filePath, NodeList nodeList) throws EngineException {
if (nodeList == null) {
throw new EngineException("Unable to write to json file: element is Null");
}
File file = new File(getAbsoluteFilePath(filePath));
synchronized (Engine.theApp.filePropertyManager.getMutex(file)) {
try {
String enc = getEncoding();
StringBuilder sb = new StringBuilder();
boolean isArray = isAppendResult() || nodeList.getLength() > 1;
for (int i = 0; i < nodeList.getLength(); ) {
Node node = nodeList.item(i);
if (node instanceof Element) {
try {
sb.append(XMLUtils.XmlToJson((Element) node, true, true, null));
} catch (JSONException e) {
sb.append(node.getNodeValue());
}
} else {
sb.append(node.getNodeValue());
}
if (++i < nodeList.getLength()) {
sb.append(",\n");
}
}
boolean writeAll = true;
if (isAppendResult()) {
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) {
FileChannel fc = randomAccessFile.getChannel();
byte[] bChar = new byte[1];
String str = null;
boolean empty = true;
if (file.exists() && file.length() >= 2) {
fc.read(ByteBuffer.wrap(bChar), 0);
str = new String(bChar, StandardCharsets.US_ASCII);
empty = !"[".equals(str);
if (!empty) {
fc.read(ByteBuffer.wrap(bChar), 1);
str = new String(bChar, StandardCharsets.US_ASCII);
empty = "]".equals(str);
}
}
if (!empty) {
long min = Math.max(0, fc.size() - 10);
long i;
for (i = fc.size() - 1; i > min; i--) {
fc.read(ByteBuffer.wrap(bChar), i);
str = new String(bChar, StandardCharsets.US_ASCII);
if (!str.isBlank()) {
break;
}
}
if ("]".equals(str)) {
sb.insert(0, ",\n");
sb.append("]");
fc.write(ByteBuffer.wrap(sb.toString().getBytes(enc)), i);
writeAll = false;
}
}
}
}
if (writeAll) {
if (isArray) {
sb.insert(0, "[").append("]");
}
FileUtils.write(file, sb.toString(), enc);
}
} catch (IOException e) {
throw new EngineException("Unable to write to json file", e);
} finally {
Engine.theApp.filePropertyManager.releaseMutex(file);
}
}
}
Aggregations