Ejemplo n.º 1
0
        public virtual void AfterDelete()
        {
            Type superClass = Reflections.GetBaseType(this.GetType());

            if (superClass != typeof(PersistentEntity))
            {
                PersistentEntity superObject = ((PersistentEntity)Reflections.GetSuperInstanceFromInstance(this));
                superObject.Delete();
            }
        }
Ejemplo n.º 2
0
        ///** Creates a <code>JSONObject</code> from the object.
        // *
        // * @param object to be converted in a <code>JSONObject</code>.
        // * @return the generated <code>JSONObject</code>.
        // */
        //public JSONObject wrap(E object) {
        //	JSONObject _JSONObject = new JSONObject();
        //	try {
        //		Class superClass = domainClass.getSuperclass();
        //		PersistentEntity superObject = null;
        //		if (superClass != PersistentEntity.class) {
        //		                try {
        //		                    _JSONObject = ((PersistentEntity) superClass.newInstance()).getTableData().wrap(object);
        //		                } catch (Exception e) {
        //		                    e.printStackTrace();
        //		                }
        //		            }
        //		            for (Field field : getColumnFields()) {
        //		                _JSONObject = EntityFieldHelper.setFieldInJSONObject(object, field, _JSONObject);
        //		            }
        //		        } catch (Exception e) {
        //		            e.printStackTrace();
        //		        }
        //		        return _JSONObject;
        //		    }

        //		    /** Creates a <code>JSONArray</code> from the object.
        //		     *
        //		     * @param list to be converted in a <code>JSONArray</code>.
        //		     * @return the generated <code>JSONArray</code>.
        //		     */
        //		    public JSONArray wrap(List<E> list) {
        //	JSONArray _JSONArray = new JSONArray();
        //	if (list != null) {
        //		for (E object : list) {
        //			_JSONArray.put(wrap(object));
        //		}
        //	}
        //	return _JSONArray;
        //}

        private PersistentEntity GetSuperObject(long id)
        {
            Type             superClass  = Reflections.GetBaseType(tableMapping.type);
            PersistentEntity superObject = null;

            if (superClass != typeof(PersistentEntity))
            {
                superObject = ((PersistentEntity)Activator.CreateInstance(superClass)).GetTableData().Get(id);
            }
            return(superObject);
        }
Ejemplo n.º 3
0
        public virtual bool BeforeUpdate()
        {
            Type superClass = Reflections.GetBaseType(this.GetType());

            if (superClass != typeof(PersistentEntity))
            {
                PersistentEntity superObject = ((PersistentEntity)Reflections.GetSuperInstanceFromInstance(this));
                if (superObject.Update())
                {
                    this.SetId(superObject.GetId());
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 4
0
        /******************************************************** Table queries */

        /**
         * Generate the <code>CREATE TABLE<code> query from a <code>PersistentEntity</code> class
         * @param domainClass the class of the persistent entity, must extends <code>PersistentEntity</code>
         * @return the generated <code>CREATE TABLE<code> query
         */
        public static String CreateTableQuery(TableMapping <E> table)
        {
            ColumnInfo[] columns    = table.columns;
            String       sqlCreate  = "CREATE TABLE IF NOT EXISTS " + table.name + " (" + Configuration.ID_COLUMN_NAME + " INTEGER PRIMARY KEY AUTOINCREMENT";
            Type         superClass = Reflections.GetBaseType(table.type);

            /*if (superClass != typeof(PersistentEntity)) {
             *      //TODO: hay que ver en que orden se crean las clases y como corregir que la referencia no existe
             *      //TODO: en principio esta resuelto con foreign_check = O;
             *      sqlCreate += " REFERENCES " + superClass.Name + "(" + Configuration.ID_COLUMN_NAME + ") ON DELETE CASCADE";
             *      //TODO: esto lo hice por ON DELETE CASCADE pero no funciona :S
             * }*/
            for (int i = 0; i < columns.Length; i++)
            {
                ColumnInfo column = columns[i];
                if (!column.IsMultipleRelationship)
                {
                    sqlCreate += ", " + ColumnDeclaration(column);
                }
            }
            sqlCreate += ");";
            //        TODO: Cuidado con estos replace que son solo esteticos y pueden joder la sentencia
            return(sqlCreate.Replace("  ", " ").Replace(" ,", ","));
        }
Ejemplo n.º 5
0
        /** Creates an object from the stored data in the JSON.
         *
         * @param _JSONObject with the data of the object.
         * @return the generated instance.
         */
        public E Parse(JToken _JSONObject)
        {
            try {
                E obj = (E)Activator.CreateInstance(tableMapping.type);
                for (int i = 0; i < this.tableMapping.columns.Length; i++)
                {
                    ColumnInfo column = this.tableMapping.columns[i];
                    if (column.IsPrimitiveField)
                    {
                        if (_JSONObject[column.name] != null)
                        {
                            object value = column.propertyType == typeof(String) ? _JSONObject[column.name].Value <String>() : JsonConvert.DeserializeObject(_JSONObject[column.name].ToString(), column.propertyType);
                            column.property.SetValue(obj, value);
                        }
                        else
                        {
                            column.property.SetValue(obj, null);
                        }
                    }
                    else if (column.IsSingleRelationship)
                    {
                        object           value          = null;
                        PersistentEntity relationObject = (PersistentEntity)Activator.CreateInstance(column.propertyType);

                        if (_JSONObject[QueryGenerator <E> .ColumnName(column)] != null)
                        {
                            relationObject.SetServerId(JsonConvert.DeserializeObject <long>(_JSONObject[QueryGenerator <E> .ColumnName(column)].ToString()));
                            value = relationObject;
                        }
                        else if (_JSONObject[column.property.Name] != null)
                        {
                            value = relationObject.GetTableData().Parse(_JSONObject[column.name]);
                        }

                        column.property.SetValue(obj, value);
                    }
                    else if (column.IsMultipleRelationship)
                    {
                        if (_JSONObject[column.name] != null)
                        {
                            PersistentEntity        relationObject = (PersistentEntity)Activator.CreateInstance(column.propertyType.GenericTypeArguments[0]);
                            List <PersistentEntity> value          = relationObject.GetTableData().Parse(JArray.Parse(_JSONObject[column.name].ToString()));
                            column.property.SetValue(obj, value);
                        }
                        else
                        {
                            column.property.SetValue(obj, null);
                        }
                    }
                }

                Type baseType = Reflections.GetBaseType(this.tableMapping.type);
                if (baseType != typeof(PersistentEntity))
                {
                    Reflections.SetInstanceFromSuperInstance(obj, ((PersistentEntity)Activator.CreateInstance(baseType)).GetTableData().Parse(_JSONObject));
                }
                return(obj);
            } catch (Exception e) {
                SQLConsole.WriteLine(e.ToString());
                return(null);
            }
        }