Search in sources :

Example 1 with Jsonb

use of com.evolveum.midpoint.repo.sqale.jsonb.Jsonb in project midpoint by Evolveum.

the class QShadowMapping method addIndexOnlyAttributes.

private void addIndexOnlyAttributes(ShadowType shadowType, Tuple row, QShadow entityPath, List<SelectorOptions<GetOperationOptions>> retrieveOptions) throws SchemaException {
    Jsonb rowAttributes = row.get(entityPath.attributes);
    if (rowAttributes == null) {
        return;
    }
    Map<String, Object> attributes = Jsonb.toMap(rowAttributes);
    if (attributes.isEmpty()) {
        return;
    }
    ShadowAttributesType attributeContainer = shadowType.getAttributes();
    if (attributeContainer == null) {
        attributeContainer = new ShadowAttributesType(prismContext());
        shadowType.attributes(attributeContainer);
    }
    // noinspection unchecked
    PrismContainerValue<ShadowAttributesType> container = attributeContainer.asPrismContainerValue();
    // Now we retrieve indexOnly options
    for (Entry<String, Object> attribute : attributes.entrySet()) {
        @Nullable MExtItem mapping = repositoryContext().getExtensionItem(Integer.valueOf(attribute.getKey()));
        QName itemName = QNameUtil.uriToQName(mapping.itemName);
        ItemDefinition<?> definition = definitionFrom(itemName, mapping, true);
        if (definition instanceof PrismPropertyDefinition) {
            var item = container.findOrCreateProperty((PrismPropertyDefinition) definition);
            switch(mapping.cardinality) {
                case SCALAR:
                    item.setRealValue(attribute.getValue());
                    break;
                case ARRAY:
                    List<?> value = (List<?>) attribute.getValue();
                    item.setRealValues(value.toArray());
                    break;
                default:
                    throw new IllegalStateException("");
            }
            if (item.isIncomplete() && (item.getDefinition() == null || !item.getDefinition().isIndexOnly())) {
                // Item was not fully serialized / probably indexOnly item
                item.applyDefinition((PrismPropertyDefinition) definition);
            }
            item.setIncomplete(false);
        }
    }
}
Also used : QName(javax.xml.namespace.QName) ShadowAttributesType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAttributesType) Jsonb(com.evolveum.midpoint.repo.sqale.jsonb.Jsonb) MExtItem(com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Jsonb

use of com.evolveum.midpoint.repo.sqale.jsonb.Jsonb in project midpoint by Evolveum.

the class SqaleRepoSmokeTest method test900WorkingWithPgArraysJsonbAndBytea.

// region low-level tests
/**
 * This tests our type mapper/converter classes and related column mapping.
 */
@Test
public void test900WorkingWithPgArraysJsonbAndBytea() {
    QUser u = aliasFor(QUser.class);
    MUser user = new MUser();
    String userName = "user" + getTestNumber();
    setName(user, userName);
    user.policySituations = new Integer[] { 1, 2 };
    user.subtypes = new String[] { "subtype1", "subtype2" };
    // more whitespaces/lines
    user.ext = new Jsonb("{\"key\" : \"value\",\n\"number\": 47} ");
    user.photo = new byte[] { 0, 1, 0, 1 };
    try (JdbcSession jdbcSession = startTransaction()) {
        jdbcSession.newInsert(u).populate(user).execute();
        jdbcSession.commit();
    }
    MUser row = selectOne(u, u.nameNorm.eq(userName));
    assertThat(row.policySituations).contains(1, 2);
    assertThat(row.subtypes).contains("subtype1", "subtype2");
    // normalized
    assertThat(row.ext.value).isEqualTo("{\"key\": \"value\", \"number\": 47}");
    // byte[] is used for fullObject, there is no chance to miss a problem with it
    assertThat(row.photo).hasSize(4);
    // setting NULLs
    try (JdbcSession jdbcSession = startTransaction()) {
        jdbcSession.newUpdate(u).setNull(u.policySituations).set(u.subtypes, // this should do the same
        (String[]) null).setNull(u.ext).setNull(u.photo).where(u.oid.eq(row.oid)).execute();
        jdbcSession.commit();
    }
    row = selectOne(u, u.nameNorm.eq(userName));
    assertThat(row.policySituations).isNull();
    assertThat(row.subtypes).isNull();
    assertThat(row.ext).isNull();
    // but we never set fullObject to null, so this is a good test for doing so with byte[]
    assertThat(row.photo).isNull();
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) QUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser) Jsonb(com.evolveum.midpoint.repo.sqale.jsonb.Jsonb) MUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser) Test(org.testng.annotations.Test) SqaleRepoBaseTest(com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)

Example 3 with Jsonb

use of com.evolveum.midpoint.repo.sqale.jsonb.Jsonb in project midpoint by Evolveum.

the class QShadowMapping method toSchemaObject.

@Override
public ShadowType toSchemaObject(Tuple row, QShadow entityPath, Collection<SelectorOptions<GetOperationOptions>> options) throws SchemaException {
    ShadowType shadowType = super.toSchemaObject(row, entityPath, options);
    // FIXME: we store it because provisioning now sends it to repo, but it should be transient
    shadowType.asPrismObject().removeContainer(ShadowType.F_ASSOCIATION);
    GetOperationOptions rootOptions = SelectorOptions.findRootOptions(options);
    if (GetOperationOptions.isRaw(rootOptions)) {
        // If raw=true, we populate attributes with types cached in repository
        Jsonb rowAttributes = row.get(entityPath.attributes);
        applyShadowAttributesDefinitions(shadowType, rowAttributes);
    }
    List<SelectorOptions<GetOperationOptions>> retrieveOptions = SelectorOptions.filterRetrieveOptions(options);
    if (retrieveOptions.isEmpty()) {
        return shadowType;
    }
    if (loadIndexOnly(retrieveOptions)) {
        addIndexOnlyAttributes(shadowType, row, entityPath, retrieveOptions);
    }
    return shadowType;
}
Also used : GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) Jsonb(com.evolveum.midpoint.repo.sqale.jsonb.Jsonb) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)

Aggregations

Jsonb (com.evolveum.midpoint.repo.sqale.jsonb.Jsonb)3 SqaleRepoBaseTest (com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)1 MExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem)1 MUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser)1 QUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser)1 JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)1 GetOperationOptions (com.evolveum.midpoint.schema.GetOperationOptions)1 SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)1 ShadowAttributesType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAttributesType)1 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)1 QName (javax.xml.namespace.QName)1 Nullable (org.jetbrains.annotations.Nullable)1 Test (org.testng.annotations.Test)1