Java Code Examples for com.github.jsonldjava.utils.JsonUtils#toString()

The following examples show how to use com.github.jsonldjava.utils.JsonUtils#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationStoreRepositoryJDBCImplTest.java    From elucidate-server with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void testUpdateAnnotation() throws Exception {

    W3CAnnotation w3cAnnotation = generateRandomW3CAnnotation();

    String collectionId = w3cAnnotation.getCollectionId();
    String annotationId = w3cAnnotation.getAnnotationId();
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);

    Object[] params = {collectionId, annotationId, jsonStr, null};
    int[] sqlTypes = {Types.VARCHAR, Types.VARCHAR, Types.OTHER, Types.INTEGER};
    when(jdbcTemplate.query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationRowMapper) any())).thenReturn(new ArrayList<W3CAnnotation>() {
        {
            add(w3cAnnotation);
        }
    });
    W3CAnnotation returnedW3CAnnotation = annotationStoreRepository.createAnnotation(collectionId, annotationId, jsonStr, null);
    verify(jdbcTemplate, times(1)).query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationRowMapper) any());

    assertThat(w3cAnnotation, is(equalTo(returnedW3CAnnotation)));
}
 
Example 2
Source File: AnnotationStoreRepositoryJDBCImplTest.java    From elucidate-server with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void testCreateAnnotation() throws Exception {

    W3CAnnotation w3cAnnotation = generateRandomW3CAnnotation();

    String collectionId = w3cAnnotation.getCollectionId();
    String annotationId = w3cAnnotation.getAnnotationId();
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);

    Object[] params = {collectionId, annotationId, jsonStr, null};
    int[] sqlTypes = {Types.VARCHAR, Types.VARCHAR, Types.OTHER, Types.INTEGER};
    when(jdbcTemplate.query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationRowMapper) any())).thenReturn(new ArrayList<W3CAnnotation>() {
        {
            add(w3cAnnotation);
        }
    });
    W3CAnnotation returnedW3CAnnotation = annotationStoreRepository.createAnnotation(collectionId, annotationId, jsonStr, null);
    verify(jdbcTemplate, times(1)).query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationRowMapper) any());

    assertThat(w3cAnnotation, is(equalTo(returnedW3CAnnotation)));
}
 
Example 3
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 6 votes vote down vote up
private void createAnnotationTargets(W3CAnnotation w3cAnnotation) throws IOException {
    int annotationPk = w3cAnnotation.getPk();
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();

    LOGGER.info(String.format("Processing `target` values for W3CAnnotation [%s]", w3cAnnotation));
    List<AnnotationTarget> annotationTargets = new AnnotationTargetExtractor().extractTargets(jsonMap);
    LOGGER.info(String.format("Got [%s] `target` values for W3CAnnotation [%s]", annotationTargets.size(), w3cAnnotation));
    for (AnnotationTarget annotationTarget : annotationTargets) {

        String targetIri = annotationTarget.getTargetIri();
        String sourceIri = annotationTarget.getSourceIri();
        String targetJson = JsonUtils.toString(annotationTarget.getJsonMap());
        LOGGER.info(String.format("Preparing to create `target` with ID [%s], source IRI [%s] and JSON [%s]", targetIri, sourceIri, targetJson));
        annotationTarget = annotationTargetStoreRepository.createAnnotationTarget(annotationPk, targetIri, sourceIri, targetJson);
        LOGGER.info(String.format("Created AnnotationTarget [%s] in repository", annotationTarget));

        int targetPk = annotationTarget.getPk();
        Map<String, Object> targetJsonMap = annotationTarget.getJsonMap();
        createAnnotationSelectors(null, targetPk, targetJsonMap);
        createAnnotationCreators(null, null, targetPk, targetJsonMap);
        createAnnotationGenerators(null, null, targetPk, targetJsonMap);
        createAnnotationTemporals(null, null, targetPk, targetJsonMap);
    }
}
 
