Skip to content

Commit

Permalink
fix: fix RDBS export entry and DeleteNotifier (#864)
Browse files Browse the repository at this point in the history
Co-authored-by: yurem <Yuriy.Movchan@gmail.com>
  • Loading branch information
yuremm and yurem authored Feb 18, 2022
1 parent dfce3e2 commit ce5b2e6
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ <T> Map<T, List<T>> groupListByProperties(Class<T> entryClass, List<T> entries,

<T> void sortListByProperties(Class<T> entryClass, List<T> entries, boolean caseSensetive, String... sortByProperties);

@Deprecated
List<AttributeData> exportEntry(String dn);

List<AttributeData> exportEntry(String dn, String objectClass);

<T> void importEntry(String dn, Class<T> entryClass, List<AttributeData> data);

PersistenceOperationService getOperationService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

public interface DeleteNotifier {

void onBeforeRemove(String dn);
void onBeforeRemove(String dn, String[] objectClasses);

void onAfterRemove(String dn);
void onAfterRemove(String dn, String[] objectClasses);

}
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ protected <T> void removeByDn(String dn, String[] objectClasses) {
// Remove entry
try {
for (DeleteNotifier subscriber : subscribers) {
subscriber.onBeforeRemove(dn);
subscriber.onBeforeRemove(dn, objectClasses);
}
getOperationService().delete(toCouchbaseKey(dn).getKey());
for (DeleteNotifier subscriber : subscribers) {
subscriber.onAfterRemove(dn);
subscriber.onAfterRemove(dn, objectClasses);
}
} catch (Exception ex) {
throw new EntryDeleteException(String.format("Failed to remove entry: %s", dn), ex);
Expand All @@ -296,11 +296,11 @@ protected <T> void removeByDn(String dn, String[] objectClasses) {
public <T> void removeRecursivelyFromDn(String dn, String[] objectClasses) {
try {
for (DeleteNotifier subscriber : subscribers) {
subscriber.onBeforeRemove(dn);
subscriber.onBeforeRemove(dn, objectClasses);
}
getOperationService().deleteRecursively(toCouchbaseKey(dn).getKey());
for (DeleteNotifier subscriber : subscribers) {
subscriber.onAfterRemove(dn);
subscriber.onAfterRemove(dn, objectClasses);
}
} catch (Exception ex) {
throw new EntryDeleteException(String.format("Failed to remove entry: %s", dn), ex);
Expand Down Expand Up @@ -799,6 +799,10 @@ public List<AttributeData> exportEntry(String dn) {
}
}

public List<AttributeData> exportEntry(String dn, String objectClass) {
return exportEntry(dn);
}

private ConvertedExpression toCouchbaseFilter(Filter genericFilter, Map<String, PropertyAnnotation> propertiesAnnotationsMap) throws SearchException {
return FILTER_CONVERTER.convertToCouchbaseFilter(genericFilter, propertiesAnnotationsMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ public List<AttributeData> exportEntry(String dn) {
return persistenceEntryManager.exportEntry(dn);
}

@Override
public List<AttributeData> exportEntry(String dn, String objectClass) {
PersistenceEntryManager persistenceEntryManager = getEntryManagerForDn(dn);
return persistenceEntryManager.exportEntry(dn, objectClass);
}

@Override
public <T> void importEntry(String dn, Class<T> entryClass, List<AttributeData> data) {
throw new UnsupportedOperationException("Method not implemented.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,11 @@ public <T> void removeByDn(String dn, String[] objectClasses) {
// Remove entry
try {
for (DeleteNotifier subscriber : subscribers) {
subscriber.onBeforeRemove(dn);
subscriber.onBeforeRemove(dn, objectClasses);
}
getOperationService().delete(dn);
for (DeleteNotifier subscriber : subscribers) {
subscriber.onAfterRemove(dn);
subscriber.onAfterRemove(dn, objectClasses);
}
} catch (Exception ex) {
throw new EntryDeleteException(String.format("Failed to remove entry: %s", dn), ex);
Expand Down Expand Up @@ -351,11 +351,11 @@ public <T> void removeRecursivelyFromDn(String dn, String[] objectClasses) {
try {
if (getOperationService().getConnectionProvider().isSupportsSubtreeDeleteRequestControl()) {
for (DeleteNotifier subscriber : subscribers) {
subscriber.onBeforeRemove(dn);
subscriber.onBeforeRemove(dn, objectClasses);
}
getOperationService().deleteRecursively(dn);
for (DeleteNotifier subscriber : subscribers) {
subscriber.onAfterRemove(dn);
subscriber.onAfterRemove(dn, objectClasses);
}
} else {
removeSubtreeThroughIteration(dn);
Expand Down Expand Up @@ -881,6 +881,11 @@ public List<AttributeData> exportEntry(String dn) {
}
}

@Override
public List<AttributeData> exportEntry(String dn, String objectClass) {
return exportEntry(dn);
}

public int getSupportedLDAPVersion() {
return getOperationService().getSupportedLDAPVersion();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,11 @@ protected <T> void removeByDn(String dn, String[] objectClasses) {
// Remove entry
try {
for (DeleteNotifier subscriber : subscribers) {
subscriber.onBeforeRemove(dn);
subscriber.onBeforeRemove(dn, objectClasses);
}
getOperationService().delete(toSQLKey(dn).getKey(), objectClasses[0]);
for (DeleteNotifier subscriber : subscribers) {
subscriber.onAfterRemove(dn);
subscriber.onAfterRemove(dn, objectClasses);
}
} catch (Exception ex) {
throw new EntryDeleteException(String.format("Failed to remove entry: '%s'", dn), ex);
Expand All @@ -308,15 +308,13 @@ protected <T> void removeRecursivelyFromDn(String dn, String[] objectClasses) {
throw new UnsupportedOperationException("Entry class is manadatory for recursive remove operation!");
}

removeByDn(dn, objectClasses);

try {
for (DeleteNotifier subscriber : subscribers) {
subscriber.onBeforeRemove(dn);
subscriber.onBeforeRemove(dn, objectClasses);
}
getOperationService().deleteRecursively(toSQLKey(dn).getKey(), objectClasses[0]);
for (DeleteNotifier subscriber : subscribers) {
subscriber.onAfterRemove(dn);
subscriber.onAfterRemove(dn, objectClasses);
}
} catch (Exception ex) {
throw new EntryDeleteException(String.format("Failed to remove entry: '%s'", dn), ex);
Expand Down Expand Up @@ -772,10 +770,19 @@ protected <T> Sort[] getDefaultSort(Class<T> entryClass) {

@Override
public List<AttributeData> exportEntry(String dn) {
try {
throw new EntryPersistenceException("This is deprectated method. Use exportEntry(String dn, Class<T> entryClass) instead of it!");
}

@Override
public List<AttributeData> exportEntry(String dn, String objectClass) {
if (StringHelper.isEmpty(objectClass)) {
throw new MappingException("Object class isn't defined!");
}

try {
// Load entry
ParsedKey keyWithInum = toSQLKey(dn);
List<AttributeData> entry = getOperationService().lookup(keyWithInum.getKey(), null);
List<AttributeData> entry = getOperationService().lookup(keyWithInum.getKey(), objectClass);

if (entry != null) {
return entry;
Expand All @@ -786,7 +793,6 @@ public List<AttributeData> exportEntry(String dn) {
throw new EntryPersistenceException(String.format("Failed to find entry: '%s'", dn), ex);
}
}


private ConvertedExpression toSqlFilter(String key, String objectClass, Filter genericFilter, Map<String, PropertyAnnotation> propertiesAnnotationsMap) throws SearchException {
TableMapping tableMapping = getOperationService().getTabeMapping(key, objectClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.jans.orm.sql;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.jans.orm.search.filter.Filter;
import io.jans.orm.sql.impl.SqlEntryManager;
import io.jans.orm.sql.model.SimpleClient;
import io.jans.orm.sql.operation.impl.SqlConnectionProvider;
import io.jans.orm.sql.persistence.SqlEntryManagerSample;
import io.jans.orm.util.ArrayHelper;

/**
* @author Yuriy Movchan Date: 05/26/2021
*/
public class SqlSimpleClientSample {

private static final Logger LOG = LoggerFactory.getLogger(SqlConnectionProvider.class);

private SqlSimpleClientSample() {
}

public static void main(String[] args) {
// Prepare sample connection details
SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();

// Create SQL entry manager
SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();

SimpleClient newClient = new SimpleClient();
newClient.setDn("inum=test_clnt2,ou=client,o=gluu");
newClient.setDefaultAcrValues(new String[] { "test_clnt2_acr" });
newClient.setClientName("test_clnt2");

sqlEntryManager.persist(newClient);

Filter presenceFilter = Filter.createEqualityFilter("displayName", "test_clnt2");
List<SimpleClient> results = sqlEntryManager.findEntries("ou=test_clnt2,o=gluu", SimpleClient.class,
presenceFilter);
for (SimpleClient client : results) {
String[] acrs = client.getDefaultAcrValues();
if (ArrayHelper.isNotEmpty(acrs)) {
System.out.println(Arrays.toString(acrs));
}
}

sqlEntryManager.remove(newClient.getDn(), SimpleClient.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.jans.orm.sql;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.jans.orm.event.DeleteNotifier;
import io.jans.orm.search.filter.Filter;
import io.jans.orm.sql.impl.SqlEntryManager;
import io.jans.orm.sql.model.SimpleClient;
import io.jans.orm.sql.operation.impl.SqlConnectionProvider;
import io.jans.orm.sql.persistence.SqlEntryManagerSample;
import io.jans.orm.util.ArrayHelper;

/**
* @author Yuriy Movchan Date: 05/26/2021
*/
public class SqlSimpleClientWithDeleteNotifierSample {

private static final Logger LOG = LoggerFactory.getLogger(SqlConnectionProvider.class);

private SqlSimpleClientWithDeleteNotifierSample() {
}

public static void main(String[] args) {
// Prepare sample connection details
SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();

// Create SQL entry manager
SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();

sqlEntryManager.addDeleteSubscriber(new DeleteNotifier() {
@Override
public void onBeforeRemove(String dn, String[] objectClasses) {
System.out.println(Arrays.asList(objectClasses));
System.out.println(sqlEntryManager.exportEntry(dn, objectClasses[0]));
}

@Override
public void onAfterRemove(String dn, String[] objectClasses) {
System.out.println(Arrays.asList(objectClasses));
}
});

SimpleClient newClient = new SimpleClient();
newClient.setDn("inum=test_clnt3,ou=client,o=gluu");
newClient.setDefaultAcrValues(new String[] { "test_clnt3_acr" });
newClient.setClientName("test_clnt3");

sqlEntryManager.persist(newClient);

Filter presenceFilter = Filter.createEqualityFilter("displayName", "test_clnt3");
List<SimpleClient> results = sqlEntryManager.findEntries("ou=test_clnt3,o=gluu", SimpleClient.class,
presenceFilter);
for (SimpleClient client : results) {
String[] acrs = client.getDefaultAcrValues();
if (ArrayHelper.isNotEmpty(acrs)) {
System.out.println(Arrays.toString(acrs));
}
}

sqlEntryManager.remove(newClient.getDn(), SimpleClient.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.List;

import io.jans.orm.annotation.AttributeName;
import io.jans.orm.annotation.AttributesList;
import io.jans.orm.annotation.CustomObjectClass;
import io.jans.orm.annotation.DN;
Expand All @@ -30,6 +31,12 @@ public class SimpleClient implements Serializable {
@DN
private String dn;

@AttributeName(name = "displayName")
private String clientName;

@AttributeName(name = "jansDefAcrValues")
private String[] defaultAcrValues;

@AttributesList(name = "name", value = "values", sortByName = true)
private List<CustomAttribute> customAttributes = new ArrayList<CustomAttribute>();

Expand All @@ -44,7 +51,23 @@ public void setDn(String dn) {
this.dn = dn;
}

public List<CustomAttribute> getCustomAttributes() {
public String getClientName() {
return clientName;
}

public void setClientName(String clientName) {
this.clientName = clientName;
}

public String[] getDefaultAcrValues() {
return defaultAcrValues;
}

public void setDefaultAcrValues(String[] defaultAcrValues) {
this.defaultAcrValues = defaultAcrValues;
}

public List<CustomAttribute> getCustomAttributes() {
return customAttributes;
}

Expand Down
Loading

0 comments on commit ce5b2e6

Please sign in to comment.