Example #1
0
		void WebPageLinkFunction::_setFromParametersMap(const ParametersMap& map)
		{
			// Target
			string targetStr(map.get<string>(PARAMETER_TARGET));
			ParametersMap::Trim(targetStr);
			if(!targetStr.empty() && targetStr[0] >= '0' && targetStr[0] <= '9')
			{	// Page by ID
				try
				{
					RegistryKeyType pageId(lexical_cast<RegistryKeyType>(targetStr));
					_target = Env::GetOfficialEnv().get<Webpage>(pageId).get();
				}
				catch(bad_lexical_cast&)
				{
					throw RequestException("Bad cast in page id");
				}
				catch(ObjectNotFoundException<Webpage>&)
				{
					throw RequestException("No such web page");
				}
			}
			else
			{	// Page by smart URL
				_target = getSite()->getPageBySmartURL(targetStr);
				if(!_target)
				{
					throw RequestException("No such web page");
				}
			}

			
			optional<string> ot(map.getOptional<string>(PARAMETER_TEXT, false));
			_text = ot ? *ot : _target->getName();
			_useSmartURL = map.getDefault<bool>(PARAMETER_USE_SMART_URL, true);

			_confirm = map.getDefault<string>(PARAMETER_CONFIRM, string(), false);

			_title = map.getDefault<string>(PARAMETER_TITLE);

			// Class
			_class = map.getDefault<string>(PARAMETER_CLASS);

			// Additional parameters
			BOOST_FOREACH(const ParametersMap::Map::value_type& item, map.getMap())
			{
				if(item.first == PARAMETER_TEXT ||
					item.first == PARAMETER_TARGET ||
					item.first == PARAMETER_USE_SMART_URL ||
					item.first == PARAMETER_CONFIRM ||
					item.first == PARAMETER_TITLE ||
					item.first == PARAMETER_CLASS
				){
					continue;
				}

				// Using the getValue virtual method instead of item.second in order to handle DelayedEvaluationParametersMap
				_parameters.insert(item.first, map.getValue(item.first));
			}
		}
			void SVNWorkingCopy::_export(
				const Registrable& object,
				const boost::filesystem::path& dirPath,
				const bool noCommit
			) const	{

				RegistryKeyType key(object.getKey());

				//////////////////////////////////////////////////////////////////////////
				// Attributes

				// File creation or update ?
				path dumpFilePath(dirPath / path(lexical_cast<string>(key) + ".dump"));
				bool creation(!filesystem::exists(dumpFilePath));

				// Field names
				ParametersMap attributesMap;
				object.toParametersMap(attributesMap, false, false);
				ofstream dumpStream(
					dumpFilePath.string().c_str(),
					std::ios_base::out | std::ios_base::binary
				);
				dumpStream << object.getTableName() << "(";
				bool first(true);
				BOOST_FOREACH(const ParametersMap::Map::value_type& item, attributesMap.getMap())
				{
					if(first)
					{
						first = false;
					}
					else
					{
						dumpStream << ",";
					}
					dumpStream << item.first;
				}
				dumpStream << ")" << endl;

				// Values
				first = true;
				BOOST_FOREACH(const ParametersMap::Map::value_type& item, attributesMap.getMap())
				{
					string escapedValue;
					escapedValue = item.second;
					replace_all(escapedValue, "\\", "\\\\");
					replace_all(escapedValue, "\"", "\\\"");

					if(first)
					{
						first = false;
					}
					else
					{
						dumpStream << "," << endl;
					}
					dumpStream << "\"" << escapedValue << "\"";
				}
				dumpStream << endl;
				dumpStream.close();

				// If creation, svn add
				if(creation && !noCommit)
				{
					_svnAdd(dumpFilePath);
				}

				//////////////////////////////////////////////////////////////////////////
				// Files
				FilesMap filesMap;
				object.toFilesMap(filesMap);
				BOOST_FOREACH(const FilesMap::Map::value_type& item, filesMap.getMap())
				{
					// Creation or update or move
					bool creation(true);
					string fileNameMainPart(lexical_cast<string>(key) + "_" + item.first);
					string fileNameWithExtension(fileNameMainPart + "." + item.second.mimeType.getDefaultExtension());
					path filePath(dirPath / path(fileNameWithExtension));
					for(directory_iterator it(dirPath); it != directory_iterator(); ++it)
					{
						if(	!is_directory(it->path()) &&
							it->path().filename().string().size() >= fileNameMainPart.size() &&
							it->path().filename().string().substr(0, fileNameMainPart.size()) == fileNameMainPart
						){
							creation = false;
							if(it->path().filename().string() != fileNameWithExtension && !noCommit)
							{
								_svnMove(it->path(), filePath);
							}
							break;
						}
					}

					// Dump value into file
					ofstream fileStream(filePath.string().c_str(), std::ios_base::out | std::ios_base::binary);
					fileStream << item.second.content;
					fileStream.close();

					// If creation, svn add
					if(creation && !noCommit)
					{
						_svnAdd(filePath);
					}
				}


				//////////////////////////////////////////////////////////////////////////
				// Name file
				if(!object.getName().empty())
				{
					path namePath(dirPath / path(lexical_cast<string>(key) + "_" + object.getName() + ".name"));
					string commonFileName(lexical_cast<string>(key) + "_");
					string nameExtension(".name");

					// Search for an existing file
					bool creation(true);
					for(directory_iterator it(dirPath); it != directory_iterator(); ++it)
					{
						// Jump over directories
						if(is_directory(it->path()))
						{
							continue;
						}

						// Existing name ?
						if(namePath == it->path())
						{
							creation = false;
							break;
						}

						// Search for preceding name
						string fileName(it->path().filename().string());
						if(	(fileName.size() >= commonFileName.size() + nameExtension.size()) &&
							fileName.substr(0, commonFileName.size()) == commonFileName &&
							fileName.substr(fileName.size() - nameExtension.size()) == nameExtension &&
							!noCommit
						){
							_svnMove(it->path(), namePath);
							break;
						}
					}

					// Creation if the fake file if necessary
					if(creation)
					{
						ofstream nameStream(
							namePath.string().c_str(),
							std::ios_base::out | std::ios_base::binary
						);
						nameStream << endl;
						nameStream.close();
						if (!noCommit)
						{
							_svnAdd(namePath);
						}
					}
				}


				//////////////////////////////////////////////////////////////////////////
				// Sub objects
				SubObjects subObjects(object.getSubObjects());
				path subdirPath(dirPath / lexical_cast<string>(key));

				// No sub object : delete subdirectory if exists
				if(subObjects.empty())
				{
					if(exists(subdirPath))
					{
						_svnDelete(subdirPath);
					}
				}
				else // Sub objects present
				{
					// Existing file with the same name than the directory (should never happen)
					if(exists(subdirPath) && !is_directory(subdirPath))
					{
						_svnDelete(subdirPath);
					}

					// If no existing file, add a new directory
					if(!exists(subdirPath))
					{
						create_directory(subdirPath);
						_svnAdd(subdirPath);
					}

					// Loop on objects to export
					set<RegistryKeyType> existingIds;
					BOOST_FOREACH(const SubObjects::value_type& subObject, subObjects)
					{
						// Register the id
						existingIds.insert(subObject->getKey());

						// Export of the object (recursion)
						_export(*subObject, subdirPath, noCommit);
					}

					// Loop on existing files in the path and remove files that correspond
					// to non existent objects
					vector<path> filesToRemove;
					for(directory_iterator it(subdirPath); it != directory_iterator(); ++it)
					{
						string fileName(it->path().filename().string());

						// Id part of the path
						vector<string> nameParts;
						split(nameParts, fileName, is_any_of("_."));
						if(nameParts.empty())
						{
							continue;
						}
						try
						{
							// Check if the file corresponds to an existent record
							RegistryKeyType id(lexical_cast<RegistryKeyType>(nameParts[0]));
							if(existingIds.find(id) == existingIds.end())
							{
								filesToRemove.push_back(it->path());
							}
						}
						catch(bad_lexical_cast)
						{
						}
					}

					// Remove files corresponding to the deleted objects
					BOOST_FOREACH(const path& file, filesToRemove)
					{
						_svnDelete(file);
					}