Search in sources :

Example 16 with Marshaller

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;
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) StringWriter(java.io.StringWriter)

Example 17 with Marshaller

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>";
    }
}
Also used : FormLayout(org.akaza.openclinica.domain.datamap.FormLayout) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) Marshaller(org.exolab.castor.xml.Marshaller) XMLContext(org.exolab.castor.xml.XMLContext) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) Calendar(java.util.Calendar) Mapping(org.exolab.castor.mapping.Mapping) Manifest(org.akaza.openclinica.web.pform.manifest.Manifest) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) StringWriter(java.io.StringWriter) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 18 with Marshaller

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();
}
Also used : MostActivatedTerminatedAlarms(alma.alarmsystem.statistics.generated.MostActivatedTerminatedAlarms) Marshaller(org.exolab.castor.xml.Marshaller) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) Record(alma.alarmsystem.statistics.generated.Record) MostTerminatedAlarms(alma.alarmsystem.statistics.generated.MostTerminatedAlarms) MostActivatedAlarms(alma.alarmsystem.statistics.generated.MostActivatedAlarms)

Example 19 with Marshaller

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);
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) MarshalException(org.exolab.castor.xml.MarshalException) ValidationException(org.exolab.castor.xml.ValidationException) IOException(java.io.IOException) LaserObjectNotFoundException(cern.laser.business.LaserObjectNotFoundException) PatternSyntaxException(java.util.regex.PatternSyntaxException) ValidationException(org.exolab.castor.xml.ValidationException) MarshalException(org.exolab.castor.xml.MarshalException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CDBRecordDoesNotExistEx(alma.cdbErrType.CDBRecordDoesNotExistEx) StringWriter(java.io.StringWriter) FaultFamily(alma.acs.alarmsystem.generated.FaultFamily) Vector(java.util.Vector)

Example 20 with Marshaller

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);
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) MarshalException(org.exolab.castor.xml.MarshalException) ValidationException(org.exolab.castor.xml.ValidationException) StringWriter(java.io.StringWriter) FaultFamily(alma.acs.alarmsystem.generated.FaultFamily) IOException(java.io.IOException) Vector(java.util.Vector) LaserObjectNotFoundException(cern.laser.business.LaserObjectNotFoundException) PatternSyntaxException(java.util.regex.PatternSyntaxException) ValidationException(org.exolab.castor.xml.ValidationException) MarshalException(org.exolab.castor.xml.MarshalException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

Marshaller (org.exolab.castor.xml.Marshaller)23 StringWriter (java.io.StringWriter)13 IOException (java.io.IOException)10 MarshalException (org.exolab.castor.xml.MarshalException)7 ValidationException (org.exolab.castor.xml.ValidationException)7 Mapping (org.exolab.castor.mapping.Mapping)5 XMLContext (org.exolab.castor.xml.XMLContext)5 LaserObjectNotFoundException (cern.laser.business.LaserObjectNotFoundException)4 File (java.io.File)4 FileWriter (java.io.FileWriter)3 MalformedURLException (java.net.MalformedURLException)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 FaultFamily (alma.acs.alarmsystem.generated.FaultFamily)2 CDBRecordDoesNotExistEx (alma.cdbErrType.CDBRecordDoesNotExistEx)2 BufferedWriter (java.io.BufferedWriter)2 FileNotFoundException (java.io.FileNotFoundException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 Vector (java.util.Vector)2