src/midgard_core_object_class.c

00001 /* 
00002  * Copyright (C) 2006 Piotr Pokora <piotrek.pokora@gmail.com>
00003  *
00004  * This program is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License as published
00006  * by the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #include "midgard_core_object_class.h"
00020 #include "midgard/midgard_error.h"
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include "midgard/midgard_reflection_property.h"
00024 
00025 #define get_varchar_size(str) \
00026                 if(str == NULL) \
00027                 size++; \
00028         else \
00029                 size = strlen((const gchar *)str) + 1 + size; \
00030         g_free(str);
00031 
00032 #define get_datetime_size(str) \
00033                 size = 8 + size; \
00034         g_free(str);
00035 
00036 
00037 gboolean _midgard_core_object_is_valid(MgdObject *object)
00038 {
00039         guint propn, i, size = 0;
00040         GType prop_type;
00041         gchar *prop_str;
00042         const gchar *blobdir;
00043         gchar *blobpath, *location;
00044         struct stat statbuf; /* GLib 2.6 is not available everywhere, g_stat not used */
00045         const gchar *typename = G_OBJECT_TYPE_NAME(object);
00046         GParamSpec **props = g_object_class_list_properties(
00047                         G_OBJECT_GET_CLASS(G_OBJECT(object)), &propn);
00048         if(!props){
00049                 midgard_set_error(object->mgd->_mgd,
00050                                 MGD_GENERIC_ERROR,
00051                                 MGD_ERR_INVALID_OBJECT,
00052                                 "Object %s has no properties",
00053                                 G_OBJECT_TYPE_NAME(object));
00054                 g_warning("%s", object->mgd->_mgd->err->message);
00055                 g_clear_error(&object->mgd->_mgd->err);
00056                 g_free(props);
00057                 return FALSE; 
00058         }
00059         
00060         MidgardObjectClass *klass = MIDGARD_OBJECT_GET_CLASS(object);
00061 
00062         /* Attachment, we need to get file size */
00063         if(g_str_equal(typename, "midgard_attachment")){
00064                 
00065                 /* FIXME , remove legacy blobdir get */
00066                 blobdir = mgd_get_blobdir(object->mgd);
00067                 
00068                 if (!blobdir || (*blobdir != '/')
00069                                 || (stat(blobdir, &statbuf) != 0)
00070                                 || !S_ISDIR(statbuf.st_mode)) {
00071                         g_free(props);
00072                         g_error("Invalid blobdir or blobdir is not set");
00073                 }
00074                 g_object_get(G_OBJECT(object), "location", &location, NULL);
00075 
00076                 /* FIXME, get blobdir location from MidgardConfig */
00077                 if(strlen(location) > 1) {
00078                         blobpath = g_strconcat(
00079                                         blobdir,
00080                                         "/",
00081                                         location,
00082                                         NULL);
00083                         
00084                         if((stat(blobpath, &statbuf) == 0) || (!S_ISDIR(statbuf.st_mode)))
00085                                 size = statbuf.st_size;
00086                         g_free(blobpath);
00087                         g_free(location);
00088                 }
00089         }
00090 
00091         MidgardReflectionProperty *mrp = 
00092                 midgard_reflection_property_new(klass);
00093         
00094         for(i = 0; i < propn; i++){
00095                 prop_type = 
00096                         midgard_reflection_property_get_midgard_type(
00097                                         mrp,
00098                                         props[i]->name);
00099                                 
00100                 if(prop_type == MGD_TYPE_GUID) {
00101                         /* I think empty string should be set if prop_str is NULL.
00102                          * assertion failed should be triggered , but we should 
00103                          * consider language bindings ( especially PHP ) */
00104                         g_object_get(G_OBJECT(object), props[i]->name,
00105                                         &prop_str, NULL);
00106                         if(prop_str == NULL) prop_str = g_strdup("");
00107                         if(!midgard_is_guid(prop_str)){
00108                                 midgard_set_error(object->mgd->_mgd,
00109                                                 MGD_GENERIC_ERROR,
00110                                                 MGD_ERR_INVALID_PROPERTY_VALUE,
00111                                                 "'%s' property's value is not a guid. ",
00112                                                 props[i]->name);
00113                                 g_warning("%s", object->mgd->_mgd->err->message);
00114                                 g_clear_error(&object->mgd->_mgd->err);
00115                                 g_free(props);
00116                                 g_free(prop_str);
00117                                 return FALSE;
00118                         }
00119                         get_varchar_size(prop_str);
00120                 
00121                 } else if(prop_type == MGD_TYPE_LONGTEXT){
00122                         g_object_get(G_OBJECT(object), props[i]->name,
00123                                         &prop_str, NULL);                       
00124                         if(prop_str == NULL) prop_str = g_strdup("");
00125                         size = (strlen(prop_str)) + 4 + size;
00126                         g_free(prop_str);
00127                 
00128                 } else if(prop_type == MGD_TYPE_TIMESTAMP) {
00129                         g_object_get(G_OBJECT(object), props[i]->name,
00130                                         &prop_str, NULL);
00131                         /* TODO, Check if timestamp value is valid */
00132                         size =  8 + size;
00133                         g_free(prop_str);
00134                 
00135                 } else if(prop_type == MGD_TYPE_STRING) {
00136                         g_object_get(G_OBJECT(object), props[i]->name,
00137                                         &prop_str, NULL);
00138                         get_varchar_size(prop_str);
00139 
00140                 } else if(prop_type == MGD_TYPE_UINT 
00141                                 && prop_type == MGD_TYPE_INT
00142                                 && prop_type == MGD_TYPE_FLOAT
00143                                 && prop_type == MGD_TYPE_BOOLEAN){
00144                         size = size + 4;
00145                 
00146                 } else {
00147                         /* Do nothing now */
00148                 }
00149         }
00150         g_free(props);
00151 
00152         /* Object metadata */
00153         /* creator */
00154         g_object_get(G_OBJECT(object->metadata), "creator", &prop_str, NULL);
00155         get_varchar_size(prop_str);
00156         /* created */
00157         g_object_get(G_OBJECT(object->metadata), "created", &prop_str, NULL);
00158         get_datetime_size(prop_str);
00159         /* revisor */
00160         g_object_get(G_OBJECT(object->metadata), "revisor", &prop_str, NULL);
00161         get_varchar_size(prop_str);
00162         /* revised */
00163         g_object_get(G_OBJECT(object->metadata), "revised", &prop_str, NULL);
00164         get_datetime_size(prop_str);
00165         /* locker */
00166         g_object_get(G_OBJECT(object->metadata), "locker", &prop_str, NULL);
00167         get_varchar_size(prop_str);
00168         /* locked */
00169         g_object_get(G_OBJECT(object->metadata), "locked", &prop_str, NULL);
00170         get_datetime_size(prop_str);
00171         /* approver */
00172         g_object_get(G_OBJECT(object->metadata), "approved", &prop_str, NULL);
00173         get_varchar_size(prop_str);
00174         /* approved */
00175         g_object_get(G_OBJECT(object->metadata), "approved", &prop_str, NULL);
00176         get_datetime_size(prop_str);
00177         /* published */
00178         g_object_get(G_OBJECT(object->metadata), "published", &prop_str, NULL);
00179         get_datetime_size(prop_str);
00180         /* authors */
00181         g_object_get(G_OBJECT(object->metadata), "authors", &prop_str, NULL);
00182         if(prop_str == NULL)
00183                 prop_str = g_strdup("");
00184         size = strlen(prop_str) + 4 + size;
00185         g_free(prop_str);
00186         
00187         /* owner */
00188         g_object_get(G_OBJECT(object->metadata), "owner", &prop_str, NULL);
00189         get_varchar_size(prop_str);
00190         /* schedule-start */
00191         g_object_get(G_OBJECT(object->metadata), "schedulestart", &prop_str, NULL);
00192         get_datetime_size(prop_str);
00193         /* schedule-end */
00194         g_object_get(G_OBJECT(object->metadata), "scheduleend", &prop_str, NULL);
00195         get_datetime_size(prop_str);
00196         /* hidden, nav-noentry , revision ( 3 x 4 ) */
00197         size = size + 12;
00198         
00199         g_object_set(G_OBJECT(object->metadata), "size", size, NULL);
00200 
00201         return TRUE;
00202 }

Generated on Thu Feb 22 06:15:17 2007 for midgard-core by  doxygen 1.4.6