Example 4
Source File: AnnotationCollectionStoreRepositoryJDBCImplTest.java    From elucidate-server with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void testCreateAnnotationCollection() throws Exception {

    W3CAnnotationCollection w3cAnnotationCollection = generateRandomW3CAnnotationCollection();

    String collectionId = w3cAnnotationCollection.getCollectionId();
    Map<String, Object> jsonMap = w3cAnnotationCollection.getJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);

    Object[] params = {collectionId, jsonStr};
    int[] sqlTypes = {Types.VARCHAR, Types.OTHER};
    when(jdbcTemplate.query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationCollectionRowMapper) any())).thenReturn(new ArrayList<W3CAnnotationCollection>() {
        {
            add(w3cAnnotationCollection);
        }
    });
    W3CAnnotationCollection returnedW3CAnnotationCollection = annotationCollectionStoreRepository.createAnnotationCollection(collectionId, jsonStr);
    verify(jdbcTemplate, times(1)).query(anyString(), aryEq(params), aryEq(sqlTypes), (W3CAnnotationCollectionRowMapper) any());

    assertThat(w3cAnnotationCollection, is(equalTo(returnedW3CAnnotationCollection)));
}
 
Example 5
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 5 votes vote down vote up
private void createAnnotationXPathSelectors(Integer bodyPk, Integer targetPk, Map<String, Object> jsonMap) throws IOException {
    List<AnnotationXPathSelector> annotationXPathSelectors = new AnnotationXPathSelectorExtractor().extractSelectors(jsonMap);
    for (AnnotationXPathSelector annotationXPathSelector : annotationXPathSelectors) {

        String selectorIri = annotationXPathSelector.getSelectorIri();
        String value = annotationXPathSelector.getValue();
        String selectorJson = JsonUtils.toString(annotationXPathSelector.getJsonMap());
        annotationSelectorStoreRepository.createAnnotationXPathSelector(bodyPk, targetPk, selectorIri, value, selectorJson);
    }
}
 
Example 6
Source File: AbstractSchemaValidatorTest.java    From elucidate-server with MIT License 5 votes vote down vote up
protected void validateJson(String jsonFileName) throws IOException, ProcessingException, JsonLdError {

        JsonNode schema = getSchema();
        assertNotNull(schema);

        String jsonStr = getJson(jsonFileName);
        assertNotNull(jsonStr);

        Object jsonObj = JsonUtils.fromString(jsonStr);
        List<Object> expandedJson = JsonLdProcessor.expand(jsonObj);
        jsonStr = JsonUtils.toString(expandedJson);
        JsonNode json = JsonLoader.fromString(jsonStr);

        JsonValidator jsonValidator = JsonSchemaFactory.byDefault().getValidator();
        ProcessingReport processingReport = jsonValidator.validate(schema, json);
        assertNotNull(processingReport);
        if (!processingReport.isSuccess()) {

            ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
            assertNotNull(jsonArray);

            Iterator<ProcessingMessage> iterator = processingReport.iterator();
            while (iterator.hasNext()) {
                ProcessingMessage processingMessage = iterator.next();
                jsonArray.add(processingMessage.asJson());
            }

            String errorJson = JsonUtils.toPrettyString(jsonArray);
            assertNotNull(errorJson);

            fail(errorJson);
        }
    }
 
