com.formkiq.server.service.FormServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
package com.formkiq.server.service;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.formkiq.server.dao.FormDao;
import com.formkiq.server.dao.UserDao;
import com.formkiq.server.dao.WorkflowDao;
import com.formkiq.server.domain.Asset;
import com.formkiq.server.domain.AssetHolder;
import com.formkiq.server.domain.ClientForm;
import com.formkiq.server.domain.ClientWorkflow;
import com.formkiq.server.domain.Form;
import com.formkiq.server.domain.FormLog;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.Workflow;
import com.formkiq.server.domain.WorkflowLog;
import com.formkiq.server.domain.type.FormDTO;
import com.formkiq.server.domain.type.FormListDTO;
import com.formkiq.server.domain.type.SyncListDTO;
import com.formkiq.server.domain.type.UserPermission;
import com.formkiq.server.domain.type.UserRole;
import com.formkiq.server.domain.type.WorkflowListDTO;
import com.formkiq.server.service.dto.ArchiveDTO;
/**
* FormService implementation.
*
*/
@Service
public class FormServiceImpl implements FormService {
/** OAuthService. */
@Autowired
private OAuthService oauthservice;
/** FormDao. */
@Autowired
private FormDao formDao;
/** JSON Object Mapper. */
@Autowired
private Jackson2ObjectMapperBuilder jsonMapper;
/** UserDao. */
@Autowired
private UserDao userDao;
/** WorkflowDao. */
@Autowired
private WorkflowDao workflowDao;
/**
* Extracts data from a byte[] zip file.
* @param bytes bytes[]
* @return {@link ArchiveDTO}
*/
private ArchiveDTO extractJSONFromZipFile(final byte[] bytes) {
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ZipInputStream zipStream = new ZipInputStream(is);
ArchiveDTO archive = new ArchiveDTO(this.jsonMapper);
try {
ZipEntry entry = null;
while ((entry = zipStream.getNextEntry()) != null) {
String filename = entry.getName();
String data = IOUtils.toString(zipStream);
if (filename.endsWith(".form")) {
archive.addForm(data);
} else if (filename.endsWith(".jsonrecord")) {
archive.addForm(data);
} else if (filename.endsWith(".workflow")) {
archive.addWorkflow(data);
} else if (filename.endsWith(".workflowrecord")) {
archive.addWorkflow(data);
}
}
} catch (IOException e) {
throw new InvalidRequestBodyException();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(zipStream);
}
return archive;
}
@Override
public FormDTO findForm(final UserDetails ud, final String clientId,
final String formId) {
FormDTO dto = null;
User user = (User) ud;
ClientForm form = this.formDao.findForm(user, clientId, formId);
if (form != null) {
dto = new FormDTO();
byte[] data = this.formDao.findAssetData(form.getAssetid());
dto.setData(data);
dto.setSha1hash(form.getSha1hash());
dto.setPermission(getUserPermission(user));
}
if (dto == null) {
throw new FormNotFoundException(formId);
}
return dto;
}
@Override
public FormListDTO findForms(final String client) {
return this.formDao.findForms(client);
}
@Override
public FormListDTO findForms(final String client,
final String form) {
return this.formDao.findForms(client, form);
}
@Override
public FormDTO findWorkflow(final UserDetails ud, final String client,
final String workflow) {
FormDTO dto = null;
User user = (User) ud;
ClientWorkflow wf = this.workflowDao.findWorkflow(user, client,
workflow);
if (wf != null) {
dto = new FormDTO();
byte[] data = this.formDao.findAssetData(wf.getAssetid());
dto.setData(data);
dto.setSha1hash(wf.getSha1hash());
dto.setPermission(getUserPermission(user));
}
if (dto == null) {
throw new FormNotFoundException(workflow);
}
return dto;
}
@Override
public WorkflowListDTO findWorkflows(final String client) {
return this.workflowDao.findWorkflows(client);
}
@Override
public WorkflowListDTO findWorkflows(final String client,
final String workflow) {
return this.workflowDao.findWorkflows(client, workflow);
}
@Override
public SyncListDTO getSyncList(final UserDetails ud, final String client,
final int nextToken) {
SyncListDTO dto = this.formDao.findFormSyncList(client, nextToken);
return dto;
}
@Override
public SyncListDTO getSyncWorkflowList(final UserDetails user,
final String client, final int nextToken) {
SyncListDTO dto = this.workflowDao.findWorkflowSyncList(client,
nextToken);
return dto;
}
/**
* @param user User
* @return UserPermission
*/
private UserPermission getUserPermission(final User user) {
return UserRole.ROLE_ADMIN.equals(user.getRole())
? UserPermission.ROLE_WRITE : UserPermission.ROLE_READ;
}
@Override
public FormLog saveForm(final UserDetails ud, final String client,
final byte[] bytes) {
User user = (User) ud;
UserPermission permission = getUserPermission(user);
if (UserPermission.ROLE_WRITE.equals(permission)) {
ArchiveDTO archive = extractJSONFromZipFile(bytes);
Form form = archive.getForm();
String uuid = form.getUUID();
ClientForm clientForm = this.formDao.findForm(user, client, uuid);
if (clientForm == null) {
if (!this.oauthservice.isValidClient(client)) {
throw new FormAccessDeniedException();
}
clientForm = new ClientForm();
}
clientForm.setClientid(client);
clientForm.setFormid(uuid);
clientForm.setData(archive.getFormJSON());
clientForm.setInsertedDate(form.getInsertedDate());
clientForm.setUpdatedDate(form.getUpdatedDate());
clientForm.setParentUUID(form.getParentUUID());
updateAsset(clientForm, bytes);
this.formDao.saveForm(clientForm);
FormLog formLog = new FormLog();
formLog.setFormId(uuid);
formLog.setClientid(client);
formLog = this.formDao.saveFormLog(formLog);
return formLog;
}
throw new FormAccessDeniedException();
}
@Override
public WorkflowLog saveWorkflow(final UserDetails ud, final String client,
final byte[] bytes) {
User user = (User) ud;
UserPermission permission = getUserPermission(user);
if (UserPermission.ROLE_WRITE.equals(permission)) {
ArchiveDTO archive = extractJSONFromZipFile(bytes);
Workflow workflow = archive.getWorkflow();
String uuid = workflow.getUUID();
ClientWorkflow clientWorkflow = this.workflowDao
.findWorkflow(user, client, uuid);
if (clientWorkflow == null) {
if (!this.oauthservice.isValidClient(client)) {
throw new FormAccessDeniedException();
}
clientWorkflow = new ClientWorkflow();
}
clientWorkflow.setClientid(client);
clientWorkflow.setUUID(uuid);
clientWorkflow.setData(archive.getWorkflowJSON());
clientWorkflow.setInsertedDate(workflow.getInsertedDate());
clientWorkflow.setUpdatedDate(workflow.getUpdatedDate());
clientWorkflow.setParentUUID(workflow.getParentUUID());
updateAsset(clientWorkflow, bytes);
this.workflowDao.saveWorkflow(clientWorkflow);
WorkflowLog log = new WorkflowLog();
log.setClientid(client);
log.setUUID(uuid);
log = this.workflowDao.saveWorkflowLog(log);
return log;
}
throw new FormAccessDeniedException();
}
@Override
public void saveWorkflowRecord(final UserDetails ud, final String client,
final byte[] bytes) {
// User user = (User) ud;
//
// ArchiveDTO archive = extractJSONFromZipFile(bytes);
// Form form = archive.getFormRecord();
//
// String formId = form.getParentUUID();
// String recordId = form.getUUID();
//
// ClientFormRecord record = this.formRecordDao.findFormRecord(user,
// client, formId, recordId);
//
// if (record == null) {
//
// ClientForm clientForm = this.formDao.findForm(user, client, formId);
// if (clientForm == null) {
// throw new FormNotFoundException(formId);
// }
//
// record = new ClientFormRecord();
// }
//
// updateAsset(record, bytes);
//
// record.setClientid(client);
// record.setFormId(formId);
// record.setFormRecordId(recordId);
// record.setData(archive.getFormrecordJSON());
// record.setInsertedDate(form.getInsertedDate());
// record.setUpdatedDate(form.getUpdatedDate());
//
// this.formRecordDao.saveFormRecord(record);
//
// FormRecordLog log = new FormRecordLog();
// log.setFormId(formId);
// log.setClientId(client);
// log.setFormRecordId(recordId);
// this.formRecordDao.saveFormRecordLog(log);
}
/**
* Update Asset on object.
* @param obj Form
* @param bytes byte[]
*/
private void updateAsset(final AssetHolder obj, final byte[] bytes) {
Asset asset = null;
if (!StringUtils.isEmpty(obj.getAssetid())) {
asset = this.formDao.findAsset(obj.getAssetid());
}
if (asset == null) {
asset = new Asset();
}
asset.setData(this.userDao.convertToBlob(bytes));
this.formDao.saveAsset(asset);
obj.setAssetid(asset.getAssetId());
obj.setSha1hash(DigestUtils.sha1Hex(bytes));
}
}