Changeset 16578
- Timestamp:
- 06/11/08 17:49:24 (4 months ago)
- Files:
-
- trunk/midgard/core/midgard/src/midgard_object.c (modified) (23 diffs)
- trunk/midgard/core/midgard/src/midgard_object.h (modified) (4 diffs)
- trunk/midgard/core/midgard/src/midgard_tree.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/midgard/core/midgard/src/midgard_object.c
r16505 r16578 1 1 /* 2 Copyright (C) 2004,2005,2006,2007 Piotr Pokora <piotr.pokora@infoglob.pl>2 Copyright (C) 2004,2005,2006,2007,2008 Piotr Pokora <piotrek.pokora@gmail.com> 3 3 Copyright (C) 2004 Alexander Bokovoy <ab@samba.org> 4 4 … … 556 556 } 557 557 558 /** 559 * midgard_object_set_guid: 560 * @self: #MgdObject instance 561 * @guid: guid to set 562 * 563 * Sets object's guid 564 * 565 * Cases to return %FALSE: 566 * - given guid already exists in database ( MGD_ERR_DUPLICATE ) 567 * - given guid is invalid ( MGD_ERR_INVALID_PROPERTY_VALUE ) 568 * - object has already set guid property ( MGD_ERR_INVALID_PROPERTY_VALUE ) 569 * 570 * Returns: %TRUE on success, %FALSE otherwise 571 */ 558 572 gboolean midgard_object_set_guid(MgdObject *self, const gchar *guid) 559 573 { … … 772 786 } 773 787 788 /** 789 * midgard_object_update: 790 * @self: #MgdObject instance 791 * 792 * Update object's record(s). 793 * 794 * Internally such metadata properties are set (overwritten): 795 * - revisor 796 * - revision ( increased by one ) 797 * - revised 798 * 799 * Cases to return %FALSE: 800 * - Property registered with #MGD_TYPE_GUID doesn't hold valid guid ( MGD_ERR_INVALID_PROPERTY_VALUE ) 801 * - Object's class is registered with tree facilities and there is already such object in midgard tree ( MGD_ERR_DUPLICATE ) 802 * - Quota is activated and its limit is reached ( MGD_ERR_QUOTA ) 803 * - Unspecified internal error ( MGD_ERR_INTERNAL ) 804 * 805 * Returns: %TRUE if object's record(s) is successfully updated, %FALSE otherwise. 806 */ 774 807 gboolean midgard_object_update(MgdObject *self) 775 808 { … … 1112 1145 } 1113 1146 1147 /** 1148 * midgard_object_create: 1149 * @object: #MgdObject instance 1150 * 1151 * Creates new database record(s) for object. 1152 * 1153 * Internally such properties are set (overwritten): 1154 * - sitegroup 1155 * - guid (if not set by root) 1156 * - id (if set as primary property) 1157 * 1158 * Metadata properties: 1159 * - creator 1160 * - created 1161 * - revisor 1162 * - revised 1163 * - revision 1164 * - published ( set only, if not defined by user ) 1165 * 1166 * Cases to return %FALSE: 1167 * - Property registered with #MGD_TYPE_GUID doesn't hold valid guid ( MGD_ERR_INVALID_PROPERTY_VALUE ) 1168 * - Object's class is registered with tree facilities and there is already such object in midgard tree ( MGD_ERR_DUPLICATE ) 1169 * - Quota is activated and its limit is reached ( MGD_ERR_QUOTA ) 1170 * - Unspecified internal error ( MGD_ERR_INTERNAL ) 1171 * 1172 * Returns: %TRUE on success, %FALSE otherwise 1173 */ 1114 1174 gboolean midgard_object_create(MgdObject *object) 1115 1175 { … … 1181 1241 1182 1242 1183 /* Get object by its id property, id must be uint type */ 1184 gboolean midgard_object_get_by_id(MgdObject *object, guint id) 1243 /** 1244 * midgard_object_get_by_id: 1245 * @object: #MgdObject instance 1246 * @id, object's integer identifier 1247 * 1248 * Fetch object's record(s) from database using 'id' property. 1249 * 1250 * This is common practice to use 'id' property with integer type when table's 1251 * id column stores unique, primary key value which identifies object and its record(s). 1252 * However primary property with integer type is freely defined by user. 1253 * 1254 * MgdObject object instance must be created with midgard_object_new function. 1255 * When midgard connection handler is not associated with object instance, 1256 * application is terminated with 'assertion fails' error message being logged. 1257 * 1258 * Object instance created with this function should be freed using #g_object_unref. 1259 * 1260 * Cases to return %FALSE: 1261 * - There's no 'id' primary property registered for object's class ( MGD_ERR_INTERNAL ) 1262 * - Object's record can not be fetched from database ( MGD_ERR_NOT_EXISTS ) 1263 * - unspecified internal error ( MGD_ERR_INTERNAL ) 1264 * 1265 * Returns: %TRUE if object is successfully fetched from database, %FALSE otherwise. 1266 */ 1267 gboolean midgard_object_get_by_id(MgdObject *object, guint id) 1185 1268 { 1186 1269 g_assert(object != NULL); … … 1196 1279 1197 1280 /* Find "primary" property , just in case of fatal error */ 1198 prop = g_object_class_find_property( 1199 (GObjectClass*)klass, 1200 "id"); 1281 prop = g_object_class_find_property((GObjectClass*)klass, "id"); 1282 1201 1283 if(prop == NULL){ 1202 1284 MIDGARD_ERRNO_SET(object->dbpriv->mgd, MGD_ERR_INTERNAL); … … 1251 1333 return FALSE; 1252 1334 } 1253 1254 GObject **midgard_object_find(MgdObject *object, MidgardTypeHolder *holder)1255 {1256 GParamSpec **props;1257 guint propn, i;1258 GValue pval = {0,};1259 1260 MIDGARD_ERRNO_SET(object->dbpriv->mgd, MGD_ERR_OK);1261 1262 props = g_object_class_list_properties(1263 G_OBJECT_CLASS(MIDGARD_OBJECT_GET_CLASS(object)), &propn);1264 if(!props){1265 g_warning("No properties found for %s class!",1266 G_OBJECT_TYPE_NAME(object));1267 MIDGARD_ERRNO_SET(object->dbpriv->mgd, MGD_ERR_INTERNAL);1268 return NULL;1269 }1270 1271 MidgardQueryBuilder *builder =1272 midgard_query_builder_new(object->dbpriv->mgd, G_OBJECT_TYPE_NAME(object));1273 if(!builder){1274 MIDGARD_ERRNO_SET(object->dbpriv->mgd, MGD_ERR_INTERNAL);1275 return NULL;1276 }1277 1278 for(i = 0; i < propn; i++) {1279 g_value_init(&pval,props[i]->value_type);1280 g_object_get_property(G_OBJECT(object), props[i]->name, &pval);1281 switch (props[i]->value_type) {1282 1283 case G_TYPE_STRING:1284 if(g_value_get_string(&pval))1285 midgard_query_builder_add_constraint(builder,1286 props[i]->name, "=", &pval);1287 break;1288 1289 case G_TYPE_UINT:1290 if(g_value_get_uint(&pval))1291 midgard_query_builder_add_constraint(builder,1292 props[i]->name, "=", &pval);1293 break;1294 1295 case G_TYPE_INT:1296 if(g_value_get_int(&pval))1297 midgard_query_builder_add_constraint(builder,1298 props[i]->name, "=", &pval);1299 break;1300 1301 case G_TYPE_FLOAT:1302 if(g_value_get_float(&pval))1303 midgard_query_builder_add_constraint(builder,1304 props[i]->name, "=", &pval);1305 break;1306 1307 case G_TYPE_BOOLEAN:1308 if(g_value_get_boolean(&pval))1309 midgard_query_builder_add_constraint(builder,1310 props[i]->name, "=", &pval);1311 break;1312 1313 case G_TYPE_OBJECT:1314 /* TODO */1315 break;1316 1317 default:1318 if(g_value_get_string(&pval))1319 midgard_query_builder_add_constraint(builder,1320 props[i]->name, "=", &pval);1321 break;1322 }1323 g_value_unset(&pval);1324 }1325 1326 GObject **objects = midgard_query_builder_execute(builder, holder);1327 g_object_unref(builder);1328 return objects;1329 }1330 1331 1335 1332 1336 /* Initialize class. … … 1714 1718 } 1715 1719 1720 /** 1721 * midgard_object_new: 1722 * @mgd: #MidgardConnection handler 1723 * @name: name of the class 1724 * @value: optional value which holds id or guid of an object 1725 * 1726 * Creates new MgdObject object instance. 1727 * 1728 * This function is mandatory one for new midgard object initialization. 1729 * Unlike g_object_new ( which is typical function to create new GObject 1730 * instance ), midgard_object_new initializes data which are accessible 1731 * internally by object instance itself or by object's class: 1732 * 1733 * - midgard connection handler is associated with object 1734 * - 'sitegroup' property of an object is being set 1735 * 1736 * Sitegroup value is returned from midgard connection handler and may 1737 * be overwritten only by SG0 Midgard Administrator only when object 1738 * is created. Setting this property is forbidden when object is already 1739 * fetched from database. 1740 * 1741 * Object's contructor tries to determine third optional parameter value. 1742 * If it's of #G_TYPE_STRING type , then #midgard_is_guid is called to check 1743 * weather passed string is a guid , in any other case id property is used 1744 * with #G_TYPE_UINT type. New "empty" instance is created (without fetching 1745 * data from database) if @value parameter is explicitly set to NULL. 1746 * 1747 * Any object instance created with this function should be freed using typical 1748 * #g_object_unref function. 1749 * 1750 * Cases to return %NULL: 1751 * - @value holds string but it's not a valid guid 1752 * - @value holds valid id or guid but object doesn't exists in database ( MGD_ERR_NOT_EXISTS ) 1753 * - more than one object identified by particular id or guid returned 1754 * ( case possible if logged in user is root ) ( MGD_ERR_SITEGROUP_VIOLATION ) 1755 * - unspecified internal error ( MGD_ERR_INTERNAL ) 1756 * 1757 * Returns: New #MgdObject object or %NULL on failure 1758 */ 1716 1759 MgdObject * 1717 1760 midgard_object_new(MidgardConnection *mgd, const gchar *name, GValue *value) 1718 1761 { 1719 1762 g_assert(mgd != NULL); 1763 1764 MIDGARD_ERRNO_SET(mgd, MGD_ERR_OK); 1720 1765 1721 1766 GType type; … … 1752 1797 1753 1798 if(!objects) { 1754 g_free(holder); 1799 g_free(holder); 1800 MIDGARD_ERRNO_SET(mgd, MGD_ERR_NOT_EXISTS); 1755 1801 return NULL; 1756 1802 } … … 1779 1825 } 1780 1826 1781 if(!self) 1827 if(!self) { 1828 MIDGARD_ERRNO_SET(mgd, MGD_ERR_INTERNAL); 1782 1829 return NULL; 1830 } 1783 1831 1784 1832 self->dbpriv->storage_data = … … 1787 1835 if (!g_type_class_peek(type)) { 1788 1836 g_object_unref(self); 1837 MIDGARD_ERRNO_SET(mgd, MGD_ERR_INTERNAL); 1789 1838 return NULL; 1790 1839 } … … 1804 1853 } 1805 1854 1806 MgdObject *midgard_object_new_by_id( 1807 MidgardConnection *mgd, const gchar *name, gchar *id) 1808 { 1809 MgdObject *object = midgard_object_new(mgd, name, NULL); 1810 1811 if(object == NULL) 1812 return NULL; 1813 1814 GParamSpec *prop = g_object_class_find_property( 1815 G_OBJECT_GET_CLASS(G_OBJECT(object)), 1816 object->dbpriv->storage_data->primary); 1817 if(prop) { 1818 switch (prop->value_type) { 1819 1820 case G_TYPE_STRING: 1821 g_object_set(G_OBJECT(object), 1822 object->dbpriv->storage_data->primary , id, NULL); 1823 if(!midgard_object_get_by_guid(object, (const gchar *)id)) 1824 return NULL; 1825 break; 1826 1827 case G_TYPE_UINT: 1828 /* FIXME 1829 if(!midgard_object_get_by_id(object, (guint) id)) 1830 return NULL; */ 1831 break; 1832 } 1833 return object; 1834 } 1835 g_object_unref(object); 1836 return NULL; 1837 } 1838 1839 MgdObject * midgard_object_get_parent(MgdObject *mobj) 1840 { 1855 /** 1856 * midgard_object_get_parent: 1857 * @self: #MgdObject instance 1858 * 1859 * Fetch parent ( in midgard tree ) object. 1860 * 1861 * This function fetches "upper" object of the same type, and then parent 1862 * object if object's up property holds empty value. 1863 * Object is root object in tree when NULL is returned. 1864 * 1865 * Returns: new #MgdObject object instance or %NULL 1866 */ 1867 MgdObject *midgard_object_get_parent(MgdObject *self) 1868 { 1869 MgdObject *mobj = self; 1841 1870 g_assert(mobj != NULL); 1842 1871 … … 1986 2015 } 1987 2016 1988 2017 /** 2018 * midgard_object_get_by_guid: 2019 * @object: #MgdObject instance 2020 * @guid: string value which should identify object 2021 * 2022 * Fetch object's record(s) from database using 'guid' property constraint. 2023 * 2024 * MgdObject object instance must be created with midgard_object_new function. 2025 * When midgard connection handler is not associated with object instance, 2026 * application is terminated with 'assertion fails' error message being logged. 2027 * 2028 * Object instance created with this function should be freed using g_object_unref. 2029 * 2030 * Cases to return %FALSE: 2031 * - Object's record can not be fetched from database ( MGD_ERR_NOT_EXISTS ) 2032 * - unspecified internal error ( MGD_ERR_INTERNAL ) 2033 * 2034 * Retruns: %TRUE if object is successfully fetched from database, %FALSE otherwise. 2035 */ 1989 2036 gboolean midgard_object_get_by_guid(MgdObject *object, const gchar *guid) 1990 2037 { … … 2042 2089 } 2043 2090 2091 /** 2092 * midgard_object_delete: 2093 * @object: #MgdObject instance 2094 * 2095 * Delete object's record(s) from database, but object's record is not fully deleted 2096 * from database. Instead, it is marked as deleted , thus it is possible to undelete 2097 * object later with static method midgard_object_undelete. 2098 * 2099 * If object's klass is defined as multilingual , and there is more than one 2100 * language content in database , then only particular object's content is 2101 * removed. In such case object is not marked as deleted. 2102 * Content itself is not removed if language is set to 0. 2103 * 2104 * Use #midgard_object_purge if you need to purge object's data from database. 2105 * 2106 * FALSE is returned in such cases: 2107 * 2108 * - Object's has no storage defined ( MGD_ERR_OBJECT_NO_STORAGE ) 2109 * - Object's property guid is empty ( MGD_ERR_INVALID_PROPERTY_VALUE ) 2110 * - Unspecified internal error ( MGD_ERR_INTERNAL ) 2111 * 2112 * Returns: %TRUE if object is successfully deleted from database, %FALSE otherwise. 2113 */ 2044 2114 gboolean midgard_object_delete(MgdObject *object) 2045 2115 { … … 2194 2264 } 2195 2265 2266 /** 2267 * midgard_object_purge: 2268 * @object: #MgdObject instance 2269 * 2270 * Purge object's record(s) from database. 2271 * 2272 * Object's record(s) are purged from database without any possibility to recover. 2273 * After successfull call, only repligard table holds information about object's state. 2274 * Use #midgard_object_delete, if undelete facility is needed. 2275 * 2276 * Cases to return %FALSE: 2277 * - Object has no storage defined ( MGD_ERR_OBJECT_NO_STORAGE ) 2278 * - Object's property guid is empty ( MGD_ERR_INVALID_PROPERTY_VALUE ) 2279 * - No record has been deleted from database ( MGD_ERR_NOT_EXISTS ) 2280 * - Unspecified internal error ( MGD_ERR_INTERNAL ) 2281 * 2282 * Returns: %TRUE if object has been succesfully purged from database, %FALSE otherwise. 2283 */ 2196 2284 gboolean midgard_object_purge(MgdObject *object) 2197 2285 { … … 2214 2302 table = _midgard_core_class_get_table(MIDGARD_DBOBJECT_CLASS(klass)); 2215 2303 if(table == NULL) { 2216 /* Object has no storage defined. Return -1as there is nothing to delete */2304 /* Object has no storage defined. Return FALSE as there is nothing to delete */ 2217 2305 g_warning("Object '%s' has no table or storage defined!", 2218 2306 G_OBJECT_TYPE_NAME(object)); … … 2412 2500 } 2413 2501 2414 /* List all "child" objects of the same type which have 2415 * up set to object identifier */ 2416 GObject **midgard_object_list(MgdObject *object, MidgardTypeHolder *holder) 2417 { 2502 /** 2503 * midgard_object_list: 2504 * @self: #MgdObject instance 2505 * @holder: stores the number of returned objects 2506 * 2507 * @holder parameter is optional ( may be explicitly set as NULL ). 2508 * If used, should be freed when no longer needed. 2509 * holder->elements stores number of returned objects 2510 * 2511 * Returned array stores object(s) which up property is equal to @self primary property 2512 * 2513 * Returns: newly allocated, %NULL terminated array of #MgdObject objects 2514 */ 2515 GObject **midgard_object_list(MgdObject *self, MidgardTypeHolder *holder) 2516 { 2517 MgdObject *object = self; 2518 2418 2519 g_assert(object != NULL); 2419 2520 GParamSpec *fprop, *uprop; … … 2464 2565 } 2465 2566 2466 2467 /* List all child objects */ 2567 /** 2568 * midgard_object_list_children: 2569 * @object: #MgdObject instance 2570 * @childname: child typename 2571 * @holder: #MidgardTypeHolder stores number of returned objects 2572 * 2573 * @holder parameter is optional ( may be passed as NULL ). 2574 * If used, should be freed when no longer needed. 2575 * holder->elements stores number of returned objects 2576 * 2577 * Returns: newly allocated, %NULL terminated children objects array 2578 */ 2468 2579 GObject **midgard_object_list_children(MgdObject *object, 2469 2580 const gchar *childcname, MidgardTypeHolder *holder) … … 2536 2647 } 2537 2648 2649 /** 2650 * midgard_object_get_languages: 2651 * @self: #MgdObject instance 2652 * @holder: #MidgardTypeHolder which stores number of returned objects 2653 * 2654 * Returns midgard_language objects for the given Midgard Object instance. 2655 * Returned array is limited to midgard_language objects for which content 2656 * exists for the given Midgard Object instance. 2657 * 2658 * @holder parameter is optional ( may be passed as NULL ). 2659 * If used, should be freed when no longer needed. 2660 * holder->elements stores number of returned objects 2661 * 2662 * Cases to return %NULL: 2663 * - object is not multilang ( MGD_ERR_USER_DATA ) 2664 * - object has invalid guid ( MGD_ERR_INVALID_PROPERTY ) 2665 * 2666 * Returns: newly allocated, %NULL terminated array of #MgdObject objects or %NULL 2667 */ 2538 2668 GObject **midgard_object_get_languages(MgdObject *self, MidgardTypeHolder *holder) 2539 2669 { … … 2635 2765 } 2636 2766 2637 const gchar *midgard_object_parent(MgdObject *object) 2638 { 2639 g_assert(object != NULL); 2640 2641 if (object->dbpriv->storage_data->parent) 2642 return object->dbpriv->storage_data->parent; 2767 /** 2768 * midgard_object_parent: 2769 * @self: #MgdObject instance 2770 * 2771 * Returned class name is owned by midgard and should not be freed. 2772 * 2773 * Returns: class name of parent object ( in content tree ). 2774 */ 2775 const gchar *midgard_object_parent(MgdObject *self) 2776 { 2777 g_assert(self != NULL); 2778 2779 if (self->dbpriv->storage_data->parent) 2780 return self->dbpriv->storage_data->parent; 2643 2781 2644 2782 return NULL; 2645 2783 } 2646 2784 2785 /** 2786 * midgard_object_get_by_path: 2787 * @self: #MgdObject instance 2788 * @path: path at which object should be found 2789 * 2790 * Fetch object's record by path 2791 * 2792 * This function is slower than midgard_object_class_get_object_by_path, 2793 * as it has to create new object instance and copy all properties. 2794 * 2795 * #MidgardError is set by midgard_object_class_get_object_by_path in this case. 2796 * 2797 * Returns: %TRUE if object is found, %FALSE otherwise. 2798 */ 2647 2799 gboolean midgard_object_get_by_path(MgdObject *self, const gchar *path) 2648 2800 { … … 2666 2818 } 2667 2819 2820 2821 /** 2822 * midgard_object_undelete: 2823 * @mgd: #MidgardConnection handler 2824 * @guid: identifier of deleted object 2825 * 2826 * Deprecated. See #midgard_object_class_undelete 2827 * 2828 * Returns: %TRUE if object has been undeleted, %FALSE otherwise. 2829 */ 2668 2830 gboolean midgard_object_undelete(MidgardConnection *mgd, const gchar *guid) 2669 2831 { … … 2671 2833 } 2672 2834 2673 /* Set connection */ 2835 /** 2836 * midgard_object_set_connection: 2837 * @self: #MgdObject instance 2838 * @mgd: #MidgardConnection instance 2839 * 2840 * Set object's connection. 2841 * 2842 * This method associate object with connection being already initialized. 2843 * It's not required to use it in every aplication, however it's mandatory 2844 * when object has been initialized by some underlying library. For example, 2845 * object pointer is available from g_object_new instead of midgard_object_new. 2846 * 2847 * Object's sitegroup is set using given connection's one. 2848 * 2849 * Already associated connection is silently ignored. 2850 */ 2674 2851 void midgard_object_set_connection(MgdObject *self, MidgardConnection *mgd) 2675 2852 { … … 2684 2861 self->dbpriv->sg = midgard_connection_get_sitegroup_id(mgd); 2685 2862 } 2686 2687 gchar *midgard_object_export(MgdObject *self)2688 {2689 g_assert(self);2690 2691 MIDGARD_ERRNO_SET(self->dbpriv->mgd, MGD_ERR_OK);2692 2693 gchar *guid;2694 g_object_get(G_OBJECT(self), "guid", &guid, NULL);2695 2696 if(!guid){2697 midgard_set_error(self->dbpriv->mgd,2698 MGD_GENERIC_ERROR,2699 MGD_ERR_INVALID_PROPERTY_VALUE,2700 "Empty guid value! ");2701 g_warning("%s", self->dbpriv->mgd->errstr);2702 g_clear_error(&self->dbpriv->mgd->err);2703 2704 return NULL;2705 }2706 2707 /* FIXME , remove this select when PHP language bindings use 1:12708 * for zend and gobject objects , REMOVE */2709 /*2710 midgard_res *res;2711 MidgardObjectClass *klass =2712 MIDGARD_OBJECT_GET_CLASS(self);2713 2714 GString *sql = g_string_new("SELECT ");2715 g_string_append_printf(sql,2716 "NULLIF(metadata_exported,'0000-00-00 00:00:00') "2717 "AS metadata_exported FROM %s "2718 "WHERE guid = '%s' ",2719 midgard_object_class_get_table(klass),2720 guid);2721 2722 if(!midgard_core_isroot(self)){2723 g_string_append_printf(sql,2724 " AND sitegroup = %d",2725 _midgard_core_sitegroup(self));2726 }2727 2728 gchar *query = g_string_free(sql, FALSE);2729 res = mgd_query(self, query);2730 g_free(query);2731 2732 if (!res || !mgd_fetch(res)) {2733 if(res) mgd_release(res);2734 MIDGARD_ERRNO_SET(self, MGD_ERR_NOT_EXISTS);2735 return NULL;2736 }2737 self->priv->exported = g_strdup((gchar *)mgd_colvalue(res, 0));2738 mgd_release(res);2739 */2740 /* REMOVE end*/2741 2742 g_free(guid);2743 2744 /* Update object */2745 if(_midgard_object_update(self, OBJECT_UPDATE_EXPORTED))2746 return _midgard_core_object_to_xml(self);2747 2748 return NULL;2749 }trunk/midgard/core/midgard/src/midgard_object.h
r15750 r16578 1 1 /* 2 Copyright (C) 2004,2005,2006 Piotr Pokora <piotr.pokora@infoglob.pl>2 Copyright (C) 2004,2005,2006,2007,2008 Piotr Pokora <piotrek.pokora@gmail.com> 3 3 Copyright (C) 2004 Alexander Bokovoy <ab@samba.org> 4 4 … … 18 18 */ 19 19 20 #ifndef TYPES_H21 #define TYPES_H20 #ifndef MIDGARD_OBJECT_H 21 #define MIDGARD_OBJECT_H 22 22 23 23 /** … … 59 59 typedef struct MidgardMetadata MidgardMetadata; 60 60 typedef struct MidgardMetadataClass MidgardMetadataClass; 61 62 /* Get object by id and by type , only for backwards compatibility */63 64 #define _MIDGARD_OBJECT_GET_BY_TYPE(klassname) \65 if ((object = midgard_object_new(mgd, klassname, NULL)) != NULL ) { \66 if ((midgard_object_get_by_id(object, (guint) id)) == 0); \67 return object; \68 return NULL; \69 } else { \70 return NULL; \71 } \72 61 73 62 struct _MgdObject { … … 130 119 }; 131 120 132 extern void midgard_get_midgard(MgdObject *mobj, MidgardConnection *mgd);121 GType midgard_object_get_type(void); 133 122 134 /**135 * \ingroup MgdObject136 *137 * Returns Midgard Object type.138 * Registers the type as a fundamental GType unless already registered.139 */140 extern GType midgard_object_get_type(void);141 142 /**143 * \ingroup MgdObject144 *145 * MidgardObjectAction defines current state of an object if object146 * is instatiated only by guid without type name information.147 *148 * <pre>149 * enum150 * {151 * MGD_OBJECT_ACTION_NONE = 0,152 * MGD_OBJECT_ACTION_CREATE,153 * MGD_OBJECT_ACTION_UPDATE,154 * MGD_OBJECT_ACTION_DELETE,155 * MGD_OBJECT_ACTION_PURGE156 * }MidgardObjectAction;157 * </pre>158 *159 * - MGD_OBJECT_ACTION_NONE , typical object state160 * - MGD_OBJECT_ACTION_DELETE, object has been deleted and can be undeleted161 * - MGD_OBJECT_ACTION_PURGE, object has been purged ( no recovery or undelete )162 *163 */164 123 extern enum 165 124 { 166 125 MGD_OBJECT_ACTION_NONE = 0, 167 MGD_OBJECT_ACTION_DELETE, 168 MGD_OBJECT_ACTION_PURGE, 169 MGD_OBJECT_ACTION_CREATE, 170 MGD_OBJECT_ACTION_UPDATE 126 MGD_OBJECT_ACTION_DELETE, /**< Object has been deleted and can be undeleted >*/ 127 MGD_OBJECT_ACTION_PURGE, /**< Object has been purged ( no recovery or undelete ) >*/ 128 MGD_OBJECT_ACTION_CREATE, /**< Object has been created >*/ 129 MGD_OBJECT_ACTION_UPDATE /**< Object has been updated >*/ 171 130 }MidgardObjectAction; 172 131 173 /** 174 * \ingroup MgdObject 175 * 176 * Creates new MgdObject object instance. 177 * 178 * \param mgd, midgard struct connection handler 179 * \param name, object's type name 180 * \value , optional value which holds id or guid if object should 181 * be fetched from database. 182 * 183 * \return New MidgardObjectClass instance 184 * 185 * This function is mandatory one for new midgard object initialization. 186 * Unlike g_object_new ( which is typical function to create new GObject 187 * instance ), midgard_object_new initializes data which are accessible 188 * internally by object instance itself or by object's class: 189 * 190 * - midgard connection handler is associated with object 191 * - 'sitegroup' property of an object is being set 192 * 193 * Sitegroup value is returned from midgard connection handler and may 194 * be overwritten only by SG0 Midgard Administrator only when object 195 * is created. Setting this property is forbidden when object is already 196 * fetched from database. 197 * 198 * Object's contructor tries to determine third optional parameter value. 199 * If it's of G_TYPE_STRING type , then midgard_is_guid is called to check 200 * weather passed string is a guid , in any other case id property is used 201 * with G_TYPE_UINT type. 202 * 203 * Any object instance created with this function should be freed using 204 * g_object_unref function. 205 */ 206 extern MgdObject *midgard_object_new(MidgardConnection *mgd, const gchar *name, GValue *value); 132 MgdObject *midgard_object_new(MidgardConnection *mgd, const gchar *name, GValue *value); 207 133 208 /** 209 * \ingroup MgdObject 210 * 211 * DEPRECATED: use midgard_object_new with GValue instead 212 * 213 * Creates new Midgard object instance and fetch record(s) from database 214 * using object's primary property. 215 * 216 * \param mgd Midgard structure 217 * \param name name for registered type(class) 218 * \param identifier value 219 * 220 * \return new MgdObject instance 221 * 222 * The main purpose of this function is to query object's record(s) from 223 * database without any knowledge which object's property holds value of 224 * primary field and what is the type of this property. 225 * midgard_object_new_by_id also minimize the need to write full reflected 226 * code which could be used for the same purpose. 227 * 228 * Third prameter's value must be always type casted to gchar ( even if it's 229 * integer type ). Internally check for primary property and its type is made 230 * and midgard_object_get_by_id or midgard_object_get_by_guid is called to 231 * return correct object's instance. 232 * 233 * NULL is returned when object can not be queried from database. 234 * 235 */ 236 extern MgdObject *midgard_object_new_by_id(MidgardConnection *mgd, const gchar *name, gchar *id); 134 gboolean midgard_object_get_by_id(MgdObject *object, guint id); 135 gboolean midgard_object_get_by_guid(MgdObject *object, const gchar *guid); 136 gboolean midgard_object_get_by_path(MgdObject *self, const gchar *path); 237 137 238 /** 239 * \ingroup MgdObject 240 * 241 * Fetch object's record(s) from database using 'id' property. 242 * 243 * \param object, MgdObject instance 244 * \param id, object's integer identifier 245 * 246 * \return TRUE if object's record(s) is successfully fetched from database. 247 * FALSE otherwise. 248 * 249 * This is common practice to use 'id' property with integer type when table's 250 * id column stores unique value which identifies object and its record(s). 251 * However primary property with integer type is freely defined by user. 252 * 253 * MgdObject object instance must be created with midgard_object_new function. 254 * When midgard connection handler is not associated with object instance, 255 * application is terminated with 'assertion fails' error message being logged. 256 * 257 * Object instance created with this function should be freed using g_object_unref. 258 * 259 */ 260 extern gboolean midgard_object_get_by_id(MgdObject *object, guint id); 138 gboolean midgard_object_update(MgdObject *object); 139 gboolean midgard_object_create(MgdObject *object); 140 gboolean midgard_object_delete(MgdObject *object); 141 gboolean midgard_object_undelete(MidgardConnection *mgd, const gchar *guid); 142 gboolean midgard_object_purge(MgdObject *object); 261 143 262 /** 263 * \ingroup MgdObject 264 * 265 * Update object's record(s). 266 * 267 * \param object MgdObject 268 * 269 * \return TRUE when object's record(s) is successfully updated. 270 * FALSE otherwise. 271 * 272 * This function list all object's properties and creates query using 273 * properties values which are already initialized. Value of property 274 * which is not initialized is ignored. 275 * 276 * Internally such properties are being set (overwritten): 277 * 278 * - sitegroup 279 * - revisor ( object's metadata property ) 280 * - revision ( object's metadata property ) 281 * - revised ( object's metadata property ) 282 * 283 * FALSE is returned in such cases: 284 * 285 * - There is no midgard_person object associated with connection handler. 286 * - Midgard Schema is not set for this object ( this is internal fatal error ) 287 * - Object's has no storage defined. 288 * - Object has property 'name' defined and such named record already exists 289 * in object's table 290 * - Quota size is reached 291 * - Database query failed ( this is internal fatal error ) 292 * - Sitegroup property of an object was changed ( critical error ) 293 * 294 * Midgard error set by this method: 295 * 296 * - MGD_ERR_OK 297 * -# Object's record has been updated successfully. 298 * Returns TRUE 299 * - MGD_ERR_SITEGROUP_VIOLATION 300 * -# Sitegroup of an object has been chnaged 301 * - MGD_ERR_DUPLICATE 302 * -# Another object with the same name already exists in tree 303 * - MGD_ERR_QUOTA 304 * -# Quota limit is reached 305 * - MGD_ERR_INTERNAL 306 * -# SQL query failed 307 * -# No table defined for object's type 308 * - MGD_ERR_INVALID_PROPERTY_VALUE 309 * -# Some property holds incorrect value. 310 */ 311 extern gboolean midgard_object_update(MgdObject *object); 144 gchar * midgard_object_build_path(MgdObject *mobj); 312 145 313 /** 314 * \ingroup MgdObject 315 * 316 * Creates new database record(s) for object. 317 * 318 * \param object MgdObject 319 * 320 * This function list all object's properties and creates query using 321 * properties values which are already initialized. Value of property 322 * which is not initialized is set to its default value. 323 * 324 * Internally such properties are being set (overwritten): 325 * 326 * - sitegroup 327 * - guid (since Midgard 1.7.5) 328 * - id (if set as primary property) 329 * - metadata.creator (since Midgard 1.8) 330 * - metadata.created (since Midgard 1.8) 331 * - metadata.revisor (since Midgard 1.8) 332 * - metadata.revised (since Midgard 1.8) 333 * - metadata.revision (since Midgard 1.8) 334 * - metadata.published (since Midgard 1.8) 335 * 336 * Midgard error set by this method: 337 * 338 * - MGD_ERR_OK 339 * -# Object's record has been created successfully 340 * Returns TRUE 341 * - MGD_ERR_NOT_EXISTS 342 * -# Storage table not defined in schema 343 * -# SQL query failed ( database query error is populated with warn log level ) 344 * Returns FALSE 345 * - MGD_ERR_DUPLICATE 346 * -# Object with the same name already exists in tree 347 * Returns FALSE 348 * - MGD_ERR_INTERNAL 349 * -# Repligard record hasn't been created for object's record 350 * -# Critical unknown reason 351 * Returns FALSE 352 * - MGD_ERR_INVALID_PROPERTY_VALUE 353 * -# Some property holds incorrect value. ( For example property with MGD_TYPE_GUID type 354 * holds string which doesn't contain at least 21 and at most 80 characters). 355 * -# GUID and UUID reference http://www.midgard-project.org/development/mrfc/0018.html 356 * 357 * Since Midgard 1.8 , Midgard Administrator (Sitegroup 0) is able to specify 358 * sitegroup of created object. 359 */ 360 extern gboolean midgard_object_create(MgdObject *object); 146 gboolean midgard_object_is_in_parent_tree(MgdObject *object, guint rootid, guint id); 147 gboolean midgard_object_is_in_tree(MgdObject *object, guint rootid, guint id); 148 gchar * midgard_object_get_tree(MgdObject *object, GSList *tnodes); 149 const gchar *midgard_object_parent(MgdObject *object); 150 MgdObject *midgard_object_get_parent(MgdObject *self); 361 151 362 /** 363 * \ingroup MgdObject 364 * 365 * Find object(s) record(s) in database for the properties being set 366 * for object instance. 367 * 368 * \param object MgdObject 369 * \param[out] holder pointer to MidgardTypeHolder which stores number of returned objects 370 * 371 * \return objects 372 * 373 * This function is a simple wrapper for Midgard Query Builder. 374 * Unlike QB it only uses equal operators while looking for object records 375 * in database storage. It should be usefull if used with properties like 376 * up and name ( get by name ), up or parent ( list ), or similiar ones. 377 * 378 * You should free type holder structure if not longer needed. 379 * This parameter is optional. 380 * Number of objects is available as holder->elements. 381 * 382 */ 383 extern GObject **midgard_object_find(MgdObject *object, MidgardTypeHolder *holder); 152 gint midgard_object_get(MgdObject *object); 384 153 385 extern gchar * midgard_object_build_path(MgdObject *mobj); 386 387 /** 388 * \ingroup MgdObject 389 * 390 * Checks whether object exists in parent's type tree. 391 * 392 * \param object MgdObject 393 * \param rootid guint which identifies parent's type primary property 394 * \param id guint which identify object's primary property 395 * 396 * \return TRUE when object exists in parent tree, FALSE otherwise. 397 * 398 * Midgard tree in MgdSchema reference: 399 * http://www.midgard-project.org/midcom-permalink-a4e185a08fb2d0e278ef1ba3a739f77e 400 * 401 * Parent type in midgard tree is a type which can be "container" with object nodes 402 * of different types. 403 * 404 */ 405 extern gboolean midgard_object_is_in_parent_tree(MgdObject *object, guint rootid, guint id); 406 407 /** 408 * \ingroup MgdObject 409 * 410 * Checks whether object exists in tree ( of the same type ). 411 * 412 * \param object MgdObject 413 * \param rootid guint value which identifies type's primary property 414 * \param id guint value which identifies object's primary property 415 * 416 * \return TRUE when object exists in tree, FALSE otherwise. 417 * 418 * Midgard tree in MgdSchema reference: 419 * http://www.midgard-project.org/midcom-permalink-a4e185a08fb2d0e278ef1ba3a739f77e 420 * 421 * This function checks only objects of the same type. 422 * 423 */ 424 extern gboolean midgard_object_is_in_tree(MgdObject *object, guint rootid, guint id); 425 426 extern gchar * midgard_object_get_tree(MgdObject *object, GSList *tnodes); 427 428 extern MgdObject *midgard_object_get_by_old_guid(MidgardConnection *mgd, gchar *guid); 429 430 /** 431 * \ingroup MgdObject 432 * 433 * Fetch object's record(s) from database using 'guid' property. 434 * 435 * \param object, MgdObject instance 436 * \param guid, string with guid value 437 * 438 * \return TRUE if object's record(s) is successfully fetched from database. 439 * FALSE otherwise. 440 * 441 * MgdObject object instance must be created with midgard_object_new function. 442 * When midgard connection handler is not associated with object instance, 443 * application is terminated with 'assertion fails' error message being logged. 444 * 445 * Object instance created with this function should be freed using g_object_unref. 446 */ 447 extern gboolean midgard_object_get_by_guid(MgdObject *object, const gchar *guid); 448 449 /* 450 * \ingroup MgdObject 451 * 452 * Delete object's record(s) from database. 453 * 454 * \param object MgdObject 455 * 456 * \return TRUE if object's record(s) is successfully deleted from database. 457 * FALSE otherwise. 458 * 459 * Object's record is not fully deleted from database. Instead, it is marked 460 * as deleted , thus it is possible to undelete object later with static 461 * method midgard_object_undelete. 462 * 463 * If object's klass is defined as multilingual , and there is more tan one 464 * language content in database , then only particular object's content is 465 * removed. In such case object is not marked as deleted. 466 * Content itself is not removed if language is set to 0. 467 * 468 * FALSE is returned in such cases: 469 * 470 * - Midgard Schema is not set for this object ( MGD_ERR_INTERNAL ) 471 * - Object's has no storage defined ( MGD_ERR_OBJECT_NO_STORAGE ) 472 * - Object's property guid is empty ( MGD_ERR_INVALID_PROPERTY_VALUE ) 473 * - SQL query failed ( MGD_ERR_INTERNAL ) 474 * 475 */ 476 extern gboolean midgard_object_delete(MgdObject *object); 477 478 /** 479 * \ingroup MgdObject 480 * 481 * Purge object's record(s) from database. 482 * 483 * \param object MgdObject 484 * 485 * \return TRUE if object has been succesfully purged from database. 486 * FALSE otherwise. 487 * 488 * Caller is responsible to set 'guid' property before calling this function. 489 * 490 * FALSE is returned in such cases: 491 *