Example 7
Source File: AbstractAnnotationCollectionServiceImpl.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
public ServiceResponse<C> createAnnotationCollection(String collectionId, C annotationCollection) {

    if (StringUtils.isBlank(collectionId)) {
        collectionId = idGenerator.generateId();
    }

    if (!validateCollectionId(collectionId)) {
        return new ServiceResponse<C>(Status.NON_CONFORMANT, null);
    }

    ServiceResponse<C> serviceResponse = getAnnotationCollection(collectionId, Collections.emptyList(), ClientPreference.MINIMAL_CONTAINER);
    Status status = serviceResponse.getStatus();
    if (status.equals(Status.OK)) {
        return new ServiceResponse<C>(Status.ALREADY_EXISTS, null);
    }

    W3CAnnotationCollection w3cAnnotationCollection = convertFromAnnotationCollection(annotationCollection);

    String w3cAnnotationCollectionJson;
    try {
        Map<String, Object> w3cAnnotationCollectionMap = w3cAnnotationCollection.getJsonMap();
        w3cAnnotationCollectionJson = JsonUtils.toString(w3cAnnotationCollectionMap);
    } catch (IOException e) {
        LOGGER.debug(String.format("Detected invalid JSON on W3C Annotation Collection [%s]", w3cAnnotationCollection), e);
        return new ServiceResponse<C>(Status.NON_CONFORMANT, null);
    }

    annotationCollectionStoreRepository.createAnnotationCollection(collectionId, w3cAnnotationCollectionJson);
    return getAnnotationCollection(collectionId, Collections.emptyList(), ClientPreference.CONTAINED_DESCRIPTIONS);
}
 
Example 8
Source File: ValidationErrorTest.java    From elucidate-server with MIT License 5 votes vote down vote up
@Test
public void testValidationError() throws IOException {

    Map<String, Object> jsonMap = generateRandomJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);

    ValidationError validationError = new ValidationError();
    validationError.setJsonError(jsonStr);

    assertThat(jsonStr, is(equalTo(validationError.getJsonError())));
}
 
Example 9
Source File: SecurityUserReferenceCollectionConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
protected String getStringRepresentation(SecurityUserReferenceCollection obj, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = new HashMap<>();
    List<Map<String, String>> userJsonMaps = obj.getUsers().stream()
        .map(user -> ImmutableMap.of(
            "uid", user.getUid(),
            "id", user.getId()
        ))
        .collect(Collectors.toList());

    jsonMap.put("users", userJsonMaps);
    return JsonUtils.toString(jsonMap);
}
 
Example 10
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 5 votes vote down vote up
private void createAnnotationTextQuoteSelectors(Integer bodyPk, Integer targetPk, Map<String, Object> jsonMap) throws IOException {
    List<AnnotationTextQuoteSelector> annotationTextQuoteSelectors = new AnnotationTextQuoteSelectorExtractor().extractSelectors(jsonMap);
    for (AnnotationTextQuoteSelector annotationTextQuoteSelector : annotationTextQuoteSelectors) {

        String selectorIri = annotationTextQuoteSelector.getSelectorIri();
        String exact = annotationTextQuoteSelector.getExact();
        String prefix = annotationTextQuoteSelector.getPrefix();
        String suffix = annotationTextQuoteSelector.getSuffix();
        String selectorJson = JsonUtils.toString(annotationTextQuoteSelector.getJsonMap());
        annotationSelectorStoreRepository.createAnnotationTextQuoteSelector(bodyPk, targetPk, selectorIri, exact, prefix, suffix, selectorJson);
    }
}
 
Example 11
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 5 votes vote down vote up
private void createAnnotationTextPositionSelectors(Integer bodyPk, Integer targetPk, Map<String, Object> jsonMap) throws IOException {
    List<AnnotationTextPositionSelector> annotationTextPositionSelectors = new AnnotationTextPositionSelectorExtractor().extractSelectors(jsonMap);
    for (AnnotationTextPositionSelector annotationTextPositionSelector : annotationTextPositionSelectors) {

        String selectorIri = annotationTextPositionSelector.getSelectorIri();
        Integer start = annotationTextPositionSelector.getStart();
        Integer end = annotationTextPositionSelector.getEnd();
        String selectorJson = JsonUtils.toString(annotationTextPositionSelector.getJsonMap());
        annotationSelectorStoreRepository.createAnnotationTextPositionSelector(bodyPk, targetPk, selectorIri, start, end, selectorJson);
    }
}
 
