location.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2007-2013 by Johan De Taeye, frePPLe bvba *
4  * *
5  * This library is free software; you can redistribute it and/or modify it *
6  * under the terms of the GNU Affero General Public License as published *
7  * by the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU Affero General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Affero General Public *
16  * License along with this program. *
17  * If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  ***************************************************************************/
20 
21 #define FREPPLE_CORE
22 #include "frepple/model.h"
23 
24 namespace frepple
25 {
26 
27 template<class Location> DECLARE_EXPORT Tree utils::HasName<Location>::st;
30 
31 
33 {
34  // Initialize the metadata
35  metadata = new MetaCategory("location", "locations", reader, writer);
36 
37  // Initialize the Python class
39 }
40 
41 
43 {
44  // Initialize the metadata
45  LocationDefault::metadata = new MetaClass("location", "location_default",
46  Object::createString<LocationDefault>, true);
47 
48  // Initialize the Python class
50 }
51 
52 
54 {
55  // Writing a reference
56  if (m == REFERENCE)
57  {
59  return;
60  }
61 
62  // Write the head
63  if (m != NOHEAD && m != NOHEADTAIL)
65 
66  // Write the fields
67  HasDescription::writeElement(o, tag);
69  o->writeElement(Tags::tag_available, available);
70 
71  // Write the tail
72  if (m != NOHEADTAIL && m != NOTAIL) o->EndObject(tag);
73 }
74 
75 
77 {
78  if (pAttr.isA(Tags::tag_available) || pAttr.isA(Tags::tag_maximum))
80  else
82 }
83 
84 
85 DECLARE_EXPORT void Location::endElement(XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement)
86 {
87  if (pAttr.isA(Tags::tag_available))
88  {
89  CalendarDouble *cal = dynamic_cast<CalendarDouble*>(pIn.getPreviousObject());
90  if (cal)
91  setAvailable(cal);
92  else
93  {
94  Calendar *c = dynamic_cast<Calendar*>(pIn.getPreviousObject());
95  if (!c)
96  throw LogicException("Incorrect object type during read operation");
97  throw DataException("Calendar '" + c->getName() +
98  "' has invalid type for use as location calendar");
99  }
100  }
101  else
102  {
103  HasDescription::endElement(pIn, pAttr, pElement);
104  HasHierarchy<Location>::endElement(pIn, pAttr, pElement);
105  }
106 }
107 
108 
110 {
111  // Remove all references from buffers to this location
112  for (Buffer::iterator buf = Buffer::begin();
113  buf != Buffer::end(); ++buf)
114  if (buf->getLocation() == this) buf->setLocation(NULL);
115 
116  // Remove all references from resources to this location
117  for (Resource::iterator res = Resource::begin();
118  res != Resource::end(); ++res)
119  if (res->getLocation() == this) res->setLocation(NULL);
120 
121  // Remove all references from operations to this location
122  for (Operation::iterator oper = Operation::begin();
123  oper != Operation::end(); ++oper)
124  if (oper->getLocation() == this) oper->setLocation(NULL);
125 }
126 
127 
129 {
130  if (attr.isA(Tags::tag_name))
131  return PythonObject(getName());
132  if (attr.isA(Tags::tag_description))
133  return PythonObject(getDescription());
134  if (attr.isA(Tags::tag_category))
135  return PythonObject(getCategory());
136  if (attr.isA(Tags::tag_subcategory))
137  return PythonObject(getSubCategory());
138  if (attr.isA(Tags::tag_owner))
139  return PythonObject(getOwner());
140  if (attr.isA(Tags::tag_available))
141  return PythonObject(getAvailable());
142  if (attr.isA(Tags::tag_hidden))
143  return PythonObject(getHidden());
144  if (attr.isA(Tags::tag_members))
145  return new LocationIterator(this);
146  return NULL;
147 }
148 
149 
151 {
152  if (attr.isA(Tags::tag_name))
153  setName(field.getString());
154  else if (attr.isA(Tags::tag_description))
155  setDescription(field.getString());
156  else if (attr.isA(Tags::tag_category))
157  setCategory(field.getString());
158  else if (attr.isA(Tags::tag_subcategory))
159  setSubCategory(field.getString());
160  else if (attr.isA(Tags::tag_owner))
161  {
162  if (!field.check(Location::metadata))
163  {
164  PyErr_SetString(PythonDataException, "location owner must be of type location");
165  return -1;
166  }
167  Location* y = static_cast<Location*>(static_cast<PyObject*>(field));
168  setOwner(y);
169  }
170  else if (attr.isA(Tags::tag_available))
171  {
172  if (!field.check(CalendarDouble::metadata))
173  {
174  PyErr_SetString(PythonDataException, "location availability must be of type double calendar");
175  return -1;
176  }
177  CalendarDouble* y = static_cast<CalendarDouble*>(static_cast<PyObject*>(field));
178  setAvailable(y);
179  }
180  else if (attr.isA(Tags::tag_hidden))
181  setHidden(field.getBool());
182  else
183  return -1;
184  return 0;
185 }
186 
187 } // end namespace