Full example for using ContentPropertyService to store JsonContentProperty with page

Hi,
I am really struggling with using the ContentPropertyService for storing a JsonContentProperty with a page. Is there any example for writing and loading of a JsonContentProperty?
Thanks
Tuelle

2 Likes

Hi @ttwellmann,

I’m not sure if this will help, but I’ve searched thru our code base and this is a sample read and write:

    @Test
    public void testSimpleCreateRead() throws ServiceException, IOException {
        long pageId = TEST_PAGE.getId();
        ContentId apiPageId = ContentId.of(PAGE, pageId);
        String key = "my-super-key";
        JsonString value = makeTestString("I am saved");
        JsonContentProperty newProperty = JsonContentProperty.builder()
                .content(Reference.to(Content.builder().id(apiPageId).build()))
                .key(key)
                .value(value)
                .build();
        JsonContentProperty savedProperty = service.create(newProperty);
        assertThat(savedProperty.getContent(), nullValue());
        assertThat(savedProperty.getKey(), is(key));
        assertThat(savedProperty.getValue(), is(value));

        Option<JsonContentProperty> foundPropertyOpt = service.find().withContentId(apiPageId).withPropertyKey(key).fetchOne();
        assertThat(foundPropertyOpt.isDefined(), is(true));
        JsonContentProperty foundProperty = foundPropertyOpt.get();
        assertThat(foundProperty.getId(), is(savedProperty.getId()));
        assertThat(foundProperty.getContentRef().exists(), is(false));
        assertThat(foundProperty.getKey(), is(key));
        assertThat(foundProperty.getValue(), is(value));
        assertThat(foundProperty.getVersion(), is(savedProperty.getVersion()));
    }

Cheers,
Anne Calantog

2 Likes

Is there any way to engage ContentPropertyService for setting and getting a JsonContentProperty in a user macro (using velocity)?

2 Likes

I really would love to know how to use ContentPropertyService for storing a JsonContentProperty with a page in a user macro.

Any ideas? Thx!

Thanks for this example. May you please provide details about method “makeTestString” which builds the JsonString object? Thank you in advance. Regards. Yves

Late to the party but I’ve been struggling with this for some time and after some trail and error I think I manage to get this to work in a macro plugin I’m building. Hope to help future me and others. (Not a Java developer but I hope this will suffice)

import com.atlassian.confluence.api.service.content.ContentPropertyService;

//...

private final ContentPropertyService contentPropertyService;
private final ContentService contentService;

public MyMacro(
        ContentpropertyService contentPropertyService,
        ContentService contentService){
        this.contentPropertyService = contentPropertyService;
        this.contentService = contentService;
}


public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {

// Create the fetcher
ContentPropertyService.ContentPropertyFinder fetcher = contentPropertyService.find(
        new Expansion(JsonContentProperty.Expansions.CONTENT),
        new Expansion(JsonContentProperty.Expansions.VERSION)
);

// Get the content ID or pass a Long contentId and do a ContentId.of(pageId)
ContentId contentId = conversionContext.getEntity().getContentId();

String jsonKey = "my_propertykey"; // Dashes "-" will automatically become underscore fyi.

JsonContentProperty res = fetcher
        .withContentId(contentId)
        .withPropertyKey(jsonKey)
        .fetchOrNull();

/* 
Done, you now have your content property. You can now use Gson to convert it to a class.
Gson gson = new Gson();
String propertyString = res.getValue().getValue(); 
MyPropertyClass mydata = gson.fromJson(revisionDataStr, MyPropertyClass.class);

*/


// Below will continue on how to create or update it in the storage
Version.VersionBuilder versionBuilder = Version.builder();

if ( res != null){
    int currentVersion = res.getVersion().getNumber();
    versionBuilder.number(currentVersion + 1);
}
else{
    versionBuilder.number(1);
}

// Fetch the content object that should be passed as a reference
ContentService.ParameterContentFinder fetcher = contentService.find(
                new Expansion(Content.Expansions.SPACE),
                new Expansion(Content.Expansions.VERSION));

Content content = fetcher.withId(contentId).fetchOrNull();

/*
Create the reference.
I am not entierly sure if it would suffice to just just create a content and add the ID. The Java API wasn't clear on the usage. Here's an example:
Content content = Content.builder().id(contentId).build());
*/

JsonString value = new JsonString("\"test\":\"testvalue\"");
JsonContentProperty newProperty =
        JsonContentProperty
            .builder()
            .version(versionBuilder.build())
            .content(Reference.to(content))
            .key(jsonKey)
            .value(value)
            .build();

ContentPropertyService.Validator validator = contentPropertyService.validator();
ValidationResult validCreate = validator.validateCreate(newProperty);
ValidationResult validUpdate = validator.validateUpdate(newProperty);

JsonContentProperty savedProperty = null;
if (res == null&& validCreate.isValid()) {
    savedProperty = contentPropertyService.create(newProperty);
}else if(res != null && validUpdate.isValid())
{
    savedProperty = contentPropertyService.update(newProperty);
}

if (savedProperty != null) {
    return savedProperty.toString();
}
1 Like