All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.windowsazure.messaging.NotificationHubJob Maven / Gradle / Ivy

//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------

package com.windowsazure.messaging;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.digester3.Digester;
import org.xml.sax.SAXException;

public class NotificationHubJob {
    private static final String XML_HEADER = "";
    private static final String XML_FOOTER = "";

    private String jobId;
    private double progress;
    private NotificationHubJobType jobType;
    private NotificationHubJobStatus jobStatus;
    private String outputContainerUri;
    private String importFileUri;
    private String failure;
    private Map outputProperties;
    private Date createdAt;
    private Date updatedAt;

    private static final ThreadLocal singleEntryParser;
    private static final ThreadLocal collectionParser;

    static {
        singleEntryParser = ThreadLocal.withInitial(() -> {
            Digester instance = new Digester();
            setupSingleEntryParser(instance);
            return instance;
        });

        collectionParser = ThreadLocal.withInitial(() -> {
            Digester instance = new Digester();
            setupCollectionParser(instance);
            return instance;
        });
    }

    public String getJobId() {
        return jobId;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }

    public double getProgress() {
        return progress;
    }

    public void setProgressFromString(String progress) {
        this.progress = Double.parseDouble(progress);
    }

    public NotificationHubJobType getJobType() {
        return jobType;
    }

    public void setJobType(NotificationHubJobType jobType) {
        this.jobType = jobType;
    }

    public void setJobTypeFromString(String jobType) {
        this.jobType = Enum.valueOf(NotificationHubJobType.class, jobType);
    }

    public NotificationHubJobStatus getJobStatus() {
        return jobStatus;
    }

    public void setJobStatusFromString(String jobStatus) {
        this.jobStatus = Enum.valueOf(NotificationHubJobStatus.class, jobStatus);
    }

    public String getOutputContainerUri() {
        return outputContainerUri;
    }

    public void setOutputContainerUri(String outputContainerUri) {
        this.outputContainerUri = outputContainerUri;
    }

    public String getImportFileUri() {
        return importFileUri;
    }

    public void setImportFileUri(String importFileUri) {
        this.importFileUri = importFileUri;
    }

    public String getFailure() {
        return failure;
    }

    public void setFailure(String failure) {
        this.failure = failure;
    }

    public Map getOutputProperties() {
        return outputProperties;
    }

    public void setOutputProperties(Map outputProperties) {
        this.outputProperties = outputProperties;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAtFromString(String createdAt) {
        this.createdAt = javax.xml.bind.DatatypeConverter.parseDateTime(createdAt).getTime();
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAtFromString(String updatedAt) {
        this.updatedAt = javax.xml.bind.DatatypeConverter.parseDateTime(updatedAt).getTime();
    }

    public static NotificationHubJob parseOne(InputStream content) throws IOException, SAXException {
        return singleEntryParser.get().parse(content);
    }

    public static List parseCollection(InputStream content) throws IOException, SAXException {
        return collectionParser.get().parse(content);
    }

    public String getXml() {
        StringBuilder buf = new StringBuilder();
        buf.append(XML_HEADER);
        if (this.jobType != null) buf.append("").append(this.jobType.name()).append("");
        if (this.outputContainerUri != null)
            buf.append("").append(this.outputContainerUri).append("");
        if (this.importFileUri != null) buf.append("").append(this.importFileUri).append("");
        buf.append(XML_FOOTER);
        return buf.toString();
    }

    private static void setupCollectionParser(Digester digester) {
        digester.addObjectCreate("*/feed", LinkedList.class);
        setupSingleEntryParser(digester);
        digester.addSetNext("*/entry", "add", NotificationHubJob.class.getName());
    }

    private static void setupSingleEntryParser(Digester digester) {
        digester.addObjectCreate("*/entry", NotificationHubJob.class);
        digester.addCallMethod("*/JobId", "setJobId", 1);
        digester.addCallParam("*/JobId", 0);
        digester.addCallMethod("*/Progress", "setProgressFromString", 1);
        digester.addCallParam("*/Progress", 0);
        digester.addCallMethod("*/Type", "setJobTypeFromString", 1);
        digester.addCallParam("*/Type", 0);
        digester.addCallMethod("*/Status", "setJobStatusFromString", 1);
        digester.addCallParam("*/Status", 0);
        digester.addCallMethod("*/OutputContainerUri", "setOutputContainerUri", 1);
        digester.addCallParam("*/OutputContainerUri", 0);
        digester.addCallMethod("*/ImportFileUri", "setImportFileUri", 1);
        digester.addCallParam("*/ImportFileUri", 0);
        digester.addCallMethod("*/Failure", "setFailure", 1);
        digester.addCallParam("*/Failure", 0);
        digester.addCallMethod("*/CreatedAt", "setCreatedAtFromString", 1);
        digester.addCallParam("*/CreatedAt", 0);
        digester.addCallMethod("*/UpdatedAt", "setUpdatedAtFromString", 1);
        digester.addCallParam("*/UpdatedAt", 0);
        digester.addObjectCreate("*/OutputProperties", HashMap.class);
        digester.addCallMethod("*/d3p1:KeyValueOfstringstring", "put", 2);
        digester.addCallParam("*/d3p1:Key", 0);
        digester.addCallParam("*/d3p1:Value", 1);
        digester.addSetNext("*/OutputProperties", "setOutputProperties", Map.class.getName());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy