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

com.formkiq.server.service.dto.ArchiveDTO Maven / Gradle / Ivy

/*
 * Copyright (C) 2016 FormKiQ Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.formkiq.server.service.dto;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.StringUtils;

import com.formkiq.server.service.InvalidRequestBodyException;

/**
 * Holder of Archive file format data.
 *
 */
public class ArchiveDTO {

    /** Jackson Object Mapper. */
    private Jackson2ObjectMapperBuilder jsonMapper;

    /** Workflow Data. */
    private String workflowData;

    /** Workflow object. */
    private Workflow workflow;

    /** Map of Form UUID / Form Object. */
    private Map forms = new HashMap();

    /** Map of Form UUID / Raw String. */
    private Map formRaw = new HashMap();

    /** Map of Form Record UUID / Form Record Object. */
    private Map formrecords = new HashMap();

    /** Map of Form Record UUID / Raw String. */
    private Map formRecordsRaw = new HashMap();

    /**
     * constructor.
     * @param mapper {@link Jackson2ObjectMapperBuilder}
     */
    public ArchiveDTO(final Jackson2ObjectMapperBuilder mapper) {
        this.jsonMapper = mapper;
    }

    /**
     * Add Form to Archive.
     * @param data {@link String}
     * @throws IOException IOException
     */
    public void addForm(final String data) throws IOException {
        Form form = this.jsonMapper.build().readValue(data, Form.class);

        this.forms.put(form.getUUID(), form);
        this.formRaw.put(form.getUUID(), data);
    }

    /**
     * Add Form Record to Archive.
     * @param data {@link String}
     * @throws IOException IOException
     */
    public void addFormrecord(final String data) throws IOException {
        Form form = this.jsonMapper.build().readValue(data, Form.class);

        this.formrecords.put(form.getUUID(), form);
        this.formRecordsRaw.put(form.getUUID(), data);
    }

    /**
     * Add Workflow to Archive.
     * @param data {@link String}
     * @throws IOException IOException
     */
    public void addWorkflow(final String data) throws IOException {
        this.workflowData = data;
        this.workflow = this.jsonMapper.build().readValue(this.workflowData,
                Workflow.class);
    }

    /**
     * Returns Form in Archive.
     * @return {@link Form}
     * @throws InvalidRequestBodyException thrown if number of form != 1
     */
    public Form getForm() throws InvalidRequestBodyException {

        if (this.forms.size() == 1) {
            return this.forms.values().iterator().next();
        }

        throw new InvalidRequestBodyException();
    }

    /**
     * Returns Form Raw JSON.
     * @return {@link String}
     */
    public String getFormJSON() {
        if (isFormJSON()) {
            return this.formRaw.values().iterator().next();
        }

        throw new InvalidRequestBodyException();
    }

    /**
     * Returns Form Record in Archive.
     * @return {@link Form}
     */
    public Form getFormRecord() {
        if (this.formrecords.size() == 1) {
            return this.formrecords.values().iterator().next();
        }

        throw new InvalidRequestBodyException();
    }
    /**
     * Returns Form Record Raw JSON.
     * @return {@link String}
     */
    public String getFormrecordJSON() {
        if (this.formRecordsRaw.size() == 1) {
            return this.formRecordsRaw.values().iterator().next();
        }

        throw new InvalidRequestBodyException();
    }

    /**
     * Returns Workflow.
     * @return {@link Workflow}
     */
    public Workflow getWorkflow() {

        if (this.workflow != null) {
            return this.workflow;
        }

        throw new InvalidRequestBodyException();
    }

    /**
     * Returns Workflow JSON.
     * @return {@link String}
     */
    public String getWorkflowJSON() {

        if (!StringUtils.isEmpty(this.workflowData)) {
            return this.workflowData;
        }

        throw new InvalidRequestBodyException();
    }

    /**
     * Is Form JSON.
     * @return boolean
     */
    public boolean isFormJSON() {
        return this.forms.size() == 1;
    }

    /**
     * Whether archive is valid.
     * @return boolean
     */
    public boolean isValid() {
        return this.forms.isEmpty();
    }

    /**
     * @return {@link UUID}
     */
    public UUID getUUID() {
        return isFormJSON() ? UUID.fromString(getForm().getUUID())
                : UUID.fromString(getWorkflow().getUUID());
    }

    /**
     * @return {@link String}
     */
    public String getName() {
        return isFormJSON() ? getForm().getName() : getWorkflow().getName();
    }

    /**
     * @return {@link UUID}
     */
    public UUID getParentUUID() {
        String uuid = isFormJSON() ? getForm().getParentUUID()
                : getWorkflow().getParentUUID();
        return !StringUtils.isEmpty(uuid) ? UUID.fromString(uuid) : null;
    }

    /**
     * @return {@link String}
     */
    public String getData() {
        return isFormJSON() ? getFormJSON() : getWorkflowJSON();
    }

    /**
     * @return {@link Date}
     */
    public Date getInsertedDate() {
        return isFormJSON() ? getForm().getInsertedDate()
                : getWorkflow().getInsertedDate();
    }

    /**
     * @return {@link Date}
     */
    public Date getUpdatedDate() {
        return isFormJSON() ? getForm().getUpdatedDate()
                : getWorkflow().getUpdatedDate();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy