public boolean validate(NativeDocument document) { if (document == null) { return false; } String objectClass = document.getTagValue(RepositoryStoreConstants.TAG_NAME_OBJECT_CLASS); if (!BWMetaImporterConstants.SOURCE_FORMAT.equals(objectClass)) { return false; } Attachment<?> sourceAttachment = document.getAttachments().get(CommonExtractorContstants.SOURCE_ATTRIBUTE); return sourceAttachment != null; }
private Attachment<?> getAttachmentPart(NativeDocument document, String fileName, int totalAttachmentsCount) throws Exception { List<Attachment<?>> contentAttachments = new ArrayList<>(); for (Entry<String, ? extends Attachment<?>> attachment : document.getAttachments().entrySet()) { if (isContentAttachment(attachment.getValue())) { contentAttachments.add(attachment.getValue()); String partFilename = getPartFilename(document, attachment.getValue()); if (StringUtils.equals(fileName, partFilename)) { return attachment.getValue(); } } } if (contentAttachments.isEmpty() || totalAttachmentsCount != 1) { return null; } return contentAttachments.get(0); }
@Override public void doWithFile(YContentFile file) { String type = file.getType(); if (FileTypes.FT_PLAIN_TEXT.equals(type)) { String location = file.getLocations().get(0); // System.out.println("adding..."+location); if (location != null) { Attachment<?> part = document.getAttachments().get(location); String text = ""; if (part instanceof BinaryAttachment) { byte[] content = ((BinaryAttachment) part).getContent(); text = new String(content); } textBuilder.append(text); } } }
@Override public NativeDocument process(final NativeDocument input) { if (MapUtils.isNotEmpty(attachmentQueryMap)) { for (Attachment<?> attachment : input.getAttachments().values()) { if (isPathAccepted(attachment.getId())) { List<String> invalidPaths = new ArrayList<String>(); processSingleAttachment(input, attachment, invalidPaths); if (!invalidPaths.isEmpty()) { attachment.addTagByKey(PROPERTY_INVALID_PATHS, StringUtils.join(invalidPaths, ";")); } } } } return input; }
protected DataResponse toDataResponse(YElement element, NativeDocument document) { DataResponse response = new DataResponse(element, dataResponseQualityComparator); for (Attachment<?> attachment : document.getAttachments().values()) { Resource resource = toResource(document, attachment); if (resource != null) { response.addContent(attachment.getId(), resource); } } return response; }
@Override public Iterable<DataResponse> feedFromCurrentElement() { NativeDocument document = state.getCurrentDocument(); if(document == null){ return Collections.emptyList(); } String objectClass = document.getTagValue(RepositoryStoreConstants.TAG_NAME_OBJECT_CLASS); if(!BWMetaImporterConstants.SOURCE_FORMAT.equals(objectClass)){ return Collections.emptyList(); } String levelToImport = getLevelToImport(document); Attachment<?> sourceAttachment = document.getAttachments().get(CommonExtractorContstants.SOURCE_ATTRIBUTE); if (sourceAttachment == null) { warn("No source attachment.", document.getId()); return Collections.emptyList(); } InputStream inputStream = getStream(sourceAttachment); List<YElement> elements = builder.build(new InputStreamResource(inputStream, "Record id: " + document.getId())); Stream<YElement> elementsStream = elements.stream(); if (StringUtils.isNotBlank(levelToImport)) { elementsStream = elementsStream.filter(e -> StringUtils.equals(YModelUtils.getType(e), levelToImport)); } return elementsStream.map(e -> toDataResponse(e, document)).collect(Collectors.toList()); }
private String getPartFilename(NativeDocument document, Attachment<?> attachment) throws Exception { String name = attachment.getTagValue(RepositoryStoreConstants.TAG_NAME_CONTENT_NAME); if (name != null) { return name; } String xpath = attachment.getTagValue(ImporterConstants.PROPERTY_ATTACHMENT_PATH_IN_METADATA); if (xpath == null) { return null; } xpath = xpath.replace(LOCATION_XPATH_NAME, FILE_NAME_XPATH_NAME); String metadataLocation = attachment.getTagValue(ImporterConstants.PROPERTY_PARENT_ATTACHMENT); if (metadataLocation == null) { return null; } Attachment<?> metadataAttachment = document.getAttachments().get(metadataLocation); JXPathContext context = getBwMeta(metadataAttachment); try { String attachmentFileName = getFileName(context, xpath); return attachmentFileName; } catch (JXPathNotFoundException e) { LOGGER.warn("Couldn't retrieve attachment name from document: " + document.getId() + " and path: " + xpath); return null; } }