コード例 #1
0
void getDetails( XmlTransaction &txn, XmlManager &mgr, const XmlContainer &container, const std::string &query, XmlQueryContext &context )
{
	////////////////////////////////////////////////////////////////////////
	//////  Performs an query (in context) against the         ///////
	//////  provided container.                                      ///////
	////////////////////////////////////////////////////////////////////////

	///// some defensive code eliminated for clarity //

	// Perform the query. Result type is by default Result Document
	std::string fullQuery = "collection('" + container.getName() + "')" + query;
	try {
		std::cout << "Exercising query '" << fullQuery << "' " << std::endl;
		std::cout << "Return to continue: ";
		getc(stdin);

		XmlResults results( mgr.query( txn, fullQuery, context ) );
		XmlValue value;
		std::cout << "\n\tProduct : Price : Inventory Level\n";
		while( results.next(value) ) {
			/// Retrieve the value as a document
			XmlDocument theDocument = value.asDocument();

			/// Obtain information of interest from the document. Note that the
			//  wildcard in the query expression allows us to not worry about what
			//  namespace this document uses.
			std::string item = getValue( txn, mgr, theDocument, "fn:string(/*/product)", context);
			std::string price = getValue( txn, mgr, theDocument, "fn:string(/*/inventory/price)", context);
			std::string inventory = getValue( txn, mgr, theDocument, "fn:string(/*/inventory/inventory)", context);

			std::cout << "\t" << item << " : " << price << " : " << inventory << std::endl;

		}
		std::cout << "\n";
		std::cout << (unsigned int) results.size()
			<< " objects returned for expression '"
			<< fullQuery << "'\n" << std::endl;
	}
	//Catches XmlException
	catch(std::exception &e) {
		std::cerr << "Query " << fullQuery << " failed\n";
		std::cerr << e.what() << "\n";
		txn.abort();
		exit(-1);
	}
}
コード例 #2
0
void doContextQuery( XmlTransaction &txn, XmlManager &mgr, const std::string cname,
					const std::string &query, XmlQueryContext &context )
{
	////////////////////////////////////////////////////////////////////////
	//////  Performs a simple query (with context) against the ///////
	//////  provided container.                                      ///////
	////////////////////////////////////////////////////////////////////////

	///// some defensive code eliminated for clarity //

	// Perform the query. Result type is by default Result Document
	std::string fullQuery = "collection('" + cname + "')" + query;
	try {
		std::cout << "Exercising query '" << fullQuery << "' " << std::endl;
		std::cout << "Return to continue: ";
		getc(stdin);
		std::cout << "\n";

		XmlResults results( mgr.query(txn, fullQuery, context ) );
		XmlValue value;
		while(results.next(value)) {
			// Get the document's name and print it to the console
			XmlDocument theDocument = value.asDocument();
			std::cout << "Document name: " << theDocument.getName() << std::endl;
			std::cout << value.asString() << std::endl;
		}

		std::cout << (unsigned int)results.size()
			<< " objects returned for expression '" << fullQuery
			<< "'\n" << std::endl;

	}
	//Catches XmlException
	catch(std::exception &e) {
		std::cerr << "Query " << fullQuery << " failed\n";
		std::cerr << e.what() << "\n";
		txn.abort();
		exit( -1 );
	}
}
コード例 #3
0
void MgResourceDefinitionManager::DeleteResource(
    MgResourceIdentifier* resource, bool strict)
{
    assert(NULL != resource);

    MG_RESOURCE_SERVICE_TRY()

    string resourcePathname;
    MgUtil::WideCharToMultiByte(resource->ToString(), resourcePathname);

    // Set up an XQuery.

    string query;

    if (resource->IsFolder())
    {
        query  = "for $i in collection('";
        query += m_container.getName();
        query += "')";
        query += "/*[starts-with(dbxml:metadata('dbxml:name'),'";
        query += resourcePathname;
        query += "')]";
        query += " order by dbxml:metadata('dbxml:name', $i) descending return $i";
    }
    else
    {
        query  = "collection('";
        query += m_container.getName();
        query += "')";
        query += "/*[dbxml:metadata('dbxml:name')='";
        query += resourcePathname;
        query += "']";
    }

    // Execute the XQuery.

    XmlManager& xmlManager = m_container.getManager();
    XmlQueryContext queryContext = xmlManager.createQueryContext();
    queryContext.setNamespace(MgResourceInfo::sm_metadataPrefix,
        MgResourceInfo::sm_metadataUri);
    XmlResults results = IsTransacted() ?
        xmlManager.query(GetXmlTxn(), query, queryContext, 0) :
        xmlManager.query(query, queryContext, 0);

    if (0 == results.size())
    {
        if (!strict || (IsResourceContentManager() && resource->IsFolder()))
        {
            return;
        }
        else
        {
            m_repositoryMan.ThrowResourceNotFoundException(*resource,
                L"MgResourceDefinitionManager.DeleteResource",
                __LINE__, __WFILE__);
        }
    }

    // Delete the resources.

    MgResourceIdentifier currResource;
    XmlUpdateContext updateContext = xmlManager.createUpdateContext();
    XmlValue xmlValue;

    while (results.next(xmlValue))
    {
        XmlDocument xmlDoc = xmlValue.asDocument();

        currResource.SetResource(MgUtil::MultiByteToWideChar(xmlDoc.getName()));

        if (!currResource.IsRoot())
        {
            DeleteDocument(currResource, xmlDoc, updateContext);
        }
    }

    if (!resource->IsRoot())
    {
        m_repositoryMan.UpdateDateModifiedResourceSet(resource->GetFullPath(true));
    }

    MG_RESOURCE_CONTAINER_CATCH_AND_THROW(L"MgResourceDefinitionManager.DeleteResource")
}
コード例 #4
0
///////////////////////////////////////////////////////////////////////////////
/// \brief
/// Packages the specified resource.
///
void MgLibraryResourceContentManager::PackageResource(MgResourceIdentifier& resource,
    MgResourcePackageMaker& packageMaker)
{
    ACE_ASSERT(resource.IsFolder());

    MG_RESOURCE_SERVICE_TRY()

    string resourcePathname;
    MgUtil::WideCharToMultiByte(resource.ToString(), resourcePathname);

    // Set up an XQuery.
    XmlManager& xmlMan = m_container.getManager();
    XmlQueryContext queryContext = xmlMan.createQueryContext();
    string query;

    if (m_repositoryMan.m_currUserIsAdmin)
    {
        queryContext.setEvaluationType(XmlQueryContext::Lazy);
        query = "collection('";
    }
    else
    {
        queryContext.setEvaluationType(XmlQueryContext::Eager);
        query = "for $i in collection('";
    }

    query += m_container.getName();
    query += "')";
    query += "/*[starts-with(dbxml:metadata('dbxml:name'),'";
    query += resourcePathname;

    if (m_repositoryMan.m_currUserIsAdmin)
    {
        query += "')]";
    }
    else
    {
        query += "')] order by dbxml:metadata('dbxml:name', $i) return $i";
    }

    // Execute the XQuery.
    XmlResults results = IsTransacted() ?
        xmlMan.query(GetXmlTxn(), query, queryContext, 0) :
        xmlMan.query(query, queryContext, 0);

    MgLibraryRepositoryManager& libraryRepositoryMan = (MgLibraryRepositoryManager&)m_repositoryMan;
    MgResourceDataFileManager* dataFileMan = libraryRepositoryMan.GetResourceDataFileManager();
    ACE_ASSERT(NULL != dataFileMan);
    MgResourceDataStreamManager* dataStreamMan = libraryRepositoryMan.GetResourceDataStreamManager();
    ACE_ASSERT(NULL != dataStreamMan);

    // Get the resources
    MgResourceIdentifier currResource;
    XmlValue xmlValue;

    while (results.next(xmlValue))
    {
        // Note that the permission checks were already done at the time we
        // packaged the resource headers.
        const XmlDocument& xmlDoc = xmlValue.asDocument();
        currResource.SetResource(MgUtil::MultiByteToWideChar(xmlDoc.getName()));

        // Package the resource content.
        if (!packageMaker.PackageResourceContent(currResource, xmlDoc))
        {
            // The current user has no read permission to this resource.
            continue;
        }

        // Skip packaging resource data if there is no resource tag.
        XmlValue tagValue;

        if (!xmlDoc.getMetaData(MgResourceInfo::sm_metadataUri,
            MgResourceInfo::sm_metadataNames[MgResourceInfo::Tags], tagValue))
        {
            continue;
        }

        STRING resourceTags;
        MgUtil::MultiByteToWideChar(tagValue.asString(), resourceTags);

        // Get the file path if it exists.
        MgTagManager tagMan(resourceTags);
        STRING filePath;
        MgTagInfo filePathTag;

        if (tagMan.GetTag(MgResourceTag::DataFilePath, filePathTag, false))
        {
            filePath = dataFileMan->GetResourceDataFilePath();
            filePath += filePathTag.GetAttribute(MgTagInfo::TokenValue);
            filePath += L"/";
        }

        // Enumerate the resource data.
        MgTagMap& tagMap = tagMan.GetTagMap();

        for (MgTagMap::const_iterator i = tagMap.begin();
            i != tagMap.end(); ++i)
        {
            CREFSTRING dataName = (*i).first;

            // Skip the reserved tags.
            if (tagMan.IsReservedTag(dataName))
            {
                continue;
            }

            // Get the resource data.
            const MgTagInfo& tagInfo = (*i).second;
            CREFSTRING mimeType = tagInfo.GetAttribute(MgTagInfo::MimeType);
            CREFSTRING dataType = tagInfo.GetAttribute(MgTagInfo::StorageType);
            Ptr<MgByteReader> byteReader;

            if (MgResourceDataType::File == dataType)
            {
                ACE_ASSERT(!filePath.empty());
                STRING pathname = filePath;
                pathname += dataName;

                byteReader = dataFileMan->GetResourceData(pathname, mimeType);
            }
            else if (MgResourceDataType::Stream == dataType)
            {
                string dataKey;
                MgUtil::WideCharToMultiByte(
                    tagInfo.GetAttribute(MgTagInfo::TokenValue), dataKey);

                byteReader = dataStreamMan->GetResourceData(dataKey, dataName,
                    mimeType);
            }
            else if (MgResourceDataType::String == dataType)
            {
                string data;
                MgUtil::WideCharToMultiByte(
                    tagInfo.GetAttribute(MgTagInfo::TokenValue), data);

                Ptr<MgByteSource> byteSource = new MgByteSource(
                    (BYTE_ARRAY_IN)data.c_str(), (INT32)data.length());

                byteSource->SetMimeType(mimeType);
                byteReader = byteSource->GetReader();
            }
            else
            {
                throw new MgInvalidResourceDataTypeException(
                    L"MgLibraryResourceContentManager.PackageResource",
                    __LINE__, __WFILE__, NULL, L"", NULL);
            }

            // Package the resource data.
            packageMaker.PackageResourceData(currResource, byteReader,
                dataName, dataType);
        }
    }

    MG_RESOURCE_SERVICE_CATCH_AND_THROW(L"MgLibraryResourceContentManager.PackageResource")
}