<commit_before>package io.divide.shared.web.transitory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.divide.shared.web.transitory.query.Query;

import java.util.*;

import static io.divide.shared.util.ObjectUtils.isArray;
import static io.divide.shared.util.ObjectUtils.v2c;

/*  *
 * Created with IntelliJ IDEA.
 * User: williamwebb
 * Date: 8/20/13
 * Time: 6:38 PM
 *
 * User entered data will
 */

public class TransientObject {
    public static final String USER_DATA = "user_data";
    public static final String META_DATA = "meta_data";

    public static final MetaKey OBJECT_TYPE_KEY   = new MetaKey("object_type");
    public static final MetaKey OWNER_ID_KEY       = new MetaKey("owner_key");
    public static final MetaKey OBJECT_KEY        = new MetaKey("object_key");
    public static final MetaKey CREATE_DATE_KEY   = new MetaKey("create_date_key");
    public static final MetaKey MODIFIED_DATE_KEY = new MetaKey("modified_date_key");
    private static final MetaKey PERMISSIONS_KEY = new MetaKey("permissions_key");

    private static Gson gson = initGson();
    protected Map<String,Object> user_data = new LinkedHashMap<String, Object>();
    protected Map<String,String> meta_data = new LinkedHashMap<String, String>();

    private transient boolean isNewObject = false;

    private TransientObject(){}

    protected <T extends TransientObject> TransientObject(Class<T> objectType){
        isNewObject = true;
        meta_put(PERMISSIONS_KEY, new FilePermissions());
//        setOwnerId(getLoggedInUser());
        setObjectType(Query.safeTable(objectType));
        setObjectKey(UUID.randomUUID().toString());
        updateCreateDate();
    }

    private static Gson initGson(){
        Gson gson = new GsonBuilder()
                .serializeNulls()
                .create();
        return gson;
    }

    protected Credentials getLoggedInUser(){
        return null;
    }

    public boolean isReadable(){
        if(isNewObject)return true;
        FilePermissions fp = getFilePermissions();

        Credentials user = getLoggedInUser();

        if(user!=null && user.isSystemUser()) return true;
        else if(fp.isReadable(FilePermissions.Level.WORLD))return true;
        else if(fp.isReadable(FilePermissions.Level.GROUP) && user!=null && fp.getGroups().contains(user.getUserGroup())) return true;
        else if(fp.isReadable(FilePermissions.Level.OWNER) && user!=null && getOwnerId().equals(user)) return true;
        else return false;
    }

    public boolean isWritable(){
        if(isNewObject)return true;
        FilePermissions fp = getFilePermissions();

        Credentials user = getLoggedInUser();

        if(user!=null && user.isSystemUser()) return true;
        else if(fp.isWritable(FilePermissions.Level.WORLD))return true;
        else if(fp.isWritable(FilePermissions.Level.GROUP) && user!=null && fp.getGroups().contains(user.getUserGroup())) return true;
        else if(fp.isWritable(FilePermissions.Level.OWNER) && user!=null && getOwnerId().equals(user)) return true;
        else return false;
    }

    private void canRead(){
        if(!isReadable()) {
            throw new IllegalAccessError(meta_data.get(OBJECT_KEY.KEY) + " is not readable for " + getLoggedInUser() +". " + getFilePermissions());
        }
    }

    private void canWrite(){
        if(!isWritable()) {
            throw new IllegalAccessError(meta_data.get(OBJECT_KEY.KEY) + " is not writable for " + getLoggedInUser() +". " + getFilePermissions());
        }
    }

    public void setFilePermissions(FilePermissions filePermissions){
        if(isWritable()){
            meta_data.put(PERMISSIONS_KEY.KEY,gson.toJson(filePermissions,FilePermissions.class));
        }
    }

    public FilePermissions getFilePermissions(){
        return gson.fromJson(meta_data.get(PERMISSIONS_KEY.KEY), FilePermissions.class);
    }

    protected final void meta_put(MetaKey key, Object object) {
        canWrite();

        updateModifiedDate();
        if(object instanceof String)
            meta_data.put(key.KEY, (String) object);
        else
            meta_data.put(key.KEY,gson.toJson(object));
    }

