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"
25 #include "../MongoDBRef.h"
26 #include "../GridFileManager.h"
27 
29 {
31  entity(NULL)
32  {
33  }
34 
36  {
37  }
38 
39  std::string AbstractEntityWrapper::getId() const
40  {
41  return entity ? entity->getId() : "";
42  }
43 
44  std::string AbstractEntityWrapper::getName() const
45  {
46  return entity ? entity->getName() : "";
47  }
48 
49  EntityBasePtr AbstractEntityWrapper::getEntity() const
50  {
51  return entity;
52  }
53 
54  void AbstractEntityWrapper::setEntity(const EntityBasePtr& entity)
55  {
56  this->entity = entity;
57  }
58 
59  //void AbstractEntityWrapper::setEntity(EntityBase* entity)
60  //{
61  // this->entity = entity;
62  //}
63 
64  void AbstractFileEntityWrapper::cleanUpAttributeFiles(memoryx::EntityAttributeBasePtr oldAttr, memoryx::EntityAttributeBasePtr newAttr)
65  {
66  if (!oldAttr || !newAttr)
67  {
68  return;
69  }
70 
71  int size = oldAttr->size();
72  int newSize = newAttr->size();
73 
74  for (int i = 0; i < size; ++i)
75  {
76  armarx::VariantPtr oldMongoRef = armarx::VariantPtr::dynamicCast(oldAttr->getValueAt(i));
77 
78  std::string oldDocId;
79 
80  if (oldMongoRef)
81  {
82  oldDocId = oldMongoRef->getClass<MongoDBRef>()->docId;
83  }
84 
85  std::string newDocId;
86 
87  if (i < newSize)
88  {
89  armarx::VariantPtr newMongoRef = armarx::VariantPtr::dynamicCast(newAttr->getValueAt(i));
90  newDocId = newMongoRef->get<MongoDBRef>()->docId;
91  }
92 
93  if (i >= newSize || (!oldDocId.empty() && newDocId != oldDocId))
94  {
95  ARMARX_INFO_S << "Removing old file: " << oldAttr->getName() ;
96  fileManager->removeAttrFile(oldAttr, i);
97  }
98  }
99 
100  }
101 
102  void AbstractFileEntityWrapper::removeAttributeFiles(const memoryx::EntityAttributeBasePtr& attr)
103  {
104  if (!attr)
105  {
106  return;
107  }
108 
109  int size = attr->size();
110  for (int i = 0; i < size; ++i)
111  {
112  armarx::VariantPtr oldMongoRef = armarx::VariantPtr::dynamicCast(attr->getValueAt(i));
113 
114  std::string oldDocId;
115 
116  if (oldMongoRef)
117  {
118  oldDocId = oldMongoRef->getClass<MongoDBRef>()->docId;
119  if (!oldDocId.empty())
120  {
121  ARMARX_INFO_S << "Removing file: " << attr->getName() ;
122  fileManager->removeAttrFile(attr, i);
123  }
124  }
125  }
126 
127  }
128 
129  std::string AbstractFileEntityWrapper::cacheAttributeFile(const std::string& attrName, bool preserveOriginalFName /* = false */) const
130  {
131  std::string result = "";
132 
133  if (getEntity()->hasAttribute(attrName))
134  {
135  fileManager->ensureFileInCache(getEntity()->getAttribute(attrName), result, preserveOriginalFName);
136  }
137 
138  return result;
139  }
140 
141 
143  fileManager(fileManager)
144  {
145 
146  }
147 }
memoryx::EntityWrappers::AbstractFileEntityWrapper::removeAttributeFiles
void removeAttributeFiles(const memoryx::EntityAttributeBasePtr &attr)
removes all files of an attribute from the gridfs
Definition: AbstractEntityWrapper.cpp:102
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:64
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:49
memoryx::EntityWrappers::AbstractEntityWrapper::AbstractEntityWrapper
AbstractEntityWrapper()
Definition: AbstractEntityWrapper.cpp:30
IceInternal::Handle< Variant >
memoryx::EntityWrappers::AbstractEntityWrapper::~AbstractEntityWrapper
~AbstractEntityWrapper() override
Definition: AbstractEntityWrapper.cpp:35
AbstractEntityWrapper.h
memoryx::EntityWrappers::AbstractEntityWrapper::getName
std::string getName() const
Get the name of the stored entity.
Definition: AbstractEntityWrapper.cpp:44
memoryx::EntityWrappers::AbstractFileEntityWrapper::cacheAttributeFile
std::string cacheAttributeFile(const std::string &attrName, bool preserveOriginalFName=false) const
Definition: AbstractEntityWrapper.cpp:129
memoryx::EntityWrappers
Definition: AbstractEntityWrapper.cpp:28
memoryx::EntityWrappers::AbstractEntityWrapper::setEntity
void setEntity(const EntityBasePtr &entity)
Replace the stored entity with a new one.
Definition: AbstractEntityWrapper.cpp:54
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:32
memoryx::EntityWrappers::AbstractEntityWrapper::getId
std::string getId() const
Get the identifier of the stored entity.
Definition: AbstractEntityWrapper.cpp:39
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
memoryx::EntityWrappers::AbstractFileEntityWrapper::AbstractFileEntityWrapper
AbstractFileEntityWrapper(GridFileManagerPtr fileManager)
Definition: AbstractEntityWrapper.cpp:142
Variant.h
memoryx::EntityWrappers::AbstractFileEntityWrapper::fileManager
GridFileManagerPtr fileManager
Definition: AbstractEntityWrapper.h:106