~ubuntu-branches/ubuntu/hardy/steam/hardy

« back to all changes in this revision

Viewing changes to server/libraries/Visconte.pmod/Request.pmod/Result.pike

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2006-11-21 16:03:12 UTC
  • mfrom: (2.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20061121160312-nf96y6nihzsyd2uv
Tags: 2.2.31-3
Add patch to prevent inconsistent data after shutdown.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import Parser.XML.Tree;
 
2
 
 
3
 
 
4
/**
 
5
 * <p>class Result - Standard Return Type for an action, which covers the result of an action. It offers the ability to
 
6
 * render the Result either as XML or as a mapping. Further it contains a set of messages, which presents errors, 
 
7
 * warning and status of an action (if needed). 
 
8
 * Only one OWLItem or OWLContainer can be linked to one result, new set statements will override the previous ones.
 
9
 *</p>
 
10
 */
 
11
 
 
12
 
 
13
 
 
14
/**
 
15
 * <p>Rendering type for this resul</p>
 
16
 */
 
17
    public constant XML = 1;
 
18
 
 
19
 
 
20
/**
 
21
 * <p>Rendering type</p>
 
22
 */
 
23
    public constant RENDER_SINGLE = 0;
 
24
    
 
25
/**
 
26
 * <p>Rendering type</p>
 
27
 */
 
28
    public constant RENDER_DEEP = 1;
 
29
 
 
30
/**
 
31
 * <p>Rendering type</p>
 
32
 */
 
33
    public constant RENDER_UP = 2;
 
34
 
 
35
    
 
36
 
 
37
/**
 
38
 * <p>conatains one of RENDER_SINGLE(default),  RENDER_DEEP or RENDER_UP</p>
 
39
 */
 
40
    private int render_flag = 0;
 
41
    
 
42
/**
 
43
 * <p>counter for the message container</p>
 
44
 */
 
45
    private int messagecounter = 0;
 
46
 
 
47
/**
 
48
 * <p>internal flag for the result type,ITEM or CLUSTER or CONTAINER </p>
 
49
 */    
 
50
    private int result_type  = 0;    
 
51
/**
 
52
 * <p>Result has a single item</p>
 
53
 */    
 
54
    private constant ITEM  = 1;
 
55
    
 
56
/**
 
57
 * <p>Result is a cluster (more than on OWLContainer)</p>
 
58
 */    
 
59
    private constant CLUSTER  = 2;
 
60
 
 
61
/**
 
62
 * <p>Result is an OWLContainer</p>
 
63
 */    
 
64
    private constant CONTAINER  = 3;    
 
65
/**
 
66
 * <p>Container for the messages</p>
 
67
 */
 
68
    private array messages = ({ })   ;
 
69
 
 
70
/**
 
71
 * <p>Container for a set of owlitems</p>
 
72
 */
 
73
    private Visconte.Onto.OWLContainer owl_container ;
 
74
    
 
75
/**
 
76
 * <p>The single OWLItem</p>
 
77
 */
 
78
    private Visconte.Onto.Item.OWLItem owl_item ;
 
79
 
 
80
/**
 
81
 * <p>array storing more than one OWLContainer</p>
 
82
 */
 
83
    private array(array) cluster = ({  }); ;
 
84
    
 
85
    
 
86
/**
 
87
 * <p>Constructor</p>
 
88
 * 
 
89
 */
 
90
    public void create() {        
 
91
        // your code here
 
92
    } 
 
93
 
 
94
/**
 
95
 * <p>Returns an XML Representation of the Result</p>
 
96
 * Checks the result type and and returns a rendered XML-String
 
97
 * 
 
98
 * @param void|list_flags - mapping with attribute-value that can be added to the &lt;list&gt; tag
 
99
 * @return string - the rendered XML
 
100
 */
 
101
    public string get_XML_Result(void|mapping list_flags) 
 
102
    {     
 
103
            
 
104
            XMLConversion xc;
 
105
            string xml_result;
 
106
            if(list_flags)
 
107
                    xc= XMLConversion(list_flags);
 
108
            else
 
109
                    xc= XMLConversion();   
 
110
            switch(result_type)
 
111
            {
 
112
                case ITEM:
 
113
                         xml_result = xc->convert_Item(owl_item, render_flag);
 
114
                         break;
 
115
                 
 
116
                case CLUSTER:
 
117
                         xml_result = xc->convert_Cluster(cluster);
 
118
                         break;
 
119
                
 
120
                case CONTAINER:
 
121
                         xml_result = xc->convert_Container(owl_container);
 
122
                         break;
 
123
                default:
 
124
                xml_result="<list></list>";
 
125
                break;
 
126
            }
 
127
       
 
128
      return xml_result;
 
129
       
 
130
    } 
 
131
 
 
132
/**
 
133
 * <p>Returns a mapping with given tag and attribute name and the id as keys</p>
 
134
 * 
 
135
 * 
 
136
 * @param attribute - the attribute to look for
 
137
 * @param tag - the tag to look for (e.g. individual etc.) 
 
138
 * @return mapping with ids -> attributes
 
139
 */
 
140
    public mapping get_id_mapping(string attribute, string tag) 
 
141
    {    
 
142
            mapping(string:string) id_mapping=([]);
 
143
            Node root = Parser.XML.Tree.parse_input(get_XML_Result());
 
144
            array childs = root->get_descendants(0);
 
145
            foreach(childs,Node n)
 
146
            {
 
147
                    mapping attr = n->get_attributes();
 
148
                   
 
149
                    if(n->get_tag_name()==tag && has_index(attr, "id") && has_index(attr, attribute))
 
150
                    {
 
151
                            id_mapping+=([ attr["id"] : attr[attribute] ]); 
 
152
                    }
 
153
            }
 
154
            return id_mapping;
 
155
    }
 
156
    
 
157
/**
 
158
 * <p>Returns an Array with mappings that have all tags and attribute as name:value set</p>
 
159
 * 
 
160
 * @return array - containing the result mappings
 
161
 */
 
162
    public array get_result_mapping() 
 
163
    {    
 
164
            array items=({});
 
165
            Node root = Parser.XML.Tree.parse_input(get_XML_Result());
 
166
            array childs = root->get_descendants(0);
 
167
            foreach(childs,Node n)
 
168
            {
 
169
                    mapping attr = n->get_attributes();
 
170
                    attr+=([ "tag_name" : n->get_tag_name() ]);
 
171
                    items+=({attr});
 
172
            }
 
173
            return items;
 
174
    }
 
175
    
 
176
    
 
177
    
 
178
/**
 
179
 * <p>Check if this Result has an error</p>
 
180
 * 
 
181
 * 
 
182
 * @param error_num - checks for special Error number, see File MessageCodes
 
183
 * @return true or false
 
184
 */
 
185
    public int has_error(int|void error_num) 
 
186
    { 
 
187
           
 
188
            foreach(messages, Visconte.Request.Message m)
 
189
            {
 
190
                    if(m->is_error())
 
191
                    {
 
192
                            if(error_num && m->errorcode==error_num)
 
193
                            {    
 
194
                                return 1;
 
195
                                break;
 
196
                            }
 
197
                            
 
198
                            if(!error_num)
 
199
                            {
 
200
                                    return 1;
 
201
                                    break;
 
202
                            }
 
203
                    }
 
204
            }
 
205
            return 0;
 
206
    }
 
207
            
 
208
/**
 
209
 * <p>Returns the messages either as XML or as array</p>
 
210
 * 
 
211
 * 
 
212
 * @param content_type - if set to 1 XML will be rendered else the array will be
 
213
 * returned 
 
214
 * @return mixed (array or string) 
 
215
 */
 
216
    public mixed get_Messages(int content_type) 
 
217
    {        
 
218
            
 
219
       if(content_type==XML)
 
220
       {
 
221
               Node root_node = Node(Parser.XML.Tree.XML_ROOT,"root", ([ ]), "");
 
222
               mapping(string:string) attr = ([ ]);;
 
223
 
 
224
               string out="";
 
225
               foreach(messages,Visconte.Request.Message msg)
 
226
               {
 
227
                       attr+=([ "type" : (string)msg->type , "about" : msg->about , "messagecode" : (string)msg->errorcode ]);
 
228
                      
 
229
                       Node msg_node = Node(Parser.XML.Tree.XML_ELEMENT,"message", attr,"");
 
230
                       Node backtrace_node = Node(Parser.XML.Tree.XML_ELEMENT,"backtrace", ([ ]),"");
 
231
                       Node msg_text = Node(Parser.XML.Tree.XML_TEXT,"", ([ ]),msg->msg);
 
232
                       Node backtrace_text = Node(Parser.XML.Tree.XML_TEXT,"", ([ ]),msg->backtrace);
 
233
                       msg_node->add_child(msg_text);
 
234
                       msg_node->add_child(backtrace_node);
 
235
                       backtrace_node->add_child(backtrace_text);
 
236
 
 
237
                       root_node->add_child(msg_node);
 
238
                       
 
239
               }
 
240
               return root_node->render_xml();
 
241
       }
 
242
       else
 
243
       {
 
244
               return messages;
 
245
       }
 
246
    } 
 
247
  
 
248
/**
 
249
 * <p>Dumps the messages to the console</p>
 
250
 */
 
251
    public void dump_messages()
 
252
    {
 
253
           foreach(messages, Visconte.Request.Message m)
 
254
            {
 
255
                 write(m->to_string());
 
256
            }
 
257
    }
 
258
/**
 
259
 * <p>Add a message to the Result</p>
 
260
 * 
 
261
 * 
 
262
 * @param msg - a Message-object 
 
263
 */
 
264
    public void add_Message(Visconte.Request.Message msg) 
 
265
    {       
 
266
            if(msg && objectp(msg))
 
267
            {
 
268
                    messages+=({msg});
 
269
                    messagecounter++;
 
270
            }
 
271
    }
 
272
    
 
273
/**
 
274
 * <p>Add a OWLContainer to the cluster</p>
 
275
 * 
 
276
 * 
 
277
 * @param meta - mapping with attribute-value shape, will be added to the XML-
 
278
 * Representation 
 
279
 */
 
280
    public void add_cluster(mapping meta, Visconte.Onto.OWLContainer container) 
 
281
    {       
 
282
            if(container)
 
283
            {
 
284
                    cluster+=({ ({meta, container}) });
 
285
                    result_type=CLUSTER;    
 
286
            }
 
287
    }
 
288
            
 
289
/**
 
290
 * <p>Set an Visconte.Onto.OWLContainer to the Result. This overwrites any 
 
291
 * existing Visconte.Onto.OWLContainer or/and Visconte.Onto.Item.OWLItem</p>
 
292
 * 
 
293
 * @param container - the Visconte.Onto.OWLContainer 
 
294
 */
 
295
    public void set_OWLContainer(Visconte.Onto.OWLContainer container) 
 
296
    {        
 
297
            if(container)
 
298
            {
 
299
                    //if a Visconte.Onto.OWLContainer is added, null an existing Visconte.Onto.Item.OWLItem
 
300
                    this_program::owl_container=container;
 
301
                    result_type=CONTAINER;  
 
302
            }
 
303
            
 
304
                    
 
305
    } 
 
306
    
 
307
/**
 
308
 * <p>Set an Visconte.Onto.Item.OWLItem to the Result. This overwrites any 
 
309
 * existing Visconte.Onto.OWLContainer or/and Visconte.Onto.Item.OWLItem</p>
 
310
 * 
 
311
 * 
 
312
 * @param item - The OWLitem 
 
313
 */
 
314
    public void set_OWLItem(Visconte.Onto.Item.OWLItem item, int|void render_flag) 
 
315
    {        
 
316
            if(item)
 
317
            {
 
318
                    //if a OWLItem is added, null an existing Visconte.Onto.OWLContainer
 
319
                    this_program::owl_item=item;
 
320
                    result_type=ITEM;  
 
321
                    if(render_flag)
 
322
                            this_program::render_flag=render_flag;
 
323
                    else
 
324
                            this_program::render_flag=RENDER_SINGLE;
 
325
                   
 
326
            }
 
327
            
 
328
                    
 
329
    } 
 
330
    
 
331
    
 
332
    
 
333
    
 
334
/**
 
335
 *    class XMLConversion - class to translate OWLItems to the visconte exchange format
 
336
 *
 
337
 */
 
338
 
 
339
 
 
340
    
 
341
class XMLConversion
 
342
{
 
343
        import Visconte;
 
344
        import Visconte.Onto.Item;
 
345
        import Parser.XML.Tree;
 
346
        
 
347
 
 
348
/**
 
349
 * <p>the Model Tag</p>
 
350
 */
 
351
        public constant MODEL_NODE = "model";
 
352
/**
 
353
 * <p>The List Tag</p>
 
354
 */
 
355
        public constant LIST = "list";
 
356
/**
 
357
 * <p>The DatatypeProperty tag</p>
 
358
 */
 
359
        public constant DATA_TYPE_NODE = "property";
 
360
/**
 
361
 * <p>The ObjectProperty Tag </p>
 
362
 */
 
363
        public constant OBJECT_PROPERTY_NODE = "association";
 
364
/**
 
365
 * <p>The individual tag</p>
 
366
 */
 
367
        public constant INDIVIDUAL_NODE = "individual";
 
368
/**
 
369
 * <p>The cluser tag</p>
 
370
 */
 
371
        public constant CLUSTER_NODE = "cluster";
 
372
/**
 
373
 * <p>Tag for any attribute (property or associaion) of an individual</p>
 
374
 */
 
375
        public constant INDIVIDUAL_PROPERTY = "attribute";
 
376
 
 
377
/**
 
378
 * <p>Attributes for the list tag</p>
 
379
 */
 
380
        public mapping(string:string) list_flag =([]);  
 
381
 
 
382
/**
 
383
 * <p>Set an Visconte.Onto.OWLContainer to the Result. This overwrites any 
 
384
 * existing Visconte.Onto.OWLContainer or/and OWLItem</p>
 
385
 * 
 
386
 * @param container - the Visconte.Onto.OWLContainer 
 
387
 */     
 
388
        public string create(void|mapping list_flag)
 
389
        {       
 
390
                if(list_flag)
 
391
                this_program::list_flag=list_flag;
 
392
        }
 
393
/**
 
394
 * <p>Renders a cluster as XML</p>
 
395
 * 
 
396
 * @param clusters - the array containing the OWLContainer 
 
397
 */     
 
398
        public string convert_Cluster(array clusters)
 
399
        {
 
400
                Node xml_cluster_root = Node(XML_ROOT,"root",([]),"");
 
401
                foreach(clusters, array cluster)
 
402
                {
 
403
                        Node cluster_node = Node(XML_ELEMENT,CLUSTER_NODE,cluster[0],"");
 
404
                        xml_cluster_root->add_child(cluster_node);
 
405
                        Visconte.Onto.OWLContainer container  = cluster[1];
 
406
                        while(container->has_items())
 
407
                                cluster_node->add_child(create_Item_Node(container->next(), 0) );
 
408
                }
 
409
                return xml_cluster_root->render_xml();
 
410
        }
 
411
        
 
412
        
 
413
/**
 
414
 * <p>Render a OWLContainer as XML</p>
 
415
 * 
 
416
 * @param container - the Visconte.Onto.OWLContainer 
 
417
 */     
 
418
        public string convert_Container(Visconte.Onto.OWLContainer container)
 
419
        {
 
420
                container->reset_iterator();
 
421
                list_flag->size = (string)container->size_of();
 
422
                Node xml_container = Node(XML_ELEMENT,LIST,list_flag,"");
 
423
                while(container->has_items())
 
424
                {
 
425
                        Visconte.Onto.Item.OWLItem x =container->next();
 
426
                        xml_container->add_child(create_Item_Node(x,0));
 
427
                }
 
428
                return xml_container->render_xml();
 
429
        }
 
430
 
 
431
/**
 
432
 * <p>Render a OWLItem as XML</p>
 
433
 * 
 
434
 * @param item - the OWLItem
 
435
 * @param render_flag - the flags: RENDER_SINGLE(default),  RENDER_DEEP or RENDER_UP
 
436
 */     
 
437
        public string convert_Item(Visconte.Onto.Item.OWLItem item, int render_flag)
 
438
        {
 
439
                return  create_Item_Node(item,render_flag)->render_xml();
 
440
        }
 
441
 
 
442
 
 
443
/**
 
444
 * <p>Creates a Node for an OWLItem/p>
 
445
 * @param item - the OWLItem
 
446
 * @param render_flag - the flags: RENDER_SINGLE(default),  RENDER_DEEP or RENDER_UP
 
447
 */     
 
448
        private  Node create_Item_Node(Visconte.Onto.Item.OWLItem item, int render_flag)
 
449
        {
 
450
                 switch(item->ITEMTYPE)
 
451
                 {
 
452
                        case Visconte.Onto.Item.OWLItem.CLASS:
 
453
                         
 
454
                        return create_Class_Node(item, render_flag);
 
455
                        break;
 
456
                        
 
457
                        case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
 
458
                        return create_Object_Property_Node(item);
 
459
                        break;
 
460
                        
 
461
                        case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
 
462
                        return create_Datatype_Property_Node(item);
 
463
                        break;
 
464
                        
 
465
                        case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
 
466
                        return create_Individual_Node(item);
 
467
                        break;
 
468
                        
 
469
                        default:
 
470
                         return Node(XML_ELEMENT,MODEL_NODE,([]),"");
 
471
                        break;
 
472
                 }
 
473
        }
 
474
        
 
475
        
 
476
/**
 
477
 * <p>this method creates an Class Node in visconte format</p>
 
478
 * 
 
479
 * @param item - the OWLClass
 
480
 * @param render_flag - the flags: RENDER_SINGLE(default),  RENDER_DEEP or RENDER_UP
 
481
 */     
 
482
        private  Node create_Class_Node(Visconte.Onto.Item.OWLItem item, int render_flag)
 
483
        {       
 
484
                mapping(string:string) attr = ([ ]);;
 
485
                Node model_node;
 
486
                attr+=(["id": ID(item->get_Id(),1)->uref]);
 
487
                attr+=(["label" : item->get_Label()]);
 
488
                attr+=(["weight" : (string) item->get_weight(5)]);
 
489
                attr+=(["drops" : (string) item->get_weight()]);
 
490
                attr+=(["lastModified" : (string) item->get_date_of_change() ]);
 
491
                if(item->is_document_term())
 
492
                        attr+=(["isDocumentTerm" : "true"]);
 
493
                else
 
494
                        attr+=(["isDocumentTerm" : "false"]);
 
495
                
 
496
                
 
497
                model_node = Node(XML_ELEMENT,MODEL_NODE,attr,"");
 
498
                
 
499
                Visconte.Onto.OWLContainer container = item->get_domain_properties();
 
500
                
 
501
                while(container->has_items())
 
502
                {
 
503
                        model_node->add_child(create_Item_Node(container->next(),render_flag));
 
504
                }
 
505
                if(render_flag==Visconte.Request.Result.RENDER_DEEP)
 
506
                {
 
507
                        container = item->get_childs();
 
508
                        while(container->has_items())
 
509
                        {
 
510
                                model_node->add_child(create_Item_Node(container->next(),render_flag));
 
511
                        }
 
512
                }
 
513
                if(render_flag==Visconte.Request.Result.RENDER_UP)
 
514
                {
 
515
                        
 
516
                        if(item->has_parent())
 
517
                        {
 
518
                                item = item->get_parent();
 
519
                                {
 
520
                                        Node parent = create_Item_Node(item,render_flag);
 
521
                                        parent->add_child(model_node);
 
522
                                        return parent;
 
523
                                }
 
524
                        }
 
525
                }
 
526
                                
 
527
                return  model_node;
 
528
                
 
529
                
 
530
        }
 
531
/**
 
532
 * <p>this method creates an ObjectProperty Node  in visconte format</p>
 
533
 * 
 
534
 * @param item - the OWLObjectProperty 
 
535
 */     
 
536
        private  Node create_Object_Property_Node(Visconte.Onto.Item.OWLItem item)
 
537
        {       
 
538
                mapping(string:string) attr = ([ ]);
 
539
                attr+=(["id" : ID(item->get_Id(),1)->uref]);
 
540
                attr+=(["label" : item->get_Label()]);
 
541
                attr+=(["with" : ID(item->get_range()->get_Id())->uref]);
 
542
                attr+=(["domain" : ID(item->get_domain()->get_Id())->uref]);
 
543
                attr+=(["lastModified" : (string)  item->get_date_of_change()]);
 
544
                return  Node(XML_ELEMENT,OBJECT_PROPERTY_NODE,attr,"");
 
545
        }
 
546
/**
 
547
 * <p>this method creates an DatatypeProperty Node  in visconte format</p>
 
548
 * 
 
549
 * @param item - the OWLDatatypeProperty 
 
550
 */     
 
551
        private  Node create_Datatype_Property_Node(Visconte.Onto.Item.OWLItem item)
 
552
        {       
 
553
                mapping(string:string) attr = ([ ]);
 
554
                attr+=(["id" : ID(item->get_Id(),1)->uref]);
 
555
                attr+=(["label" : item->get_Label()]);
 
556
                attr+=(["type" : item->get_range()]);
 
557
                attr+=(["lastModified" : (string)  item->get_date_of_change()]);
 
558
                return  Node(XML_ELEMENT,DATA_TYPE_NODE,attr,"");
 
559
        }       
 
560
        
 
561
 
 
562
/**
 
563
 * <p>this method creates an DatatypeProperty Node  in visconte format</p>
 
564
 * 
 
565
 * @param item - the OWLDatatypeProperty 
 
566
 */     
 
567
        private  Node create_Individual_Node(Visconte.Onto.Item.OWLItem item)
 
568
        {       
 
569
                Node individual;
 
570
                mapping(string:string) attr = ([ ]);
 
571
                
 
572
                attr+=(["id" : ID(item->get_Id(),1)->uref]);
 
573
                attr+=(["label" : item->get_Label()]);
 
574
                attr+=(["domain" : ID(item->get_model_class()->get_Id())->uref]);
 
575
                attr+=(["domainName" : item->get_model_class()->get_Label()]);
 
576
                attr+=(["lastModified" : (string)  item->get_date_of_change()]);
 
577
                if(item->is_document_term())
 
578
                {
 
579
                        mapping steam_properties = item->get_steam_properties();
 
580
                        attr+=(["objectID" : (string)  steam_properties->objectid]);
 
581
                        attr+=(["mimetype" : (string)  steam_properties->mimetype]);
 
582
                }
 
583
                
 
584
                individual = Node(XML_ELEMENT,INDIVIDUAL_NODE,attr,"");
 
585
                array property_map = item->get_property_map();
 
586
                foreach(property_map, array property_definition)
 
587
                {
 
588
                        OWLProperty property = property_definition[0];
 
589
                        string value = property_definition[1];
 
590
                        mapping(string:string) attr = ([ ]);
 
591
                        attr+=(["propertyId" : ID(property->get_Id(),1)->uref ]);
 
592
                        
 
593
                        attr+=(["label" : property->get_Label() ]);
 
594
                        
 
595
                        if(property->ITEMTYPE==Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY)
 
596
                        {
 
597
                                attr+=(["range" : ID(property->get_range()->get_Id(),1)->uref ]);
 
598
                                attr+=(["type" : "object"]);
 
599
                                attr+=(["value" : ID(value)->uref ]);
 
600
 
 
601
                        }
 
602
                        else
 
603
                        {
 
604
                                
 
605
                                attr+=(["range" : property->get_range() ]);
 
606
                                attr+=(["type" : "data"]);
 
607
                                attr+=(["value" : (string) value]);
 
608
                        }
 
609
                        
 
610
                        individual->add_child(Node(XML_ELEMENT,INDIVIDUAL_PROPERTY,attr,""));
 
611
                }
 
612
                return  individual;
 
613
        }       
 
614
}