Commit b4b09319 authored by Raimbek Egemberdiev's avatar Raimbek Egemberdiev

рефакторинг импорта завершена

parents e204aeb0 a132f0a8
package kz.arta.synergy.api.services;
import kz.arta.synergy.api.JsonUtils;
import kz.arta.synergy.api.Query;
import kz.arta.synergy.api.QueryContext;
import kz.arta.synergy.api.RestHttpQuery;
import kz.arta.synergy.api.services.storage.Attachment;
import kz.arta.synergy.api.services.storage.AttachmentList;
import java.io.IOException;
/**
* Created by: Abzal Kultayev
* Date: 12.11.16
* Time: 16:37
*/
public class StorageService {
private final RestHttpQuery restHttpQuery;
private StorageService(QueryContext context) {
this.restHttpQuery = new RestHttpQuery(context);
}
public static StorageService newInstance(QueryContext context) {
return new StorageService(context);
}
/**
* Создает временный файл на сервере
* @return Возвращает путь временного файла на сервере
* @throws IOException
*/
public String startUploadFile() throws IOException {
Query query = Query.newInstance().url("/rest/api/storage/start_upload");
String result = restHttpQuery.doQuery(query);
return JsonUtils.getValueByKey(result, "file");
}
/**
* Метод добавляет вложение к документу из временного файла, загруженного на сервер с помощью API метода rest/api/storage/start_upload
* @param documentID идентификатор документа
* @param filename название файла
* @param filepath путь до файла на сервере
* @return errorCode и errorMessage
* @throws IOException при проблеммах с сетью
*/
public String createAttachment(String documentID, String filename, String filepath) throws IOException {
Query query = Query.newInstance()
.methodPost()
.url("/rest/api/docflow/doc/attachment/create")
.formParam("fileName", filename)
.formParam("filePath", filepath)
.formParam("path", "ase:workContainer")
.formParam("documentID", documentID);
return restHttpQuery.doQuery(query);
}
public Attachment createAndGetAttachment(String documentID, String filename, String filepath) throws IOException {
String attachment = createAttachment(documentID, filename, filepath);
if (!JsonUtils.getValueByKey(attachment, "errorCode").equals("0")) {
throw new IOException("Attachment was not created");
}
AttachmentList lastAttachments = getLastAttachments(documentID);
return lastAttachments.get(0);
}
public AttachmentList getLastAttachments(String documentID) throws IOException {
Query query = Query.newInstance()
.url("/rest/api/docflow/doc/last_attachments")
.queryParam("documentID", documentID);
return JsonUtils.read(restHttpQuery.doQuery(query), AttachmentList.class);
}
}
package kz.arta.synergy.api.services.storage;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
/**
* Created by: Abzal Kultayev
* Date: 14.11.16
* Time: 13:19
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Attachment {
private String uuid;
private String dataUUID;
private boolean mobile;
private String name;
@JsonProperty("is_editable")
private boolean isEditable;
@JsonProperty("created_label")
private String createdLabel;
private String created;
private String icon;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getDataUUID() {
return dataUUID;
}
public void setDataUUID(String dataUUID) {
this.dataUUID = dataUUID;
}
public boolean isMobile() {
return mobile;
}
public void setMobile(boolean mobile) {
this.mobile = mobile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEditable() {
return isEditable;
}
public void setEditable(boolean editable) {
isEditable = editable;
}
public String getCreatedLabel() {
return createdLabel;
}
public void setCreatedLabel(String createdLabel) {
this.createdLabel = createdLabel;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
package kz.arta.synergy.api.services.storage;
import java.util.ArrayList;
/**
* Created by: Abzal Kultayev
* Date: 14.11.16
* Time: 13:36
*/
public class AttachmentList extends ArrayList<Attachment> {
}
package kz.arta.synergy.api.services;
import kz.arta.synergy.api.QueryContext;
import kz.arta.synergy.api.services.storage.Attachment;
import kz.arta.synergy.api.services.storage.AttachmentList;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
/**
* Created by: Abzal Kultayev
* Date: 12.11.16
* Time: 16:56
*/
public class StorageServiceTest {
StorageService service = StorageService.newInstance(new QueryContext("http://192.168.3.233:8080/Synergy", "NppAdmin", "123456"));
private String documentID = "05647530-a72f-11e6-bfd9-00163ebf20cd";
@Test
public void testStartUploadFile() throws Exception {
String tempFileName = service.startUploadFile();
assertNotNull(tempFileName);
}
@Test
public void testCreateAttachment() throws Exception {
String tempFileName = service.startUploadFile();
String attachment = service.createAttachment(documentID, "FILENAME.PDF", tempFileName);
assertEquals(attachment, "");
}
@Test
public void testLastAttachments() throws Exception {
AttachmentList attachmentList = service.getLastAttachments(documentID);
assertNotNull(attachmentList);
}
@Test
public void testCreateAndGetAttachment() throws Exception {
String tempFileName = service.startUploadFile();
Attachment attachment = service.createAndGetAttachment(documentID, "Filename.pdf", tempFileName);
assertNotNull(attachment);
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment