Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "COSMOS DG Providing CMDBf Query and Registration Services"

(Creating Query Handlers)
 
(15 intermediate revisions by the same user not shown)
Line 20: Line 20:
  
 
== Outline ==
 
== Outline ==
 
 
 
 
 
 
  
  
 
== Content ==
 
== Content ==
  
 +
The content of this document has been merged with http://wiki.eclipse.org/COSMOS_DG_Constructing_a_Data_Manager
  
 
= Providing CMDBf Query Support =
 
 
As described in the previous section, the [http://www.cmdbf.org CMDBf specification] defines a query and a registration service.  COSMOS provides a set of reusable, extensible classes and interfaces that can be utilized in implementing such services.  This section will cover how COSMOS APIs can be used in providing a CMDBf query implementation for the Student-Teacher example.
 
 
 
== Architecture ==
 
 
The process of handling a CMDBf query involves traversing a structure representing the request, processing each individual part, and generating a structure representing the result.  COSMOS has already defined structures representing the request and response.  There is also generic code in place for traversing the request and invoking handlers for individual part of the CMDBf request.  A consumer needs to only provide implementation of such handlers as part of implementing the query service.
 
 
A handler is required to process each CMDBf query constraint that an implementation supports.  In addition to constraint handlers, the framework also requires two handlers for item templates and relationship templates.  Here's the order in which the handlers are invoked:
 
 
# Item/Relationship template handler
 
# Instance id constraint handler
 
# Record type constraint handler
 
# Property value constraint handler
 
 
OR
 
 
# Item/Relationship template handler
 
# XPath constraint handler
 
 
It is the responsibility of the handler factory class to create instances of each handler above.  The MDR toolkit automatically generates a handler factory.  See src/org.eclipse.cosmos.example.mdr.handlers.QueryHandlerFactory.java for the class generated.
 
 
== Creating Query Handlers ==
 
 
This sample will provide four handlers in total:
 
 
# An item template handler
 
# An instance id constraint handler
 
# A record type constraint handler
 
# A relationship handler
 
 
<b>Create</b> the following classes for each handler under src/org.eclipse.cosmos.example.mdr.handlers:
 
 
# ItemTemplateHandler
 
# ItemInstanceIdHandler
 
# ItemRecordTypeHandler.java
 
# RelationshipTemplateHandler.java
 
 
In addition to the four classes above, create an interface called "ICMDBfSampleConstants" under the same package to contain constants commonly used between handlers.  The content of the interface appears below:
 
<pre>
 
package org.eclipse.cosmos.example.mdr.handlers;
 
 
/**
 
* Constants used by this CMDBf query sample
 
*/
 
public interface ICMDBfSampleConstants
 
{
 
/**
 
* The key for the data provider that will be
 
* stored in the initialization data of the constraint
 
* handlers
 
*/
 
public static final String DATA_PROVIDER = "org.eclipse.cosmos.samples.cmdbf.services.query.ICMDBfSampleConstants";
 
}
 
</pre>
 
 
<b>ItemTemplateHandler</b> is there to simply process item templates that do not contain any constraints.  The content of the class appears below:
 
 
<pre>
 
package org.eclipse.cosmos.example.mdr.handlers;
 
 
import org.eclipse.cosmos.dc.cmdbf.services.common.CMDBfServiceException;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.service.impl.AbstractItemTemplateHandler;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.response.artifacts.IItemConvertible;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.response.artifacts.INodes;
 
 
/**
 
* An item template handler is invoked prior to invoking any constraint handler
 
* for item templates.
 
*/
 
public class ItemTemplateHandler extends AbstractItemTemplateHandler
 
{
 
 
/**
 
* This method is only invoked if there are no constraints included in an
 
* item template.
 
*
 
* @param nodes The nodes element to append items to
 
*/
 
protected void appendAllItems(INodes nodes) throws CMDBfServiceException
 
{
 
XMLRepository repo = (XMLRepository)getValue(ICMDBfSampleConstants.DATA_PROVIDER);
 
addItems(nodes, repo.classes);
 
addItems(nodes, repo.students);
 
addItems(nodes, repo.teachers);
 
}
 
 
 
/**
 
* A convenient method used to add individual nodes to the
 
* INodes instance passed in.
 
*
 
* @param nodes Container for individual nodes
 
* @param itemConvertibles Elements that are convertible to an Item
 
*/
 
private void addItems(INodes nodes, IItemConvertible[] itemConvertibles)
 
{
 
for (int i = 0; i < itemConvertibles.length; i++)
 
{
 
nodes.addItem(itemConvertibles[i].toItem(nodes));
 
}
 
}
 
}
 
</pre> 
 
 
The instance id constraint is used to locate items based on the id attribute of the student/teacher element or the course code defined for a class element.  The implementation simply walks through students, teachers, and classes to locate items that match the instance id constraint.  The content of <b>ItemInstanceIdHandler</b> appears below:
 
 
<pre>
 
package org.eclipse.cosmos.example.mdr.handlers;
 
 
import org.eclipse.cosmos.dc.cmdbf.services.common.CMDBfServiceException;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.service.impl.AbstractItemConstraintHandler;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.input.artifacts.IConstraint;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.input.artifacts.IInstanceIdConstraint;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.response.artifacts.INodes;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.response.artifacts.QueryOutputArtifactFactory;
 
import org.eclipse.cosmos.dc.cmdbf.services.transform.artifacts.IInstanceId;
 
import org.eclipse.cosmos.example.mdr.handlers.XMLRepository.SchoolMember;
 
 
/**
 
* This is the handler class for instance id constraints included
 
* in item templates.  Adopters can either provide a direct implementation of
 
* IItemConstraintHandler or extend AbstractItemConstraintHandler
 
*/
 
public class ItemInstanceIdHandler extends AbstractItemConstraintHandler
 
{
 
@Override
 
protected INodes handle(INodes context, IConstraint constraint) throws CMDBfServiceException
 
{
 
INodes result = QueryOutputArtifactFactory.getInstance().createNodes(context.getId());
 
IInstanceIdConstraint instanceIdConstraint = (IInstanceIdConstraint)constraint;
 
IInstanceId[] instanceIds = instanceIdConstraint.getInstanceIds();
 
for (int i = 0; i < instanceIds.length; i++) {
 
if (!XMLRepository.MDR_ID.equals(instanceIds[i].getMdrId()
 
.toString())) {
 
continue;
 
}
 
String localId = instanceIds[i].getLocalId().toString();
 
XMLRepository repo = (XMLRepository) getValue(ICMDBfSampleConstants.DATA_PROVIDER);
 
// Traverse the students
 
traverseSchoolMembers(result, localId, repo.students);
 
// Traverse the teachers
 
traverseSchoolMembers(result, localId, repo.teachers);
 
// Traverse the classes
 
for (int j = 0; j < repo.classes.length; j++)
 
{
 
if (localId.equals(repo.classes[j].courseCode))
 
{
 
result.addItem(repo.classes[j]);
 
}
 
}
 
}
 
return result;
 
}
 
 
private void traverseSchoolMembers (INodes result, String localId, SchoolMember[] members)
 
{
 
for (int i = 0; i < members.length; i++)
 
{
 
if (localId.equals(members[i].identity.id))
 
{
 
result.addItem(members[i]);
 
}
 
}
 
}
 
}
 
</pre>
 
 
Similarly, ItemRecordTypeHandler is used to process record type constraints.  The handler supports three record types: students, teachers, and class.  The content of <b>ItemRecordTypeHandler</b> appears below:
 
 
<pre>
 
package org.eclipse.cosmos.example.mdr.handlers;
 
 
import org.eclipse.cosmos.dc.cmdbf.services.common.CMDBfServiceException;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.service.IItemConstraintHandler;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.service.impl.AbstractItemConstraintHandler;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.input.artifacts.IConstraint;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.input.artifacts.IRecordType;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.response.artifacts.INodes;
 
import org.eclipse.cosmos.dc.cmdbf.services.query.transform.response.artifacts.QueryOutputArtifactFactory;
 
import org.eclipse.cosmos.dc.cmdbf.services.transform.artifacts.IGraphElement;
 
import org.eclipse.cosmos.dc.cmdbf.services.transform.artifacts.IItem;
 
import org.eclipse.cosmos.dc.cmdbf.services.transform.artifacts.IRecord;
 
import org.eclipse.cosmos.example.mdr.handlers.XMLRepository.ClassSession;
 
import org.eclipse.cosmos.example.mdr.handlers.XMLRepository.SchoolMember;
 
import org.eclipse.cosmos.example.mdr.handlers.XMLRepository.Student;
 
import org.eclipse.cosmos.example.mdr.handlers.XMLRepository.Teacher;
 
 
/**
 
* This is the handler for the record type constraint specified in
 
* and item template.  Adopters can either extend AbstractItemConstraintHandler
 
* or provide a direct implementation of {@link IItemConstraintHandler}
 
*/
 
public class ItemRecordTypeHandler extends AbstractItemConstraintHandler
 
{
 
/**
 
* Record type representing student
 
*/
 
private static final String RECORD_TYPE_STUDENT = "student";
 
 
/**
 
* Record type representing teacher
 
*/
 
private static final String RECORD_TYPE_TEACHER = "teacher";
 
 
/**
 
* Record type representing class
 
*/
 
private static final String RECORD_TYPE_CLASS = "class";
 
 
 
@Override
 
protected INodes handle(INodes context, IConstraint constraint) throws CMDBfServiceException
 
{
 
IRecordType recordType = (IRecordType)constraint;
 
String localName = recordType.getLocalName();
 
XMLRepository repo = (XMLRepository)getValue(ICMDBfSampleConstants.DATA_PROVIDER);
 
INodes result = QueryOutputArtifactFactory.getInstance().createNodes(context.getId());
 
 
// If the record type constraint includes all items as its context
 
if (context.isStartingContext())
 
{
 
if (RECORD_TYPE_STUDENT.equals(localName))
 
{
 
addSchoolMembers(result, repo.students);
 
}
 
else if (RECORD_TYPE_TEACHER.equals(localName))
 
{
 
addSchoolMembers(result, repo.teachers);
 
}
 
else if (RECORD_TYPE_CLASS.equals(localName))
 
{
 
for (int i = 0; i < repo.classes.length; i++)
 
{
 
result.addItem(repo.classes[i]);
 
}
 
}
 
 
return result;
 
}
 
 
IGraphElement[] elements = context.getElements();
 
for (int i = 0; i < elements.length; i++)
 
{
 
IRecord[] records = elements[i].getRecords();
 
for (int j = 0; j < records.length; j++)
 
{
 
if (RECORD_TYPE_STUDENT.equals(localName) && records[j].getValue() instanceof Student)
 
{
 
result.addItem((IItem)elements[i]);
 
continue;
 
}
 
else if (RECORD_TYPE_TEACHER.equals(localName) && records[j].getValue() instanceof Teacher)
 
{
 
result.addItem((IItem)elements[i]);
 
continue;
 
}
 
else if (RECORD_TYPE_CLASS.equals(localName) && records[j].getValue() instanceof ClassSession)
 
{
 
result.addItem((IItem)elements[i]);
 
continue;
 
}
 
}
 
}
 
 
return result;
 
}
 
 
private void addSchoolMembers(INodes result, SchoolMember[] members)
 
{
 
for (int i = 0; i < members.length; i++)
 
{
 
result.addItem(members[i]);
 
}
 
}
 
}
 
</pre>
 
 
[[Category:COSMOS_Development_Guide]]
 
[[Category:COSMOS_Development_Guide]]

Latest revision as of 11:44, 2 June 2008

COSMOS Wiki > COSMOS Document Plan > COSMOS Manual Guide

COSMOS Development Guide Providing CMDBf Query and Registration Services

Category: Development Guide

Owner Ali Mehregani
Bug # 219142
Due dates Schedule

Outline

Content

The content of this document has been merged with http://wiki.eclipse.org/COSMOS_DG_Constructing_a_Data_Manager

Back to the top