Search in sources :

Example 1 with EzyPair

use of com.tvd12.ezyfox.util.EzyPair in project dahlia by youngmonkeys.

the class CommandDeleteHandler method handle.

@Override
public Object handle(CommandDelete command) {
    int collectionId = command.getCollectionId();
    Collection collection = databases.getCollection(collectionId);
    if (collection == null)
        throw new CollectionNotFoundException(collectionId);
    EzyObject query = command.getQuery();
    Predicate<EzyObject> predicate = queryToPredicate.toPredicate(query);
    CollectionSetting setting = collection.getSetting();
    CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
    FieldSetting sId = setting.getId();
    Map<String, FieldSetting> sFields = setting.getFields();
    List<EzyPair<Record, EzyObject>> deletedItems = new ArrayList<>();
    synchronized (collection) {
        collection.forEach(new RecordConsumer() {

            @Override
            public void accept(Record r) {
                EzyObject value = collectionStorage.readRecord(r, sId, sFields);
                boolean accepted = predicate.test(value);
                if (accepted)
                    deletedItems.add(new EzyPair<>(r, value));
            }
        });
        for (EzyPair<Record, EzyObject> pair : deletedItems) {
            Record deletedRecord = pair.getKey();
            EzyObject deletedValue = pair.getValue();
            Comparable id = deletedRecord.getId();
            collection.remove(id);
            deletedRecord.setAlive(false);
            collectionStorage.storeRecord(deletedRecord, sId, sFields, deletedValue);
        }
    }
    EzyArray answer = EzyEntityFactory.newArray();
    for (EzyPair<Record, EzyObject> pair : deletedItems) {
        EzyObject answerItem = EzyEntityFactory.newObject();
        EzyObject updateItem = pair.getValue();
        answerItem.put(Constants.FIELD_ID, updateItem.get(Constants.FIELD_ID));
    }
    return answer;
}
Also used : ArrayList(java.util.ArrayList) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) RecordConsumer(com.tvd12.dahlia.core.function.RecordConsumer) CollectionNotFoundException(com.tvd12.dahlia.exception.CollectionNotFoundException) FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) EzyPair(com.tvd12.ezyfox.util.EzyPair) EzyArray(com.tvd12.ezyfox.entity.EzyArray) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Example 2 with EzyPair

use of com.tvd12.ezyfox.util.EzyPair in project dahlia by youngmonkeys.

the class CommandUpdateHandler method handle.

@Override
public Object handle(CommandUpdate command) {
    int collectionId = command.getCollectionId();
    Collection collection = databases.getCollection(collectionId);
    if (collection == null)
        throw new CollectionNotFoundException(collectionId);
    EzyObject query = command.getQuery();
    Predicate<EzyObject> predicate = queryToPredicate.toPredicate(query);
    CollectionSetting setting = collection.getSetting();
    CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
    FieldSetting sId = setting.getId();
    Map<String, FieldSetting> sFields = setting.getFields();
    EzyObject update = command.getUpdate();
    List<EzyPair<Record, EzyObject>> updateItems = new ArrayList<>();
    synchronized (collection) {
        collection.forEach(new RecordConsumer() {

            @Override
            public void accept(Record r) {
                EzyObject value = collectionStorage.readRecord(r, sId, sFields);
                boolean accepted = predicate.test(value);
                if (accepted)
                    updateItems.add(new EzyPair<>(r, value));
            }
        });
        for (EzyPair<Record, EzyObject> pair : updateItems) {
            Record record = pair.getKey();
            EzyObject updateItem = pair.getValue();
            updateItem(updateItem, update);
            collectionStorage.storeRecord(record, sId, sFields, updateItem);
        }
    }
    EzyArray answer = EzyEntityFactory.newArray();
    for (EzyPair<Record, EzyObject> pair : updateItems) {
        EzyObject answerItem = EzyEntityFactory.newObject();
        EzyObject updateItem = pair.getValue();
        answerItem.put(Constants.FIELD_ID, updateItem.get(Constants.FIELD_ID));
    }
    return answer;
}
Also used : ArrayList(java.util.ArrayList) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) RecordConsumer(com.tvd12.dahlia.core.function.RecordConsumer) CollectionNotFoundException(com.tvd12.dahlia.exception.CollectionNotFoundException) FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) EzyPair(com.tvd12.ezyfox.util.EzyPair) EzyArray(com.tvd12.ezyfox.entity.EzyArray) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Aggregations

Collection (com.tvd12.dahlia.core.entity.Collection)2 Record (com.tvd12.dahlia.core.entity.Record)2 RecordConsumer (com.tvd12.dahlia.core.function.RecordConsumer)2 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)2 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)2 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)2 CollectionNotFoundException (com.tvd12.dahlia.exception.CollectionNotFoundException)2 EzyArray (com.tvd12.ezyfox.entity.EzyArray)2 EzyObject (com.tvd12.ezyfox.entity.EzyObject)2 EzyPair (com.tvd12.ezyfox.util.EzyPair)2 ArrayList (java.util.ArrayList)2