{
if (elements == null || elements.size() == 0) {
return null;
}
ExecutionContext ec = op.getExecutionContext();
Iterator iter = elements.iterator();
while (iter.hasNext()) {
validateElementForReading(ec, iter.next());
}
// Since the datastore doesn't support 'or', we're going to sort the keys in memory.
// issue an ancestor query that fetches all children between the first key and the last,
// and then build the array of indices from there. The query may return entities that are
// not in the elements so we have to be careful.
if (elements.isEmpty()) {
return new int[0];
}
if (MetaDataUtils.readRelatedKeysFromParent(storeMgr, ownerMemberMetaData)) {
// Obtain via field of List<Key> in parent
String propName = EntityUtils.getPropertyName(storeMgr.getIdentifierFactory(), ownerMemberMetaData);
Entity ownerEntity = getOwnerEntity(op);
if (ownerEntity.hasProperty(propName)) {
Object value = ownerEntity.getProperty(propName);
if (value == null) {
return new int[0];
}
// Convert elements into list of keys to search for
List<Key> keys = (List<Key>) value;
Set<Key> elementKeys = Utils.newHashSet();
for (Object element : elements) {
Key key = EntityUtils.getKeyForObject(element, ec);
if (key != null) {
elementKeys.add(key);
}
}
// Generate indices list for these elements
int i = 0;
List<Integer> indicesList = new ArrayList<Integer>();
for (Key key : keys) {
if (elementKeys.contains(key)) {
indicesList.add(i);
}
i++;
}
int[] indices = new int[indicesList.size()];
i = 0;
for (Integer index : indicesList) {
indices[i++] = index;
}
return indices;
}
} else {
// Owned relation in earlier storage version so use parentKey to determine membership of list
List<Key> keys = Utils.newArrayList();
Set<Key> keySet = Utils.newHashSet();
for (Object ele : elements) {
ApiAdapter apiAdapter = ec.getApiAdapter();
Object keyOrString =
apiAdapter.getTargetKeyForSingleFieldIdentity(apiAdapter.getIdForObject(ele));
Key key = keyOrString instanceof Key ? (Key) keyOrString : KeyFactory.stringToKey((String) keyOrString);
if (key == null) {
throw new NucleusUserException("Collection element does not have a primary key.");