use of org.exolab.castor.xml.Marshaller in project OpenClinica by OpenClinica.
the class OpenRosaXmlGenerator method buildStringXForm.
private String buildStringXForm(Html html) throws Exception {
StringWriter writer = new StringWriter();
Marshaller marshaller = xmlContext.createMarshaller();
marshaller.setNamespaceMapping("h", "http://www.w3.org/1999/xhtml");
marshaller.setNamespaceMapping("jr", "http://openrosa.org/javarosa");
marshaller.setNamespaceMapping("xsd", "http://www.w3.org/2001/XMLSchema");
marshaller.setNamespaceMapping("ev", "http://www.w3.org/2001/xml-events");
marshaller.setNamespaceMapping("", "http://www.w3.org/2002/xforms");
marshaller.setProperty("org.exolab.castor.indent", "false");
marshaller.setWriter(writer);
marshaller.marshal(html);
String xform = writer.toString();
return xform;
}
use of org.exolab.castor.xml.Marshaller in project OpenClinica by OpenClinica.
the class OpenRosaServices method getManifest.
/**
* @api {get} /rest2/openrosa/:studyOID/manifest Get Form Manifest
* @apiName getManifest
* @apiPermission admin
* @apiVersion 3.8.0
* @apiParam {String} studyOID Study Oid.
* @apiGroup Form
* @apiDescription Gets additional information on a particular Form, including links to associated media.
*/
@GET
@Path("/{studyOID}/manifest")
@Produces(MediaType.TEXT_XML)
public String getManifest(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("studyOID") String studyOID, @QueryParam("formId") String uniqueId, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
if (!mayProceedPreview(studyOID))
return null;
String formLayoutOid = getFormLayoutOid(uniqueId);
FormLayout formLayout = formLayoutDao.findByOcOID(formLayoutOid);
Manifest manifest = new Manifest();
List<FormLayoutMedia> mediaList = formLayoutMediaDao.findByFormLayoutIdForNoteTypeMedia(formLayout.getFormLayoutId());
String urlBase = getCoreResources().getDataInfo().getProperty("sysURL").split("/MainMenu")[0];
if (mediaList != null && mediaList.size() > 0) {
for (FormLayoutMedia media : mediaList) {
MediaFile mediaFile = new MediaFile();
mediaFile.setFilename(media.getName());
File image = new File(Utils.getCrfMediaSysPath() + media.getPath() + media.getName());
mediaFile.setHash(DigestUtils.md5Hex(media.getName()) + Double.toString(image.length()));
mediaFile.setDownloadUrl(urlBase + "/rest2/openrosa/" + studyOID + "/downloadMedia?formLayoutMediaId=" + media.getFormLayoutMediaId());
manifest.add(mediaFile);
}
}
// Add user list
MediaFile userList = new MediaFile();
LinkedHashMap<String, Object> subjectContextCache = (LinkedHashMap<String, Object>) context.getAttribute("subjectContextCache");
if (subjectContextCache != null) {
String userXml = getUserXml(context);
userList.setHash((DigestUtils.md5Hex(userXml)));
}
userList.setFilename("users.xml");
userList.setDownloadUrl(urlBase + "/rest2/openrosa/" + studyOID + "/downloadUsers");
manifest.add(userList);
try {
// Create the XML manifest using a Castor mapping file.
XMLContext xmlContext = new XMLContext();
Mapping mapping = xmlContext.createMapping();
mapping.loadMapping(getCoreResources().getURL("openRosaManifestMapping.xml"));
xmlContext.addMapping(mapping);
Marshaller marshaller = xmlContext.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.setWriter(writer);
marshaller.marshal(manifest);
// Set response headers
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentDate = new Date();
cal.setTime(currentDate);
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss zz");
format.setCalendar(cal);
response.setHeader("Content-Type", "text/xml; charset=UTF-8");
response.setHeader("Date", format.format(currentDate));
response.setHeader("X-OpenRosa-Version", "1.0");
return writer.toString();
} catch (Exception e) {
LOGGER.error(e.getMessage());
LOGGER.error(ExceptionUtils.getStackTrace(e));
return "<Error>" + e.getMessage() + "</Error>";
}
}
use of org.exolab.castor.xml.Marshaller in project ACS by ACS-Community.
the class StatHashMap method calculate.
/**
* Calculate the statistics of the passed time interval.
* <P>
* The figures calculated are:
* <UL>
* <LI>Total number of different alarms issued
* <LI>Total number of activations and terminations
* <LI>Total number of terminations
* <LI>Total number of activations
* <LI>Average number of activations and terminations per second
* <LI>The 5 alarms that has been changed (activated and/or terminated) more often
* <LI>The 5 alarms that has been activated more often
* <LI>The 5 alarms that has been terminated more often
* </UL>
*
* @param statStruct The object with numbers for the statistics
* @throws IOException In case of error with the file
* @throws ValidationException In case of error validating data
* @throws MarshalException IN case of error "marshalling" data on file
*/
private void calculate(StatStruct statStruct) throws IOException, MarshalException, ValidationException {
// A collection to iterate over the values
List<AlarmInfo> infos = new ArrayList<AlarmInfo>(statStruct.alarmsInfo.values());
// The number of different alarms published in the interval
int totAlarms = infos.size();
// Total number of operations (i.e. activations and terminations)
int totOperations = statStruct.numActiavations + statStruct.numTerminations;
// Average number of operations per second
float avgOpPerSecond = (float) totOperations / (float) (timeInterval * 60);
// Get the file to write the statistics
BufferedWriter outF;
try {
outF = openOutputFile();
} catch (Throwable t) {
logger.log(AcsLogLevel.ERROR, "Can't write on file: statistics lost", t);
return;
}
// Build the string to write on disk (help of castor)
Record statRec = new Record();
statRec.setTimestamp(IsoDateFormat.formatCurrentDate());
statRec.setProcessedAlarmTypes(totAlarms);
statRec.setActivations(statStruct.numActiavations);
statRec.setTerminations(statStruct.numTerminations);
statRec.setTotalAlarms(totOperations);
statRec.setAvgAlarmsPerSecond(Float.valueOf(String.format("%.2f", avgOpPerSecond)));
MostActivatedAlarms maa = new MostActivatedAlarms();
Collections.sort(infos, new ComparatorByType(AlarmInfo.VALUE_TYPE.ACTIVATIONS));
maa.setID(appendListOfAlarms(infos, AlarmInfo.VALUE_TYPE.ACTIVATIONS, 4));
statRec.setMostActivatedAlarms(maa);
MostTerminatedAlarms mta = new MostTerminatedAlarms();
Collections.sort(infos, new ComparatorByType(AlarmInfo.VALUE_TYPE.TERMINATIONS));
mta.setID(appendListOfAlarms(infos, AlarmInfo.VALUE_TYPE.TERMINATIONS, 4));
statRec.setMostTerminatedAlarms(mta);
MostActivatedTerminatedAlarms mata = new MostActivatedTerminatedAlarms();
Collections.sort(infos, new ComparatorByType(AlarmInfo.VALUE_TYPE.OPERATIONS));
mata.setID(appendListOfAlarms(infos, AlarmInfo.VALUE_TYPE.OPERATIONS, 4));
statRec.setMostActivatedTerminatedAlarms(mata);
Marshaller marshaller = getMarshaller(outF);
marshaller.marshal(statRec);
// This string appears at the end of the XML file
outF.write(closeXMLTag);
statStruct.alarmsInfo.clear();
infos.clear();
outF.flush();
outF.close();
}
use of org.exolab.castor.xml.Marshaller in project ACS by ACS-Community.
the class ACSAlarmDAOImpl method updateFaultFamily.
public void updateFaultFamily(FaultFamily ff) {
if (conf == null || !conf.isWriteable())
throw new IllegalStateException("no writable configuration accessor");
if (ff == null)
throw new IllegalArgumentException("Null FaultFamily argument");
StringWriter FFWriter = new StringWriter();
Marshaller FF_marshaller;
try {
FF_marshaller = new Marshaller(FFWriter);
} catch (IOException e) {
e.printStackTrace();
return;
}
FF_marshaller.setValidation(false);
try {
FF_marshaller.marshal(ff);
} catch (MarshalException e) {
e.printStackTrace();
return;
} catch (ValidationException e) {
e.printStackTrace();
return;
}
String path = ALARM_DEFINITION_PATH + "/" + ff.getName();
try {
try {
conf.getConfiguration(path);
conf.deleteConfiguration(path);
} catch (CDBRecordDoesNotExistEx e) {
//Record does not exist, so we skip removing it.
throw new IllegalStateException("FaultFamily doesn't exist");
}
conf.addConfiguration(path, FFWriter.toString().replaceFirst("xsi:type=\".*\"", ""));
} catch (IllegalStateException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
return;
}
Vector<FaultFamily> ffs = new Vector<FaultFamily>();
ffs.add(ff);
removeAlarmsMap(ffs);
generateAlarmsMap(ffs);
}
use of org.exolab.castor.xml.Marshaller in project ACS by ACS-Community.
the class ACSAlarmDAOImpl method addFaultFamily.
public void addFaultFamily(FaultFamily ff) {
if (conf == null || !conf.isWriteable())
throw new IllegalStateException("no writable configuration accessor");
if (ff == null)
throw new IllegalArgumentException("Null FaultFamily argument");
StringWriter FFWriter = new StringWriter();
Marshaller FF_marshaller;
try {
FF_marshaller = new Marshaller(FFWriter);
} catch (IOException e) {
e.printStackTrace();
return;
}
FF_marshaller.setValidation(false);
try {
FF_marshaller.marshal(ff);
} catch (MarshalException e) {
e.printStackTrace();
return;
} catch (ValidationException e) {
e.printStackTrace();
return;
}
String path = ALARM_DEFINITION_PATH + "/" + ff.getName();
try {
conf.addConfiguration(path, FFWriter.toString().replaceFirst("xsi:type=\".*\"", ""));
} catch (Exception e) {
throw new IllegalStateException("FaultFamily already exists");
}
Vector<FaultFamily> ffs = new Vector<FaultFamily>();
ffs.add(ff);
generateAlarmsMap(ffs);
}
Aggregations