Search in sources :

Example 1 with CommandSave

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

the class LocalCollection method save.

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

Example 2 with CommandSave

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

the class CommandSaveHandler method handle.

@Override
public Object handle(CommandSave command) {
    int collectionId = command.getCollectionId();
    EzyArray data = command.getData();
    Collection collection = databases.getCollection(collectionId);
    CollectionSetting setting = collection.getSetting();
    FieldSetting sId = setting.getId();
    Map<String, FieldSetting> sFields = setting.getFields();
    long dataSize = collection.getDataSize();
    CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
    EzyArray answerItems = EzyEntityFactory.newArray();
    synchronized (collection) {
        for (int i = 0; i < data.size(); ++i) {
            EzyObject answerItem = EzyEntityFactory.newObject();
            EzyObject item = data.get(i);
            Comparable id = item.get(Constants.FIELD_ID);
            if (id != null) {
                Record existed = collection.findById(id);
                if (existed != null)
                    answerItem.put(Constants.RESULT_FIELD_EXISTED, true);
            } else {
                while (true) {
                    id = UUID.randomUUID();
                    Record existed = collection.findById(id);
                    if (existed == null)
                        break;
                }
            }
            Record record = new Record(id, dataSize);
            collection.insert(record);
            collection.increaseDataSize();
            collectionStorage.storeRecord(record, sId, sFields, item);
            answerItem.put(Constants.FIELD_ID, id);
            answerItems.add(id);
        }
    }
    return answerItems;
}
Also used : FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) 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) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Aggregations

EzyArray (com.tvd12.ezyfox.entity.EzyArray)2 CommandSave (com.tvd12.dahlia.core.command.CommandSave)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 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)1 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)1 EzyObject (com.tvd12.ezyfox.entity.EzyObject)1