    protected final <O> O meta_get(Class<O> clazz, MetaKey key){
//        canRead();

        if(clazz.equals(String.class))
            return (O) meta_data.get(key.KEY);
        else
            return gson.fromJson(meta_data.get(key.KEY),clazz);
    }

    protected final boolean meta_remove(MetaKey key){
        return meta_data.remove(key.KEY) != null;
    }

    public void put(String key, Object object) {
        canWrite();

        updateModifiedDate();
        user_data.put(key,object);

//        if(isCollection(object)){
//            Object[] array = ((Collection) object).toArray(new Object[0]);
//            user_data.put(key, array);
//        }
//        else if(isArray(object)){
//            Object[] array = (Object[]) object;
//            user_data.put(key, array);
//        } else {
//            user_data.put(key,object);
//        }
    }

    public void putAll(Map<? extends String ,? extends Object> map) {
        canWrite();

        updateModifiedDate();
        user_data.putAll(map);
    }

    public <O> O get(Class<O> clazz, String key){
        canWrite();
        Object o = null;
        try{
            o = user_data.get(key);
            return clazz.cast( o );
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("Get(" + clazz.getSimpleName() + "," + key + ")");
            System.out.println(o);
            return null;
        }

//        if(isCollection(clazz)){
//            return (O) v2c((Object[])user_data.get(key));
//        } else {
//            return (O) user_data.get(key);
//        }
    }

    public final boolean remove(String key){
        canWrite();

        updateModifiedDate();
        return user_data.remove(key) != null;
    }

    public final void removeAll(){
        canWrite();

        updateModifiedDate();
        user_data.clear();
    }

