Example usage for javax.xml.registry.infomodel RegistryObject addAssociation

List of usage examples for javax.xml.registry.infomodel RegistryObject addAssociation

Introduction

In this page you can find the example usage for javax.xml.registry.infomodel RegistryObject addAssociation.

Prototype

void addAssociation(Association association) throws JAXRException;

Source Link

Document

Adds specified Association to use this object as source.

Usage

From source file:de.kp.ames.web.function.bulletin.PostingLCM.java

/**
 * Submit posting for a certain recipient; a posting is an 
 * extrinsic object that is classified as posting and contained
 * within the respective positing container; 
 * //from w  w w  .j a va  2s. c o m
 * the association to the addressed recipient is mapped onto an 
 * association instance
 * 
 * @param recipient
 * @param data
 * @return
 * @throws Exception 
 */
public String submitPosting(String recipient, String data) throws Exception {

    /*
     * Create or retrieve registry package that is 
     * responsible for managing posting
     */
    RegistryPackageImpl container = null;
    JaxrDQM dqm = new JaxrDQM(jaxrHandle);

    List<RegistryPackageImpl> list = dqm.getRegistryPackage_ByClasNode(ClassificationConstants.FNC_ID_Posting);
    if (list.size() == 0) {
        /*
         * Create container
         */
        container = createPostingPackage();

    } else {
        /*
         * Retrieve container
         */
        container = list.get(0);

    }

    /* 
     * A posting is a certain extrinsic object that holds all relevant
     * and related information in a single JSON repository item
     */
    ExtrinsicObjectImpl eo = null;
    JaxrTransaction transaction = new JaxrTransaction();

    /* 
     * Create extrinsic object that serves as a container for
     * the respective posting
     */
    eo = createExtrinsicObject();
    if (eo == null)
        throw new JAXRException();

    /* 
     * Identifier
     */
    String eid = JaxrIdentity.getInstance().getPrefixUID(FncConstants.POSTING_PRE);

    /*
    * Name & description using default locale
    */
    JSONObject jPosting = new JSONObject(data);

    /*
     * Name is the 'name' of the recipient
     */
    String name = "[POST] " + jPosting.getString(JaxrConstants.RIM_NAME);
    String desc = "[SUBJ] " + jPosting.getString(JaxrConstants.RIM_SUBJECT);

    /* 
     * Home url
     */
    String home = jaxrHandle.getEndpoint().replace("/saml", "");

    /*
     * Set properties
     */
    setProperties(eo, eid, name, desc, home);

    /* 
     * Mime type & handler
     */
    String mimetype = GlobalConstants.MT_JSON;
    eo.setMimeType(mimetype);

    byte[] bytes = data.getBytes(GlobalConstants.UTF_8);

    DataHandler handler = new DataHandler(FileUtil.createByteArrayDataSource(bytes, mimetype));
    eo.setRepositoryItem(handler);

    /*
     * Create classification
     */
    ClassificationImpl c = createClassification(ClassificationConstants.FNC_ID_Posting);
    c.setName(createInternationalString(Locale.US, "Posting Classification"));

    /* 
     * Associate classification and posting container
     */
    eo.addClassification(c);

    /* 
     * Add extrinsic object to container
     */
    container.addRegistryObject(eo);

    /*
     * Create association
     */
    String aid = JaxrIdentity.getInstance().getPrefixUID(FncConstants.POSTING_PRE);
    AssociationImpl a = this.createAssociation_RelatedTo(eo);
    /*
     * Set properties
     */
    setProperties(a, aid, "Recipient Link",
            "This is a directed association between a recipient and the respective posting.", home);

    /*
     * Source object (could be User or Organization)
     */

    RegistryObject so = (RegistryObject) jaxrHandle.getDQM().getRegistryObject(recipient);

    so.addAssociation(a);

    /*
     * Add association to container
     */
    container.addRegistryObject(a);

    /*
     * Save objects
     */
    confirmAssociation(a);

    /*
     * Only the registryPackage needs to be added for save
     * All dependent objects will be added automatically 
     */
    transaction.addObjectToSave(container);
    saveObjects(transaction.getObjectsToSave(), false, false);

    /*
     * Supply reactor
     */
    ReactorParams reactorParams = new ReactorParams(jaxrHandle, eo, ClassificationConstants.FNC_ID_Posting,
            RAction.C_INDEX);
    ReactorImpl.onSubmit(reactorParams);

    /*
     * Retrieve response message
     */
    JSONObject jResponse = transaction.getJResponse(eid, FncMessages.POSTING_CREATED);
    return jResponse.toString();

}