Search in sources :

Example 1 with CommandSaveOne

use of com.tvd12.dahlia.core.command.CommandSaveOne in project dahlia by youngmonkeys.

the class CommandSaveOneHandler method handle.

@Override
public Object handle(CommandSaveOne command) {
    int collectionId = command.getCollectionId();
    EzyObject data = command.getData();
    Collection collection = databases.getCollection(collectionId);
    CollectionSetting setting = collection.getSetting();
    CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
    Comparable id = data.get(Constants.FIELD_ID);
    EzyObject answer = EzyEntityFactory.newObject();
    synchronized (collection) {
        if (id != null) {
            Record existed = collection.findById(id);
            if (existed != null)
                answer.put(Constants.RESULT_FIELD_EXISTED, true);
        } else {
            while (true) {
                id = UUID.randomUUID();
                Record existed = collection.findById(id);
                if (existed == null)
                    break;
            }
            data.put(Constants.FIELD_ID, id);
        }
        Record record = new Record(id, collection.getDataSize());
        collection.insert(record);
        collection.increaseDataSize();
        collectionStorage.storeRecord(record, setting.getId(), setting.getFields(), data);
    }
    answer.put(Constants.FIELD_ID, id);
    return answer;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Example 2 with CommandSaveOne

use of com.tvd12.dahlia.core.command.CommandSaveOne in project dahlia by youngmonkeys.

the class LocalCollection method save.

@Override
public EzyObject save(EzyObject record) {
    CommandSaveOne command = new CommandSaveOne(store.getId(), record);
    EzyObject result = commandExecutor.execute(command);
    return result;
}
Also used : CommandSaveOne(com.tvd12.dahlia.core.command.CommandSaveOne) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Aggregations

EzyObject (com.tvd12.ezyfox.entity.EzyObject)2 CommandSaveOne (com.tvd12.dahlia.core.command.CommandSaveOne)1 Collection (com.tvd12.dahlia.core.entity.Collection)1 Record (com.tvd12.dahlia.core.entity.Record)1 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)1 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)1