com.formkiq.server.service.dto.ArchiveDTO Maven / Gradle / Ivy
package com.formkiq.server.service.dto;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.StringUtils;
import com.formkiq.server.domain.Form;
import com.formkiq.server.domain.Workflow;
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 (this.forms.size() == 1) {
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();
}
/**
* Whether archive is valid.
* @return boolean
*/
public boolean isValid() {
return this.forms.isEmpty();
}
}