Example 12
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 5 votes vote down vote up
private void createAnnotationSvgSelectors(Integer bodyPk, Integer targetPk, Map<String, Object> jsonMap) throws IOException {
    List<AnnotationSVGSelector> annotationSvgSelectors = new AnnotationSVGSelectorExtractor().extractSelectors(jsonMap);
    for (AnnotationSVGSelector annotationSvgSelector : annotationSvgSelectors) {

        String selectorIri = annotationSvgSelector.getSelectorIri();
        String value = annotationSvgSelector.getValue();
        String selectorJson = JsonUtils.toString(annotationSvgSelector.getJsonMap());
        annotationSelectorStoreRepository.createAnnotationSvgSelector(bodyPk, targetPk, selectorIri, value, selectorJson);
    }
}
 
Example 13
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 5 votes vote down vote up
private void createAnnotationFragmentSelector(Integer bodyPk, Integer targetPk, AnnotationFragmentSelector annotationFragmentSelector) throws IOException {

        String selectorIri = annotationFragmentSelector.getSelectorIri();
        String conformsTo = annotationFragmentSelector.getConformsTo();
        String value = annotationFragmentSelector.getValue();
        Integer x = annotationFragmentSelector.getX();
        Integer y = annotationFragmentSelector.getY();
        Integer w = annotationFragmentSelector.getW();
        Integer h = annotationFragmentSelector.getH();
        Integer start = annotationFragmentSelector.getStart();
        Integer end = annotationFragmentSelector.getEnd();
        String selectorJson = JsonUtils.toString(annotationFragmentSelector.getJsonMap());
        annotationSelectorStoreRepository.createAnnotationFragmentSelector(bodyPk, targetPk, selectorIri, conformsTo, value, x, y, w, h, start, end, selectorJson);
    }
 
Example 14
Source File: W3CAnnotationRowMapperTest.java    From elucidate-server with MIT License 5 votes vote down vote up
@Test
public void testRowMapper() throws SQLException, IOException {

    // Build up our fake data
    String annotationId = generateRandomId();
    String cacheKey = generateRandomCacheKey();
    String collectionId = generateRandomId();
    Date createdDateTime = generateRandomDate();
    boolean deleted = generateRandomBoolean();
    Map<String, Object> jsonMap = generateRandomJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);
    Date modifiedDateTime = generateRandomDate();

    // Stub out the result set
    ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.getString("annotationid")).thenReturn(annotationId);
    when(resultSet.getString("cachekey")).thenReturn(cacheKey);
    when(resultSet.getString("collectionid")).thenReturn(collectionId);
    when(resultSet.getTimestamp("createddatetime")).thenReturn(new Timestamp(createdDateTime.getTime()));
    when(resultSet.getBoolean("deleted")).thenReturn(deleted);
    when(resultSet.getString("json")).thenReturn(jsonStr);
    when(resultSet.getTimestamp("modifieddatetime")).thenReturn(new Timestamp(modifiedDateTime.getTime()));

    // Execute the row mapper
    W3CAnnotation w3cAnnotation = new W3CAnnotationRowMapper().mapRow(resultSet, 0);

    // Verify the W3CAnnotation fields
    assertThat(annotationId, is(equalTo(w3cAnnotation.getAnnotationId())));
    assertThat(cacheKey, is(equalTo(w3cAnnotation.getCacheKey())));
    assertThat(collectionId, is(equalTo(w3cAnnotation.getCollectionId())));
    assertThat(createdDateTime, is(equalTo(w3cAnnotation.getCreatedDateTime())));
    assertThat(deleted, is(equalTo(w3cAnnotation.isDeleted())));
    assertThat(jsonMap, is(equalTo(w3cAnnotation.getJsonMap())));
    assertThat(modifiedDateTime, is(equalTo(w3cAnnotation.getModifiedDateTime())));
}
 
Example 15
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 5 votes vote down vote up
private void createAnnotationCssSelectors(Integer bodyPk, Integer targetPk, Map<String, Object> jsonMap) throws IOException {
    List<AnnotationCSSSelector> annotationCssSelectors = new AnnotationCSSSelectorExtractor().extractSelectors(jsonMap);
    for (AnnotationCSSSelector annotationCssSelector : annotationCssSelectors) {

        String selectorIri = annotationCssSelector.getSelectorIri();
        String value = annotationCssSelector.getValue();
        String selectorJson = JsonUtils.toString(annotationCssSelector.getJsonMap());
        annotationSelectorStoreRepository.createAnnotationCssSelector(bodyPk, targetPk, selectorIri, value, selectorJson);
    }
}
 
Example 16
Source File: W3CAnnotationCollectionRowMapperTest.java    From elucidate-server with MIT License 5 votes vote down vote up
@Test
public void testRowMapper() throws SQLException, IOException {

    // Build up our fake data
    String cacheKey = generateRandomCacheKey();
    String collectionId = generateRandomId();
    Date createdDateTime = generateRandomDate();
    boolean deleted = generateRandomBoolean();
    Map<String, Object> jsonMap = generateRandomJsonMap();
    String jsonStr = JsonUtils.toString(jsonMap);
    Date modifiedDateTime = generateRandomDate();

    // Stub out the result set
    ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.getString("cachekey")).thenReturn(cacheKey);
    when(resultSet.getString("collectionid")).thenReturn(collectionId);
    when(resultSet.getTimestamp("createddatetime")).thenReturn(new Timestamp(createdDateTime.getTime()));
    when(resultSet.getBoolean("deleted")).thenReturn(deleted);
    when(resultSet.getString("json")).thenReturn(jsonStr);
    when(resultSet.getTimestamp("modifieddatetime")).thenReturn(new Timestamp(modifiedDateTime.getTime()));

    // Execute the row mapper
    W3CAnnotationCollection w3cAnnotationCollection = new W3CAnnotationCollectionRowMapper().mapRow(resultSet, 0);

    // Verify the W3CAnnotation fields
    assertThat(cacheKey, is(equalTo(w3cAnnotationCollection.getCacheKey())));
    assertThat(collectionId, is(equalTo(w3cAnnotationCollection.getCollectionId())));
    assertThat(createdDateTime, is(equalTo(w3cAnnotationCollection.getCreatedDateTime())));
    assertThat(deleted, is(equalTo(w3cAnnotationCollection.isDeleted())));
    assertThat(jsonMap, is(equalTo(w3cAnnotationCollection.getJsonMap())));
    assertThat(modifiedDateTime, is(equalTo(w3cAnnotationCollection.getModifiedDateTime())));
}
 
Example 17
Source File: AnnotationReferenceCollectionMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
protected String getStringRepresentation(AnnotationReferenceCollection obj, MediaType contentType)
    throws Exception {
    Map<String, Object> jsonMap = new HashMap<>();
    List<String> iris = obj.getAnnotations().stream()
        .map(ref -> iriBuilder.buildW3CAnnotationIri(ref.getCollectionId(), ref.getId()))
        .collect(Collectors.toList());

    jsonMap.put("annotations", iris);
    return JsonUtils.toString(jsonMap);
}
 
