use of org.devgateway.ocds.persistence.mongo.Identifiable in project oc-explorer by devgateway.
the class ReleaseCompilerService method mergeFieldStrategyArrayMergeById.
/**
* @param leftCollection
* @param rightCollection
* @return
* @see {@link MergeStrategy#arrayMergeById}
*/
@SuppressWarnings("unchecked")
protected <S extends Collection<Identifiable>> S mergeFieldStrategyArrayMergeById(final S leftCollection, final S rightCollection) {
// target collections must be instantiated
S target = null;
try {
target = (S) leftCollection.getClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
// we add all the left
target.addAll(leftCollection);
// iterate all right elements
for (Identifiable rightIdentifiable : rightCollection) {
// if there is an existing element with the same id, perform merge
// on the children and replace existing left element
Identifiable leftIdentifiable = getIdentifiableById(rightIdentifiable.getIdProperty(), leftCollection);
if (leftIdentifiable != null) {
target.remove(leftIdentifiable);
target.add(mergeOcdsBeans(leftIdentifiable, rightIdentifiable));
} else {
// otherwise add the new element to the left list
target.add(rightIdentifiable);
}
}
return target;
}
use of org.devgateway.ocds.persistence.mongo.Identifiable in project ocvn by devgateway.
the class ReleaseCompilerService method mergeFieldFromOcdsBeans.
/**
* Computes the output of an atomic merging operation on a specific field
*
* @param field the field to perform the merge on
* @param leftBean the left bean
* @param rightBean the right bean
* @return the merged result
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
protected <S> Object mergeFieldFromOcdsBeans(final Field field, final S leftBean, final S rightBean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object rightFieldValue = PropertyUtils.getProperty(rightBean, field.getName());
Object leftFieldValue = PropertyUtils.getProperty(leftBean, field.getName());
if (fieldsAnnotatedWithMerge.contains(field)) {
MergeStrategy mergeStrategy = field.getDeclaredAnnotation(Merge.class).value();
switch(mergeStrategy) {
case overwrite:
return mergeFieldStrategyOverwrite(leftFieldValue, rightFieldValue);
case ocdsOmit:
return mergeFieldStrategyOcdsOmit(leftFieldValue, rightFieldValue);
case ocdsVersion:
return mergeFieldStrategyOcdsVersion(leftFieldValue, rightFieldValue);
case arrayMergeById:
return mergeFieldStrategyArrayMergeById((Collection<Identifiable>) leftFieldValue, (Collection<Identifiable>) rightFieldValue);
default:
throw new RuntimeException("Unknown or unimplemented merge strategy!");
}
} else {
// recursively invoke the method on the field value
return mergeOcdsBeans(leftFieldValue, rightFieldValue);
}
}
use of org.devgateway.ocds.persistence.mongo.Identifiable in project ocvn by devgateway.
the class ReleaseCompilerService method mergeFieldStrategyArrayMergeById.
/**
* @param leftCollection
* @param rightCollection
* @return
* @see {@link MergeStrategy#arrayMergeById}
*/
@SuppressWarnings("unchecked")
protected <S extends Collection<Identifiable>> S mergeFieldStrategyArrayMergeById(final S leftCollection, final S rightCollection) {
// target collections must be instantiated
S target = null;
try {
target = (S) leftCollection.getClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
// we add all the left
target.addAll(leftCollection);
// iterate all right elements
for (Identifiable rightIdentifiable : rightCollection) {
// if there is an existing element with the same id, perform merge
// on the children and replace existing left element
Identifiable leftIdentifiable = getIdentifiableById(rightIdentifiable.getIdProperty(), leftCollection);
if (leftIdentifiable != null) {
target.remove(leftIdentifiable);
target.add(mergeOcdsBeans(leftIdentifiable, rightIdentifiable));
} else {
// otherwise add the new element to the left list
target.add(rightIdentifiable);
}
}
return target;
}
use of org.devgateway.ocds.persistence.mongo.Identifiable in project oc-explorer by devgateway.
the class ReleaseCompilerService method mergeFieldFromOcdsBeans.
/**
* Computes the output of an atomic merging operation on a specific field
*
* @param field the field to perform the merge on
* @param leftBean the left bean
* @param rightBean the right bean
* @return the merged result
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
protected <S> Object mergeFieldFromOcdsBeans(final Field field, final S leftBean, final S rightBean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object rightFieldValue = PropertyUtils.getProperty(rightBean, field.getName());
Object leftFieldValue = PropertyUtils.getProperty(leftBean, field.getName());
if (fieldsAnnotatedWithMerge.contains(field)) {
MergeStrategy mergeStrategy = field.getDeclaredAnnotation(Merge.class).value();
switch(mergeStrategy) {
case overwrite:
return mergeFieldStrategyOverwrite(leftFieldValue, rightFieldValue);
case ocdsOmit:
return mergeFieldStrategyOcdsOmit(leftFieldValue, rightFieldValue);
case ocdsVersion:
return mergeFieldStrategyOcdsVersion(leftFieldValue, rightFieldValue);
case arrayMergeById:
return mergeFieldStrategyArrayMergeById((Collection<Identifiable>) leftFieldValue, (Collection<Identifiable>) rightFieldValue);
default:
throw new RuntimeException("Unknown or unimplemented merge strategy!");
}
} else {
// recursively invoke the method on the field value
return mergeOcdsBeans(leftFieldValue, rightFieldValue);
}
}
Aggregations