AbstractEntityWrapper.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * 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, see <http://www.gnu.org/licenses/>.
15 *
16 * @package MemoryX::Core
17 * @author ALexey Kozlov ( kozlov at kit dot edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "AbstractEntityWrapper.h"
24 
26 
27 #include "../GridFileManager.h"
28 #include "../MongoDBRef.h"
29 
31 {
33  {
34  }
35 
37  {
38  }
39 
40  std::string
42  {
43  return entity ? entity->getId() : "";
44  }
45 
46  std::string
48  {
49  return entity ? entity->getName() : "";
50  }
51 
52  EntityBasePtr
54  {
55  return entity;
56  }
57 
58  void
59  AbstractEntityWrapper::setEntity(const EntityBasePtr& entity)
60  {
61  this->entity = entity;
62  }
63 
64  //void AbstractEntityWrapper::setEntity(EntityBase* entity)
65  //{
66  // this->entity = entity;
67  //}
68 
69  void
70  AbstractFileEntityWrapper::cleanUpAttributeFiles(memoryx::EntityAttributeBasePtr oldAttr,
71  memoryx::EntityAttributeBasePtr newAttr)
72  {
73  if (!oldAttr || !newAttr)
74  {
75  return;
76  }
77 
78  int size = oldAttr->size();
79  int newSize = newAttr->size();
80 
81  for (int i = 0; i < size; ++i)
82  {
83  armarx::VariantPtr oldMongoRef =
84  armarx::VariantPtr::dynamicCast(oldAttr->getValueAt(i));
85 
86  std::string oldDocId;
87 
88  if (oldMongoRef)
89  {
90  oldDocId = oldMongoRef->getClass<MongoDBRef>()->docId;
91  }
92 
93  std::string newDocId;
94 
95  if (i < newSize)
96  {
97  armarx::VariantPtr newMongoRef =
98  armarx::VariantPtr::dynamicCast(newAttr->getValueAt(i));
99  newDocId = newMongoRef->get<MongoDBRef>()->docId;
100  }
101 
102  if (i >= newSize || (!oldDocId.empty() && newDocId != oldDocId))
103  {
104  ARMARX_INFO_S << "Removing old file: " << oldAttr->getName();
105  fileManager->removeAttrFile(oldAttr, i);
106  }
107  }
108  }
109 
110  void
111  AbstractFileEntityWrapper::removeAttributeFiles(const memoryx::EntityAttributeBasePtr& attr)
112  {
113  if (!attr)
114  {
115  return;
116  }
117 
118  int size = attr->size();
119  for (int i = 0; i < size; ++i)
120  {
121  armarx::VariantPtr oldMongoRef = armarx::VariantPtr::dynamicCast(attr->getValueAt(i));
122 
123  std::string oldDocId;
124 
125  if (oldMongoRef)
126  {
127  oldDocId = oldMongoRef->getClass<MongoDBRef>()->docId;
128  if (!oldDocId.empty())
129  {
130  ARMARX_INFO_S << "Removing file: " << attr->getName();
131  fileManager->removeAttrFile(attr, i);
132  }
133  }
134  }
135  }
136 
137  std::string
139  bool preserveOriginalFName /* = false */) const
140  {
141  std::string result = "";
142 
143  if (getEntity()->hasAttribute(attrName))
144  {
145  fileManager->ensureFileInCache(
146  getEntity()->getAttribute(attrName), result, preserveOriginalFName);
147  }
148 
149  return result;
150  }
151 
153  fileManager(fileManager)
154  {
155  }
156 } // namespace memoryx::EntityWrappers
memoryx::EntityWrappers::AbstractFileEntityWrapper::removeAttributeFiles
void removeAttributeFiles(const memoryx::EntityAttributeBasePtr &attr)
removes all files of an attribute from the gridfs
Definition: AbstractEntityWrapper.cpp:111
memoryx::EntityWrappers::AbstractFileEntityWrapper::cleanUpAttributeFiles
void cleanUpAttributeFiles(EntityAttributeBasePtr oldAttr, EntityAttributeBasePtr newAttr)
cleanUpAttributeFiles compares the files attached to the two given attributes and removes the files o...
Definition: AbstractEntityWrapper.cpp:70
memoryx::MongoDBRef
Represents a cross-database reference to a document in MongoDB.
Definition: MongoDBRef.h:45
memoryx::EntityWrappers::AbstractEntityWrapper::getEntity
EntityBasePtr getEntity() const
Get the stored name of the stored entity.
Definition: AbstractEntityWrapper.cpp:53
memoryx::EntityWrappers::AbstractEntityWrapper::AbstractEntityWrapper
AbstractEntityWrapper()
Definition: AbstractEntityWrapper.cpp:32
IceInternal::Handle< Variant >
memoryx::EntityWrappers::AbstractEntityWrapper::~AbstractEntityWrapper
~AbstractEntityWrapper() override
Definition: AbstractEntityWrapper.cpp:36
AbstractEntityWrapper.h
memoryx::EntityWrappers::AbstractEntityWrapper::getName
std::string getName() const
Get the name of the stored entity.
Definition: AbstractEntityWrapper.cpp:47
memoryx::EntityWrappers::AbstractFileEntityWrapper::cacheAttributeFile
std::string cacheAttributeFile(const std::string &attrName, bool preserveOriginalFName=false) const
Definition: AbstractEntityWrapper.cpp:138
memoryx::EntityWrappers
Definition: AbstractEntityWrapper.cpp:30
memoryx::EntityWrappers::AbstractEntityWrapper::setEntity
void setEntity(const EntityBasePtr &entity)
Replace the stored entity with a new one.
Definition: AbstractEntityWrapper.cpp:59
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:33
memoryx::EntityWrappers::AbstractEntityWrapper::getId
std::string getId() const
Get the identifier of the stored entity.
Definition: AbstractEntityWrapper.cpp:41
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
memoryx::EntityWrappers::AbstractFileEntityWrapper::AbstractFileEntityWrapper
AbstractFileEntityWrapper(GridFileManagerPtr fileManager)
Definition: AbstractEntityWrapper.cpp:152
Variant.h
memoryx::EntityWrappers::AbstractFileEntityWrapper::fileManager
GridFileManagerPtr fileManager
Definition: AbstractEntityWrapper.h:106