    private long updateCreateDate(){<commit_msg>Add option to directly serialize object instead of leaving it up to networking code.<commit_after>package io.divide.shared.web.transitory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.divide.shared.web.transitory.query.Query;

import java.util.*;

import static io.divide.shared.util.ObjectUtils.isArray;
import static io.divide.shared.util.ObjectUtils.v2c;

/*  *
 * Created with IntelliJ IDEA.
 * User: williamwebb
 * Date: 8/20/13
 * Time: 6:38 PM
 *
 * User entered data will
 */

public class TransientObject {
    public static final String USER_DATA = "user_data";
    public static final String META_DATA = "meta_data";

    public static final MetaKey OBJECT_TYPE_KEY   = new MetaKey("object_type");
    public static final MetaKey OWNER_ID_KEY       = new MetaKey("owner_key");
    public static final MetaKey OBJECT_KEY        = new MetaKey("object_key");
    public static final MetaKey CREATE_DATE_KEY   = new MetaKey("create_date_key");
    public static final MetaKey MODIFIED_DATE_KEY = new MetaKey("modified_date_key");
    private static final MetaKey PERMISSIONS_KEY = new MetaKey("permissions_key");

    private static Gson gson = initGson();
    protected Map<String,Object> user_data = new LinkedHashMap<String, Object>();
    protected Map<String,String> meta_data = new LinkedHashMap<String, String>();

    private transient boolean isNewObject = false;

    private TransientObject(){}

    protected <T extends TransientObject> TransientObject(Class<T> objectType){
        isNewObject = true;
        meta_put(PERMISSIONS_KEY, new FilePermissions());
//        setOwnerId(getLoggedInUser());
        setObjectType(Query.safeTable(objectType));
        setObjectKey(UUID.randomUUID().toString());
        updateCreateDate();
    }

    private static Gson initGson(){
        Gson gson = new GsonBuilder()
                .serializeNulls()
                .create();
        return gson;
    }

    protected Credentials getLoggedInUser(){
        return null;
    }

    public boolean isReadable(){
        if(isNewObject)return true;
        FilePermissions fp = getFilePermissions();

        Credentials user = getLoggedInUser();

        if(user!=null && user.isSystemUser()) return true;
        else if(fp.isReadable(FilePermissions.Level.WORLD))return true;
        else if(fp.isReadable(FilePermissions.Level.GROUP) && user!=null && fp.getGroups().contains(user.getUserGroup())) return true;
        else if(fp.isReadable(FilePermissions.Level.OWNER) && user!=null && getOwnerId().equals(user)) return true;
        else return false;
    }

    public boolean isWritable(){
        if(isNewObject)return true;
        FilePermissions fp = getFilePermissions();

        Credentials user = getLoggedInUser();

        if(user!=null && user.isSystemUser()) return true;
        else if(fp.isWritable(FilePermissions.Level.WORLD))return true;
        else if(fp.isWritable(FilePermissions.Level.GROUP) && user!=null && fp.getGroups().contains(user.getUserGroup())) return true;
        else if(fp.isWritable(FilePermissions.Level.OWNER) && user!=null && getOwnerId().equals(user)) return true;
        else return false;
    }

    private void canRead(){
        if(!isReadable()) {
            throw new IllegalAccessError(meta_data.get(OBJECT_KEY.KEY) + " is not readable for " + getLoggedInUser() +". " + getFilePermissions());
        }
    }

    private void canWrite(){
        if(!isWritable()) {
            throw new IllegalAccessError(meta_data.get(OBJECT_KEY.KEY) + " is not writable for " + getLoggedInUser() +". " + getFilePermissions());
        }
    }

    public void setFilePermissions(FilePermissions filePermissions){
        if(isWritable()){
            meta_data.put(PERMISSIONS_KEY.KEY,gson.toJson(filePermissions,FilePermissions.class));
        }
    }

    public FilePermissions getFilePermissions(){
        return gson.fromJson(meta_data.get(PERMISSIONS_KEY.KEY), FilePermissions.class);
    }

    protected final void meta_put(MetaKey key, Object object) {
        canWrite();

        updateModifiedDate();
        if(object instanceof String)
            meta_data.put(key.KEY, (String) object);
        else
            meta_data.put(key.KEY,gson.toJson(object));
    }

    protected final <O> O meta_get(Class<O> clazz, MetaKey key){
//        canRead();

        if(clazz.equals(String.class))
            return (O) meta_data.get(key.KEY);
        else
            return gson.fromJson(meta_data.get(key.KEY),clazz);
    }

    protected final boolean meta_remove(MetaKey key){
        return meta_data.remove(key.KEY) != null;
    }

    public void put(String key, Object object, boolean serialize){
        if(serialize)
            put(key,gson.toJson(object));
        else
            put(key,object);
    }

    public void put(String key, Object object) {
        canWrite();

        updateModifiedDate();
        user_data.put(key,object);

//        if(isCollection(object)){
//            Object[] array = ((Collection) object).toArray(new Object[0]);
//            user_data.put(key, array);
//        }
//        else if(isArray(object)){
//            Object[] array = (Object[]) object;
//            user_data.put(key, array);
//        } else {
//            user_data.put(key,object);
//        }
    }

    public void putAll(Map<? extends String ,? extends Object> map) {
        canWrite();

        updateModifiedDate();
        user_data.putAll(map);
    }

    public <O> O get(Class<O> clazz, String key, boolean deserialize){
        if(deserialize)
            return gson.fromJson(get(String.class,key),clazz);
        else
            return get(clazz,key);
    }

    public <O> O get(Class<O> clazz, String key){
        canWrite();
        Object o = null;
        try{
            o = user_data.get(key);
            return clazz.cast( o );
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("Get(" + clazz.getSimpleName() + "," + key + ")");
            System.out.println(o);
            return null;
        }
    }

    public final boolean remove(String key){
        canWrite();

        updateModifiedDate();
        return user_data.remove(key) != null;
    }

    public final void removeAll(){
        canWrite();

        updateModifiedDate();
        user_data.clear();
    }

    private long updateCreateDate(){
{'fraction_non_alphanumeric': 0.1209098164405427, 'fraction_numerical': 0.0016759776536312849, 'mean_word_length': 2.660823838737949, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1090661', 'n_tokens_mistral': 3702, 'n_tokens_neox': 3642, 'n_words': 795}