@SuppressWarnings("unchecked") static synchronized DataStore<?> getInstance(final Class<?> clz, final boolean createNewInstance) throws DataStoreException { DataStore<?> ds = c2ds.get(clz); if (createNewInstance || ds == null) { ds = new DataStore<Object>((Class<Object>) clz); c2ds.put(clz, ds); } return ds; } }
private T create(final T object, final KeyElement keyElement) throws DataStoreException { synchronized (dataStore) { if (keyElement.keyValuesMissing() || dataStore.containsKey(keyElement)) { KeyElement newKey = createSetAndGetKeys(object); return this.create(object, newKey); } dataStore.put(keyElement, object); } return object; }
@Override public void deleteData(final EdmEntitySet entitySet, final Map<String, Object> keys) throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException { DataStore<Object> dataStore = getDataStore(entitySet); Object keyInstance = dataStore.createInstance(); ANNOTATION_HELPER.setKeyFields(keyInstance, keys); dataStore.delete(keyInstance); }
@Override public void writeRelation(final EdmEntitySet sourceEntitySet, final Object sourceEntity, final EdmEntitySet targetEntitySet, final Map<String, Object> targetEntityValues) throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException { // get common data DataStore<Object> sourceStore = dataStores.get(sourceEntitySet.getName()); DataStore<Object> targetStore = dataStores.get(targetEntitySet.getName()); AnnotatedNavInfo commonNavInfo = ANNOTATION_HELPER.getCommonNavigationInfo( sourceStore.getDataTypeClass(), targetStore.getDataTypeClass()); // get and validate source fields Field sourceField = commonNavInfo.getFromField(); if (sourceField == null) { throw new AnnotationRuntimeException("Missing source field for related data (sourceStore='" + sourceStore + "', targetStore='" + targetStore + "')."); } // get related target entity Object targetEntity = targetStore.createInstance(); ANNOTATION_HELPER.setKeyFields(targetEntity, targetEntityValues); targetEntity = targetStore.read(targetEntity); // set at source setValueAtNavigationField(sourceEntity, sourceField, targetEntity); // set at target Field targetField = commonNavInfo.getToField(); if (targetField != null) { setValueAtNavigationField(targetEntity, targetField, sourceEntity); } }
public T delete(final T object) { KeyElement keyElement = getKeys(object); synchronized (dataStore) { return dataStore.remove(keyElement); } }
@Override public Object readData(final EdmEntitySet entitySet, final Map<String, Object> keys) throws ODataNotFoundException, EdmException, ODataApplicationException { DataStore<Object> store = getDataStore(entitySet); if (store != null) { Object keyInstance = store.createInstance(); ANNOTATION_HELPER.setKeyFields(keyInstance, keys); Object result = store.read(keyInstance); if (result != null) { return result; } } throw new ODataNotFoundException(ODataNotFoundException.ENTITY); }
/** * Read the result data from the target store based on <code>sourceData</code> and <code>sourceField</code> * * @param targetStore * @param sourceData * @param sourceField * @return * @throws DataStoreException */ private List<Object> readResultData(final DataStore<?> targetStore, final Object sourceData, final Field sourceField) throws DataStoreException { Object navigationInstance = getValue(sourceField, sourceData); if (navigationInstance == null) { return Collections.emptyList(); } List<Object> resultData = new ArrayList<Object>(); for (Object targetInstance : targetStore.read()) { if (navigationInstance instanceof Collection) { for (Object object : (Collection<?>) navigationInstance) { if (targetStore.isKeyEqualChecked(targetInstance, object)) { resultData.add(targetInstance); } } } else if (targetStore.isKeyEqualChecked(targetInstance, navigationInstance)) { resultData.add(targetInstance); } } return resultData; }
return resultData; } else { Object keyInstance = targetStore.createInstance(); ANNOTATION_HELPER.setKeyFields(keyInstance, targetKeys); for (Object result : resultData) { if (targetStore.isKeyEqualChecked(result, keyInstance)) { return result;
@Override public void createData(final EdmEntitySet entitySet, final Object data) throws ODataNotImplementedException, EdmException, ODataApplicationException { DataStore<Object> dataStore = getDataStore(entitySet); dataStore.create(data); }
@Override public Object newDataObject(final EdmEntitySet entitySet) throws ODataNotImplementedException, EdmException, ODataApplicationException { DataStore<Object> dataStore = getDataStore(entitySet); if (dataStore != null) { return dataStore.createInstance(); } throw new ODataRuntimeException("No DataStore found for entitySet with name: " + entitySet.getName()); }
@SuppressWarnings("unchecked") private void init(final Collection<Class<?>> annotatedClasses) throws ODataException { try { for (Class<?> clz : annotatedClasses) { String entitySetName = ANNOTATION_HELPER.extractEntitySetName(clz); if (entitySetName != null) { DataStore<Object> dhs = (DataStore<Object>) DataStore.createInMemory(clz, persistInMemory); dataStores.put(entitySetName, dhs); } else if (!ANNOTATION_HELPER.isEdmAnnotated(clz)) { throw new ODataException("Found not annotated class during DataStore initilization of type: " + clz.getName()); } } } catch (DataStore.DataStoreException e) { throw new ODataException("Error in DataStore initilization with message: " + e.getMessage(), e); } }
@Override public Object readRelatedData(final EdmEntitySet sourceEntitySet, final Object sourceData, final EdmEntitySet targetEntitySet, final Map<String, Object> targetKeys) throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException { DataStore<?> sourceStore = dataStores.get(sourceEntitySet.getName()); DataStore<?> targetStore = dataStores.get(targetEntitySet.getName()); AnnotatedNavInfo navInfo = ANNOTATION_HELPER.getCommonNavigationInfo( sourceStore.getDataTypeClass(), targetStore.getDataTypeClass()); Field sourceField = navInfo.getFromField(); if (sourceField == null) { throw new ODataRuntimeException("Missing source field for related data (sourceStore='" + sourceStore + "', targetStore='" + targetStore + "')."); } List<Object> resultData = readResultData(targetStore, sourceData, sourceField); return extractResultData(targetStore, targetKeys, navInfo, resultData); }
/** * Are the key values equal for both instances. * If all compared key values are <code>null</code> this also means equal. * Before object (keys) are compared it is validated that both object instance are NOT null * and that both are from the same class as this {@link DataStore} (see {@link #dataTypeClass}). * For the equal check on {@link #dataTypeClass} instances without validation see {@link #isKeyEqual(Object, Object)}. * * @param first first instance to check for key equal * @param second second instance to check for key equal * @return <code>true</code> if object instance have equal keys set. */ @SuppressWarnings("unchecked") public boolean isKeyEqualChecked(final Object first, final Object second) throws DataStoreException { if (first == null || second == null) { throw new DataStoreException("Tried to compare null values which is not allowed."); } else if (first.getClass() != dataTypeClass) { throw new DataStoreException("First value is no instance from required class '" + dataTypeClass + "'."); } else if (second.getClass() != dataTypeClass) { throw new DataStoreException("Second value is no instance from required class '" + dataTypeClass + "'."); } return isKeyEqual((T) first, (T) second); }
@Override public void writeRelation(final EdmEntitySet sourceEntitySet, final Object sourceEntity, final EdmEntitySet targetEntitySet, final Map<String, Object> targetEntityValues) throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException { // get common data DataStore<Object> sourceStore = dataStores.get(sourceEntitySet.getName()); DataStore<Object> targetStore = dataStores.get(targetEntitySet.getName()); AnnotatedNavInfo commonNavInfo = ANNOTATION_HELPER.getCommonNavigationInfo( sourceStore.getDataTypeClass(), targetStore.getDataTypeClass()); // get and validate source fields Field sourceField = commonNavInfo.getFromField(); if (sourceField == null) { throw new AnnotationRuntimeException("Missing source field for related data (sourceStore='" + sourceStore + "', targetStore='" + targetStore + "')."); } // get related target entity Object targetEntity = targetStore.createInstance(); ANNOTATION_HELPER.setKeyFields(targetEntity, targetEntityValues); targetEntity = targetStore.read(targetEntity); // set at source setValueAtNavigationField(sourceEntity, sourceField, targetEntity); // set at target Field targetField = commonNavInfo.getToField(); if (targetField != null) { setValueAtNavigationField(targetEntity, targetField, sourceEntity); } }
public T delete(final T object) { KeyElement keyElement = getKeys(object); synchronized (dataStore) { return dataStore.remove(keyElement); } }
@Override public Object readData(final EdmEntitySet entitySet, final Map<String, Object> keys) throws ODataNotFoundException, EdmException, ODataApplicationException { DataStore<Object> store = getDataStore(entitySet); if (store != null) { Object keyInstance = store.createInstance(); ANNOTATION_HELPER.setKeyFields(keyInstance, keys); Object result = store.read(keyInstance); if (result != null) { return result; } } throw new ODataNotFoundException(ODataNotFoundException.ENTITY); }
/** * Read the result data from the target store based on <code>sourceData</code> and <code>sourceField</code> * * @param targetStore * @param sourceData * @param sourceField * @return * @throws DataStoreException */ private List<Object> readResultData(final DataStore<?> targetStore, final Object sourceData, final Field sourceField) throws DataStoreException { Object navigationInstance = getValue(sourceField, sourceData); if (navigationInstance == null) { return Collections.emptyList(); } List<Object> resultData = new ArrayList<Object>(); for (Object targetInstance : targetStore.read()) { if (navigationInstance instanceof Collection) { for (Object object : (Collection<?>) navigationInstance) { if (targetStore.isKeyEqualChecked(targetInstance, object)) { resultData.add(targetInstance); } } } else if (targetStore.isKeyEqualChecked(targetInstance, navigationInstance)) { resultData.add(targetInstance); } } return resultData; }
return resultData; } else { Object keyInstance = targetStore.createInstance(); ANNOTATION_HELPER.setKeyFields(keyInstance, targetKeys); for (Object result : resultData) { if (targetStore.isKeyEqualChecked(result, keyInstance)) { return result;