Example 18
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 4 votes vote down vote up
private void createAnnotationCreators(Integer annotationPk, Integer bodyPk, Integer targetPk, Map<String, Object> jsonMap) throws IOException {
    List<AnnotationAgent> annotationCreators = new AnnotationCreatorExtractor().extractCreators(jsonMap);
    for (AnnotationAgent annotationCreator : annotationCreators) {

        String creatorIri = annotationCreator.getAgentIri();
        String creatorJson = JsonUtils.toString(annotationCreator.getJsonMap());

        String[] types = annotationCreator.getTypes() != null ? annotationCreator.getTypes().stream().toArray(String[]::new) : new String[]{};
        String[] typesJson = new String[types.length];
        if (types.length > 0) {
            for (int i = 0; i < typesJson.length; i++) {
                typesJson[i] = JsonUtils.toString(annotationCreator.getTypesJsonList().get(i));
            }
        }

        String[] names = annotationCreator.getNames() != null ? annotationCreator.getNames().stream().toArray(String[]::new) : new String[]{};
        String[] namesJson = new String[names.length];
        if (names.length > 0) {
            for (int i = 0; i < namesJson.length; i++) {
                namesJson[i] = JsonUtils.toString(annotationCreator.getNameJsonMaps().get(i));
            }
        }

        String nickname = annotationCreator.getNickname();

        String[] emails = annotationCreator.getEmails() != null ? annotationCreator.getEmails().stream().toArray(String[]::new) : new String[]{};
        String[] emailsJson = new String[emails.length];
        if (emails.length > 0) {
            for (int i = 0; i < emailsJson.length; i++) {
                emailsJson[i] = JsonUtils.toString(annotationCreator.getEmailJsonMaps().get(i));
            }
        }

        String[] emailSha1s = annotationCreator.getEmailSha1s() != null ? annotationCreator.getEmailSha1s().stream().toArray(String[]::new) : new String[]{};
        String[] emailSha1sJson = new String[emailSha1s.length];
        if (emailSha1s.length > 0) {
            for (int i = 0; i < emailSha1sJson.length; i++) {
                emailSha1sJson[i] = JsonUtils.toString(annotationCreator.getEmailSha1JsonMaps().get(i));
            }
        }

        String[] homepages = annotationCreator.getHomepages() != null ? annotationCreator.getHomepages().stream().toArray(String[]::new) : new String[]{};
        String[] homepagesJson = new String[homepages.length];
        if (homepages.length > 0) {
            for (int i = 0; i < homepagesJson.length; i++) {
                homepagesJson[i] = JsonUtils.toString(annotationCreator.getHomepageJsonMaps().get(i));
            }
        }

        annotationAgentStoreRepository.createAnnotationCreator(annotationPk, bodyPk, targetPk, creatorIri, creatorJson, types, typesJson, names, namesJson, nickname, emails, emailsJson, emailSha1s, emailSha1sJson, homepages, homepagesJson);
    }
}
 
Example 19
Source File: AnnotationExtractorServiceImpl.java    From elucidate-server with MIT License 4 votes vote down vote up
private void createAnnotationGenerators(Integer annotationPk, Integer bodyPk, Integer targetPk, Map<String, Object> jsonMap) throws IOException {
    List<AnnotationAgent> annotationGenerators = new AnnotationGeneratorExtractor().extractGenerators(jsonMap);
    for (AnnotationAgent annotationGenerator : annotationGenerators) {

        String generatorIri = annotationGenerator.getAgentIri();
        String generatorJson = JsonUtils.toString(annotationGenerator.getJsonMap());

        String[] types = annotationGenerator.getTypes() != null ? annotationGenerator.getTypes().stream().toArray(String[]::new) : new String[]{};
        String[] typesJson = new String[types.length];
        if (types.length > 0) {
            for (int i = 0; i < typesJson.length; i++) {
                typesJson[i] = JsonUtils.toString(annotationGenerator.getTypesJsonList());
            }
        }

        String[] names = annotationGenerator.getNames() != null ? annotationGenerator.getNames().stream().toArray(String[]::new) : new String[]{};
        String[] namesJson = new String[names.length];
        if (names.length > 0) {
            for (int i = 0; i < namesJson.length; i++) {
                namesJson[i] = JsonUtils.toString(annotationGenerator.getNameJsonMaps().get(i));
            }
        }

        String nickname = annotationGenerator.getNickname();

        String[] emails = annotationGenerator.getEmails() != null ? annotationGenerator.getEmails().stream().toArray(String[]::new) : new String[]{};
        String[] emailsJson = new String[emails.length];
        if (emails.length > 0) {
            for (int i = 0; i < emailsJson.length; i++) {
                emailsJson[i] = JsonUtils.toString(annotationGenerator.getEmailJsonMaps().get(i));
            }
        }

        String[] emailSha1s = annotationGenerator.getEmailSha1s() != null ? annotationGenerator.getEmailSha1s().stream().toArray(String[]::new) : new String[]{};
        String[] emailSha1sJson = new String[emailSha1s.length];
        if (emailSha1s.length > 0) {
            for (int i = 0; i < emailSha1sJson.length; i++) {
                emailSha1sJson[i] = JsonUtils.toString(annotationGenerator.getEmailSha1JsonMaps().get(i));
            }
        }

        String[] homepages = annotationGenerator.getHomepages() != null ? annotationGenerator.getHomepages().stream().toArray(String[]::new) : new String[]{};
        String[] homepagesJson = new String[homepages.length];
        if (homepages.length > 0) {
            for (int i = 0; i < homepagesJson.length; i++) {
                homepagesJson[i] = JsonUtils.toString(annotationGenerator.getHomepageJsonMaps().get(i));
            }
        }

        annotationAgentStoreRepository.createAnnotationGenerator(annotationPk, bodyPk, targetPk, generatorIri, generatorJson, types, typesJson, names, namesJson, nickname, emails, emailsJson, emailSha1s, emailSha1sJson, homepages, homepagesJson);
    }
}
 
