11package com .groupdocs .ui .annotation .service ;
22
3- import com .google .common .collect .Lists ;
43import com .groupdocs .annotation .common .exception .AnnotatorException ;
54import com .groupdocs .annotation .common .license .License ;
65import com .groupdocs .annotation .domain .AnnotationInfo ;
4342import java .util .Base64 ;
4443import java .util .List ;
4544
45+ import static com .groupdocs .ui .annotation .util .DocumentTypesConverter .checkedDocumentType ;
4646import static com .groupdocs .ui .annotation .util .DocumentTypesConverter .getDocumentType ;
4747import static com .groupdocs .ui .annotation .util .PathConstants .OUTPUT_FOLDER ;
4848
4949@ Service
5050public class AnnotationServiceImpl implements AnnotationService {
5151 private static final Logger logger = LoggerFactory .getLogger (AnnotationServiceImpl .class );
5252
53- private static final List <String > supportedImageFormats = Lists .newArrayList ("bmp" , "jpeg" , "jpg" , "tiff" , "tif" , "png" , "gif" , "emf" , "wmf" , "dwg" , "dicom" , "djvu" );
54- private static final List <String > supportedDiagramFormats = Lists .newArrayList (".vsd" , ".vdx" , ".vss" , ".vsx" , ".vst" , ".vtx" , ".vsdx" , ".vdw" , ".vstx" , ".vssx" );
55-
5653 @ Autowired
5754 private GlobalConfiguration globalConfiguration ;
5855
@@ -83,11 +80,11 @@ public void init() {
8380 }
8481
8582 private void initOutputDirectory () {
86- if (StringUtils .isEmpty (annotationConfiguration .getOutputDirectory ())) {
83+ if (StringUtils .isEmpty (annotationConfiguration .getOutputDirectory ())) {
8784 String outputDirectory = String .format ("%s%s" , annotationConfiguration .getFilesDirectory (), OUTPUT_FOLDER );
8885 annotationConfiguration .setOutputDirectory (outputDirectory );
8986 }
90- if (!new File (annotationConfiguration .getOutputDirectory ()).exists ()) {
87+ if (!new File (annotationConfiguration .getOutputDirectory ()).exists ()) {
9188 new File (annotationConfiguration .getOutputDirectory ()).mkdirs ();
9289 }
9390 }
@@ -110,7 +107,7 @@ public List<FileDescriptionEntity> getFileList(FileTreeRequest fileTreeRequest)
110107 // get temp directory name
111108 String tempDirectoryName = new AnnotationConfig ().getTempFolderName ();
112109 try {
113- FileTreeContainer fileListContainer = getAnnotationImageHandler () .loadFileTree (fileListOptions );
110+ FileTreeContainer fileListContainer = annotationHandler .loadFileTree (fileListOptions );
114111
115112 List <FileDescriptionEntity > fileList = new ArrayList <>();
116113 // parse files/folders list
@@ -153,24 +150,17 @@ public AnnotatedDocumentEntity getDocumentDescription(LoadDocumentRequest loadDo
153150 }
154151 // get document info container
155152 String fileName = new File (documentGuid ).getName ();
156- DocumentInfoContainer documentDescription = getAnnotationImageHandler () .getDocumentInfo (fileName , password );
153+ DocumentInfoContainer documentDescription = annotationHandler .getDocumentInfo (fileName , password );
157154
158- String documentType = documentDescription .getDocumentType ();
159- String fileExtension = parseFileExtension (documentGuid );
160- // check if document type is image
161- if (supportedImageFormats .contains (fileExtension )) {
162- documentType = "image" ;
163- } else if (supportedDiagramFormats .contains (fileExtension )) {
164- documentType = "diagram" ;
165- }
155+ String documentType = checkedDocumentType (documentGuid , documentDescription .getDocumentType ());
166156 // check if document contains annotations
167157 AnnotationInfo [] annotations = getAnnotations (documentGuid , documentType );
168158 // get info about each document page
169159 List <PageImage > pageImages = null ;
170160 List <PageData > pages = documentDescription .getPages ();
171161 // TODO: remove once perf. issue is fixed
172- if (annotationConfiguration .getPreloadPageCount () == 0 ){
173- pageImages = getAnnotationImageHandler () .getPages (fileName , imageOptions );
162+ if (annotationConfiguration .getPreloadPageCount () == 0 ) {
163+ pageImages = annotationHandler .getPages (fileName , imageOptions );
174164 }
175165 String [] supportedAnnotations = SupportedAnnotations .getSupportedAnnotations (documentType );
176166 // initiate custom Document description object
@@ -191,7 +181,7 @@ public AnnotatedDocumentEntity getDocumentDescription(LoadDocumentRequest loadDo
191181 page .setAnnotations (AnnotationMapper .instance .mapForPage (annotations , page .getNumber ()));
192182 }
193183 // TODO: remove once perf. issue is fixed
194- if (pageImages != null ) {
184+ if (pageImages != null ) {
195185 byte [] bytes = IOUtils .toByteArray (pageImages .get (i ).getStream ());
196186 String encodedImage = Base64 .getEncoder ().encodeToString (bytes );
197187 page .setData (encodedImage );
@@ -223,7 +213,7 @@ public LoadedPageEntity getDocumentPage(LoadDocumentPageRequest loadDocumentPage
223213 }
224214 // get page image
225215 String fileName = new File (documentGuid ).getName ();
226- List <PageImage > images = getAnnotationImageHandler () .getPages (fileName , imageOptions );
216+ List <PageImage > images = annotationHandler .getPages (fileName , imageOptions );
227217
228218 byte [] bytes = IOUtils .toByteArray (images .get (pageNumber - 1 ).getStream ());
229219 // encode ByteArray into String
@@ -238,15 +228,6 @@ public LoadedPageEntity getDocumentPage(LoadDocumentPageRequest loadDocumentPage
238228 }
239229 }
240230
241- /**
242- * Create new instance for AnnotationImageHandler
243- *
244- * @return AnnotationImageHandler
245- */
246- private AnnotationImageHandler getAnnotationImageHandler () {
247- return annotationHandler ;
248- }
249-
250231 @ Override
251232 public AnnotatedDocumentEntity annotate (AnnotateDocumentRequest annotateDocumentRequest ) {
252233 AnnotatedDocumentEntity annotatedDocument = new AnnotatedDocumentEntity ();
@@ -255,55 +236,76 @@ public AnnotatedDocumentEntity annotate(AnnotateDocumentRequest annotateDocument
255236 String documentGuid = annotateDocumentRequest .getGuid ();
256237 String password = annotateDocumentRequest .getPassword ();
257238 AnnotationDataEntity [] annotationsData = annotateDocumentRequest .getAnnotationsData ();
258- String documentType = annotateDocumentRequest .getDocumentType ();
239+ String documentType = checkedDocumentType ( documentGuid , annotateDocumentRequest .getDocumentType () );
259240 // initiate AnnotatedDocument object
260241 // get document info - required to get document page height and calculate annotation top position
261- DocumentInfoContainer documentInfo = getAnnotationImageHandler ().getDocumentInfo (new File (documentGuid ).getName (), password );
262- // check if document type is image
263- String fileExtension = parseFileExtension (documentGuid );
264- if (supportedImageFormats .contains (fileExtension )) {
265- documentType = "image" ;
266- }
242+ DocumentInfoContainer documentInfo = annotationHandler .getDocumentInfo (new File (documentGuid ).getName (), password );
267243 // initiate list of annotations to add
268244 List <AnnotationInfo > annotations = new ArrayList <>();
269245 InputStream file = new FileInputStream (documentGuid );
270- file = getAnnotationImageHandler ().removeAnnotationStream (file );
271- Throwable exc = null ;
246+ file = annotationHandler .removeAnnotationStream (file );
272247 for (AnnotationDataEntity annotationData : annotationsData ) {
273248 // create annotator
274249 PageData pageData = documentInfo .getPages ().get (annotationData .getPageNumber () - 1 );
275250 // add annotation, if current annotation type isn't supported by the current document type it will be ignored
276251 try {
277252 annotations .add (AnnotatorFactory .createAnnotator (annotationData , pageData ).getAnnotationInfo (documentType ));
278- } catch (UnsupportedOperationException ex ) {
279- exc = ex ;
280253 } catch (Exception ex ) {
281254 throw new TotalGroupDocsException (ex .getMessage (), ex );
282255 }
283256 }
284- String fileName = new File (documentGuid ).getName ();
257+ String forPrint = annotateDocumentRequest .getPrint () ? "Temp" : "" ;
258+ String fileName = FilenameUtils .getBaseName (documentGuid ) + forPrint + "." + FilenameUtils .getExtension (documentGuid );
285259 String path = annotationConfiguration .getOutputDirectory () + File .separator + fileName ;
286260 // check if annotations array contains at least one annotation to add
287261 if (annotations .size () > 0 ) {
288262 // Add annotation to the document
289263 int type = getDocumentType (documentType );
290264 // Save result stream to file.
291- file = getAnnotationImageHandler () .exportAnnotationsToDocument (file , annotations , type );
265+ file = annotationHandler .exportAnnotationsToDocument (file , annotations , type );
292266 }
293267 (new File (path )).delete ();
294- try (OutputStream fileStream = new FileOutputStream (path )) {
295- IOUtils .copyLarge (file , fileStream );
296- annotatedDocument .setGuid (path );
268+ if (annotateDocumentRequest .getPrint ()) {
269+ List <PageDataDescriptionEntity > annotatedPages = getAnnotatedPages (password , file );
270+ annotatedDocument .setPages (annotatedPages );
271+ (new File (path )).delete ();
272+ } else {
273+ try (OutputStream fileStream = new FileOutputStream (path )) {
274+ IOUtils .copyLarge (file , fileStream );
275+ annotatedDocument .setGuid (path );
276+ }
297277 }
298278 } catch (Exception ex ) {
299279 throw new TotalGroupDocsException (ex .getMessage (), ex );
300280 }
301281 return annotatedDocument ;
302282 }
303283
304- public String parseFileExtension (String documentGuid ) {
305- String extension = FilenameUtils .getExtension (documentGuid );
306- return extension == null ? null : extension .toLowerCase ();
284+ /**
285+ * Get pages images of annotated file
286+ *
287+ * @param password password for the file
288+ * @param inputStream stream of annotated file
289+ * @return list of pages
290+ * @throws IOException
291+ */
292+ private List <PageDataDescriptionEntity > getAnnotatedPages (String password , InputStream inputStream ) throws IOException {
293+ ImageOptions imageOptions = new ImageOptions ();
294+ // set password for protected document
295+ if (!password .isEmpty ()) {
296+ imageOptions .setPassword (password );
297+ }
298+ List <PageImage > pages = annotationHandler .getPages (inputStream , imageOptions );
299+ List <PageDataDescriptionEntity > pagesDescriptions = new ArrayList <>(pages .size ());
300+ for (PageImage pageImage : pages ) {
301+ byte [] bytes = IOUtils .toByteArray (pageImage .getStream ());
302+ String encodedImage = Base64 .getEncoder ().encodeToString (bytes );
303+ PageDataDescriptionEntity page = new PageDataDescriptionEntity ();
304+ page .setData (encodedImage );
305+
306+ pagesDescriptions .add (page );
307+ }
308+ return pagesDescriptions ;
307309 }
308310
309311 /**
@@ -315,9 +317,8 @@ public String parseFileExtension(String documentGuid) {
315317 */
316318 private AnnotationInfo [] getAnnotations (String documentGuid , String documentType ) {
317319 try (InputStream documentStream = new FileInputStream (documentGuid )) {
318- AnnotationImageHandler annotationImageHandler = getAnnotationImageHandler ();
319320 int docType = getDocumentType (documentType );
320- return new Importer (documentStream , annotationImageHandler ).importAnnotations (docType );
321+ return new Importer (documentStream , annotationHandler ).importAnnotations (docType );
321322 } catch (AnnotatorException annotatorException ) {
322323 logger .error ("Exception while extract annotations from file {}: {}" , FilenameUtils .getName (documentGuid ), annotatorException .getCause ().getLocalizedMessage ());
323324 return new AnnotationInfo [0 ];
0 commit comments