Skip to content
This repository was archived by the owner on Oct 18, 2018. It is now read-only.

Commit e815c21

Browse files
committed
Import annotations from annotated document.
1 parent 84d951f commit e815c21

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

src/main/java/com/groupdocs/ui/Utils.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.groupdocs.ui;
22

33
import com.groupdocs.annotation.common.license.License;
4+
import com.groupdocs.annotation.domain.AnnotationInfo;
45
import com.groupdocs.annotation.domain.config.AnnotationConfig;
6+
import com.groupdocs.annotation.domain.results.CreateAnnotationResult;
57
import com.groupdocs.annotation.handler.AnnotationImageHandler;
68
import com.groupdocs.annotation.handler.input.IDocumentDataHandler;
79
import com.groupdocs.annotation.handler.input.dataobjects.Document;
810

9-
import java.io.FileInputStream;
1011
import java.io.IOException;
1112
import java.io.InputStream;
12-
import java.nio.file.FileSystem;
1313
import java.nio.file.FileSystems;
1414
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
17+
import java.util.Arrays;
1518
import java.util.Properties;
1619

1720
public class Utils {
@@ -22,28 +25,39 @@ public class Utils {
2225

2326
public static AnnotationImageHandler createAnnotationImageHandler() {
2427
AnnotationConfig cfg = new AnnotationConfig();
25-
cfg.setStoragePath(getStoragePath());
28+
cfg.setStoragePath(getStoragePath().toString());
2629
AnnotationImageHandler annotator = new AnnotationImageHandler(cfg);
2730
return annotator;
2831

2932
}
3033

31-
public static Document findDocumentByName(String name) {
34+
synchronized public static Document findDocumentByName(String name) {
3235
AnnotationImageHandler imageHandler = Utils.createAnnotationImageHandler();
3336
IDocumentDataHandler documentDataHandler = imageHandler.getDocumentDataHandler();
3437
Document doc = documentDataHandler.getDocument(name);
3538
if (doc != null) {
3639
return doc;
3740
}
3841

39-
long documentId = imageHandler.createDocument(name);
40-
41-
try (InputStream original = new FileInputStream(Utils.getStoragePath() + "/" + name)) {
42-
// imageHandler.importAnnotations(original, name);
42+
AnnotationInfo[] importedAnnotations;
43+
try (InputStream original = Files.newInputStream(getStoragePath(name))) {
44+
importedAnnotations = imageHandler.importAnnotations(original);
4345
} catch (Exception x) {
4446
throw new RuntimeException(x);
4547
}
46-
return documentDataHandler.get(documentId);
48+
49+
long documentId = imageHandler.createDocument(name);
50+
Arrays.stream(importedAnnotations)
51+
.forEach(ai -> {
52+
ai.setDocumentGuid(documentId);
53+
CreateAnnotationResult car = imageHandler.createAnnotation(ai);
54+
Arrays.stream(ai.getReplies())
55+
.forEach(ari -> {
56+
imageHandler.createAnnotationReply(car.getId(), ari.getMessage());
57+
});
58+
});
59+
doc = documentDataHandler.get(documentId);
60+
return doc;
4761
}
4862

4963
public static void loadLicense() {
@@ -56,10 +70,14 @@ public static void loadLicense() {
5670
}
5771
}
5872

59-
public static String getStoragePath() {
73+
public static String getSt__oragePath() {
6074
return getProjectProperty("storage.path");
6175
}
6276

77+
public static Path getStoragePath(String... name) {
78+
return Paths.get(getProjectProperty("storage.path"), name);
79+
}
80+
6381
public static String getProjectProperty(String name) {
6482
Properties p = new Properties();
6583
try (InputStream i = Utils.class.getResourceAsStream("/project.properties")) {

src/main/java/com/groupdocs/ui/servlet/DownloadAnnotatedServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import javax.servlet.http.HttpServlet;
1212
import javax.servlet.http.HttpServletRequest;
1313
import javax.servlet.http.HttpServletResponse;
14-
import java.io.FileInputStream;
1514
import java.io.IOException;
1615
import java.io.InputStream;
16+
import java.nio.file.Files;
1717
import java.util.Arrays;
1818
import java.util.List;
1919

@@ -28,7 +28,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
2828
List<AnnotationInfo> list = Arrays.asList(imageHandler.getAnnotations(document.getId()).getAnnotations());
2929

3030
InputStream exported;
31-
try (InputStream original = new FileInputStream(Utils.getStoragePath() + "/" + filename)) {
31+
try (InputStream original = Files.newInputStream(Utils.getStoragePath(filename))) {
3232
exported = imageHandler.exportAnnotationsToDocument(original, list);
3333
} catch (Exception x) {
3434
throw new RuntimeException(x);

src/main/java/com/groupdocs/ui/servlet/FilesServlet.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import javax.servlet.http.HttpServletResponse;
1111
import java.io.IOException;
1212
import java.nio.file.Files;
13-
import java.nio.file.Paths;
1413
import java.util.ArrayList;
1514

1615
@WebServlet("/files")
@@ -20,7 +19,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
2019

2120
ArrayList<String> list = new ArrayList<>();
2221
Files.newDirectoryStream(
23-
Paths.get(Utils.getStoragePath()),
22+
Utils.getStoragePath(),
2423
entry -> Files.isRegularFile(entry) && !entry.getFileName().toString().startsWith("GroupDocs.") && !entry.getFileName().toString().equalsIgnoreCase("README.txt")
2524
).forEach(path -> {
2625
list.add(path.getFileName().toString());

0 commit comments

Comments
 (0)