ContainerFactory._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2005 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * $Id: ContainerFactory.pike,v 1.2 2010/08/18 20:32:45 astra Exp $
18  */
19 inherit "/factories/ObjectFactory";
20 #include <macros.h>
21 #include <classes.h>
22 #include <access.h>
23 #include <database.h>
24 #include <events.h>
25 #include <attributes.h>
26 #include <types.h>
27 //! This factory creates intances of the class Container.
28 class ContainerFactory : public ObjectFactory{
29 public:
30 
31 
32 
33 
34 
35 import Attributes;
36 
37 
38 protected:
39  void
40 private:
41 init_factory()
42 {
43  ::init_factory();
44  register_class_attribute(Attribute(
45  CONT_SIZE_X, "x-size", CMD_TYPE_FLOAT,
46  0.0, 0, CONTROL_ATTR_CLIENT));
47  register_class_attribute(Attribute(
48  CONT_SIZE_Y, "y-size", CMD_TYPE_FLOAT,
49  0.0, 0, CONTROL_ATTR_CLIENT));
50  register_class_attribute(Attribute(
51  CONT_SIZE_Z, "z-size", CMD_TYPE_FLOAT,
52  0.0, 0, CONTROL_ATTR_CLIENT));
53  register_class_attribute(Attribute(
54  CONT_EXCHANGE_LINKS, "exchange links",
55  CMD_TYPE_INT, 0, // link exchange turned off
56  REG_ACQ_ENVIRONMENT, CONTROL_ATTR_USER));
57  register_class_attribute(Attribute(
58  CONT_WSDL, "Container WSDL Description",
59  CMD_TYPE_OBJECT, 0,
60  REG_ACQ_ENVIRONMENT, CONTROL_ATTR_USER));
61 }
62 
63 public:
64 
65 /**
66  * Execute this Container factory to get a new container object.
67  * The vars mapping takes indices: "name", "attributes","attributesAcquired",
68  * and "attributesLocked".
69  *
70  * @param mapping vars - execute vars, especially the containers name.
71  * @return proxy of the newly created container.
72  */
73 object execute(mapping vars)
74 {
75  object obj;
76  string name = vars["name"];
77  try_event(EVENT_EXECUTE, CALLER, obj);
78  if ( vars->transient ) {
79  if ( mappingp(vars->attributes) )
80  vars->attributes[OBJ_TEMP] = 1;
81  else
82  vars->attributes = ([ OBJ_TEMP : 1 ]);
83  }
84 
85  obj = ::object_create(
86  name, CLASS_NAME_CONTAINER, 0, vars["attributes"],
87  vars["attributesAcquired"], vars["attributesLocked"],
88  vars["sanction"],
89  vars["sanctionMeta"]);
90 
91  run_event(EVENT_EXECUTE, CALLER, obj);
92 }
93 
94 int change_object_to_container ( object obj ) {
95  if ( !_SECURITY->access_write(0, obj, CALLER) )
96  return 0;
97 
98  string old_class = obj->get_class();
99  if ( old_class == CLASS_NAME_CONTAINER )
100  return 0; // obj already is a container
101  if ( !(old_class == CLASS_NAME_ROOM) )
102  steam_error("Class "+old_class+" cannot be changed to a container, only"
103  +" rooms can !");
104 
105  steam_error( "Rooms cannot be changed to containers, yet." );
106 
107  //TODO: check for invalid contents for containers (e.g. users, exits
108  // or other rooms) and remove them. Also recurse through all sub-rooms
109  // and change them to containers, too.
110 
111  if ( _Persistence->change_object_class(obj, CLASS_NAME_CONTAINER) ) {
112  obj->set_attribute( OBJ_LAST_CHANGED, query_attribute(FACTORY_LAST_REGISTER)-1 );
113  call(obj->drop, 0.0);
114  return 1;
115  }
116  return 0;
117 }
118 
119 string get_identifier() { return "Container.factory"; }
120 string get_class_name() { return "Container"; }
121 int get_class_id() { return CLASS_CONTAINER; }
122 
123 void test () {
124  object cont = execute( ([ "name": "testcontainer", ]) );
125  object doc1 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test1", ]));
126  object doc2 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test2", ]));
127 
128  doc1->move(cont);
129  Test.test("Inventory",
130  doc1->get_environment() == cont);
131  Test.test("Inventory 2",
132  search(cont->get_inventory(), doc1) >= 0 );
133 
134  cont->add_annotation(doc2);
135 
136  Test.test("Anotations Annotating",
137  doc2->get_annotating() == cont);
138  Test.test("Annotation available",
139  search(cont->get_annotations(), doc2) >= 0);
140 
141  object dup = cont->duplicate(1);
142 
143  Test.test("Duplicate Container",
144  dup->get_identifier() == "testcontainer");
145  Test.test("Duplicated Inventory",
146  sizeof(dup->get_inventory()) != 0);
147  Test.test("Duplicated Annotation",
148  sizeof(dup->get_annotations()) > 0);
149 
150  object doc3 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test3", ]));
151  doc3->move(cont);
152  doc3->set_attribute(OBJ_NAME, "test1");
153  Test.test("Identifier should be unique after rename!",
154  doc3->get_identifier() == doc3->get_object_id() + "__test1");
155  Test.test("OBJ_NAME should be changed after rename!",
156  doc3->query_attribute(OBJ_NAME) == "test1");
157 
158  object doc4 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test1",
159  "move": cont,]));
160  Test.test("Create with non-unique identifier in cont should make unique",
161  doc4->get_identifier() != "test1");
162 
163  object doc5 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test1", ]));
164  doc5->move(cont);
165  Test.test("Name should change on move!", doc5->get_identifier() != "test1");
166  Test.test("OBJ_NAME should be the same after move!",
167  doc5->query_attribute(OBJ_NAME) == "test1");
168 }
169 
170 
171 };