Example 20
Source File: AbstractAnnotationServiceImpl.java    From elucidate-server with MIT License 4 votes vote down vote up
@Override
public ServiceResponse<A> updateAnnotation(String collectionId, String annotationId, A annotation, String cacheKey) {

    ServiceResponse<A> existingAnnotationServiceResponse = getAnnotation(collectionId, annotationId);
    Status existingAnnotationStatus = existingAnnotationServiceResponse.getStatus();

    if (existingAnnotationStatus.equals(Status.NOT_FOUND)) {
        return new ServiceResponse<A>(Status.NOT_FOUND, null);
    }

    if (existingAnnotationStatus.equals(Status.DELETED)) {
        return new ServiceResponse<A>(Status.DELETED, null);
    }

    A existingAnnotation = existingAnnotationServiceResponse.getObj();

    if (!securityContext.isAuthorized(Permission.MANAGE, existingAnnotation)) {
        return new ServiceResponse<>(Status.UNAUTHORIZED, null);
    }

    String existingCacheKey = existingAnnotation.getCacheKey();
    if (!StringUtils.equals(cacheKey, existingCacheKey)) {
        return new ServiceResponse<A>(Status.CACHE_KEY_MISS, null);
    }

    Map<String, Object> annotationMap = annotation.getJsonMap();
    Map<String, Object> existingAnnotationMap = existingAnnotation.getJsonMap();

    if (!StringUtils.equals((String) annotationMap.get("via"), (String) existingAnnotationMap.get("via"))) {
        return new ServiceResponse<A>(Status.ILLEGAL_MODIFICATION, null);
    }

    if (!StringUtils.equals((String) annotationMap.get("canonical"), (String) existingAnnotationMap.get("canonical"))) {
        return new ServiceResponse<A>(Status.ILLEGAL_MODIFICATION, null);
    }

    W3CAnnotation w3cAnnotation = convertFromAnnotation(annotation);
    annotationMap = w3cAnnotation.getJsonMap();

    String annotationJson;
    try {
        annotationJson = JsonUtils.toString(annotationMap);
    } catch (IOException e) {
        LOGGER.debug(String.format("Detected invalid JSON in Annotation Map [%s]", annotationMap), e);
        return new ServiceResponse<A>(Status.NON_CONFORMANT, null);
    }
    w3cAnnotation = annotationStoreRepository.updateAnnotation(collectionId, annotationId, annotationJson);

    annotation = convertToAnnotation(w3cAnnotation);
    annotation.getJsonMap().put(JSONLDConstants.ATTRIBUTE_ID, buildAnnotationIri(collectionId, annotationId));
    return new ServiceResponse<A>(Status.OK, annotation);
}