com.ibm.maximo.oslc.AttachmentSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maximo-restclient Show documentation
Show all versions of maximo-restclient Show documentation
The Maximo REST client library provides a set of driver API's which can be consumed by an JAVA based web component that would like to interface with a Maximo instance
/*
* Licensed Materials - Property of IBM
*
* (C) COPYRIGHT IBM CORP. 2015 All Rights Reserved
*
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with
* IBM Corp.
*/
package com.ibm.maximo.oslc;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.json.*;
/**
*
* {@code AttachmenSet} implement the operations on attachmentset from Resource.
* It provides the set of Attachment.
*
* This object can be created by {@code AttachmentSet}.
* The following code shows how to create {@code AttachmentSet} using {@code AttachmentSet} Constructor
*
*
*
* Resource res = new Resource();
* AttachmentSet ats = res.attachmentSet(doclinAttrName,relName);
*
*
*
*
* The following examples demonstrate how to build a new {@code AttachmentSet}
*
*
* AttachmentSet ats = new AttachmentSet();
* AttachmentSet ats = new AttachmentSet(jsonobject,maximoconnector);
* AttachmentSet ats = new AttachmentSet(uri,maximoconnector);
*
*
*
*
* The following examples demonstrate how to set uri and jsonobject to {@code Attachment}
*
*
* ats.href(uri);
* ats.JsonObject(jo);
*
*
*
*
* The following examples show how to load and reload data
*
*
* att.load();
* att.reload();
*
*
*
*
* The following examples show how to get AttachmentSet data from {@code AttachmentSet}
*
*
* JsonObject jo = att.toJSON();
* byte[] jodata = att.toJSONBytes();
* String uri = att.getURI();
*
*
*
*
* The following examples show how to create, get and delete {@code Attachment} from{@code AttachmentSet}
*
*
* Attachment att = ats.create(new Attachment());
* Attachment att = ats.create(relation, new Attachment());
* Attachment att = ats.member(index);
* Attachment att = ats.member(id);
* ats.delete(index);
* ats.delete(id);
*
*
*
* The following example shows how to get the this page size from {@code AttachmentSet}
*
*
* int currentPageSize = ats.thisPageSize();
*
*
*
*/
public class AttachmentSet {
private String href;
private JsonObject jo;
private MaximoConnector mc;
private boolean isLoaded = false;
public AttachmentSet(){
}
public AttachmentSet (MaximoConnector mc) {
this.mc = mc;
}
public AttachmentSet(JsonObject jo, MaximoConnector mc) {
this.jo = jo;
if (jo.containsKey("rdf:about")) {
this.href = jo.getString("rdf:about");
} else {
this.href = jo.getString("href");
}
this.mc = mc;
}
public AttachmentSet(String href, MaximoConnector mc){
this.href = href;
this.mc = mc;
}
/**
* Get current URI
*
*/
public String getURI(){
return this.href;
}
/**
* Get AttahcmentSet data in JSON
*
* @throws IOException
* @throws OslcException
*/
public JsonObject toJSON() throws IOException, OslcException{
this.load();
return this.jo;
}
/**
* Get AttahcmentSet data in JSONBytes
*
* @throws IOException
* @throws OslcException
*/
public byte[] toJSONBytes() throws OslcException,IOException{
this.load();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
Json.createWriter(bo).writeObject(this.jo);
bo.close();
return bo.toByteArray();
}
public AttachmentSet href(String href){
this.href = href;
return this;
}
public AttachmentSet JsonObject(JsonObject jo){
this.jo = jo;
if (jo.containsKey("rdf:about")) {
this.href = jo.getString("rdf:about");
} else {
this.href = jo.getString("href");
}
return this;
}
/**
* Load the data for attachmentset
*
* @throws IOException
* @throws OslcException
*/
public AttachmentSet load() throws IOException, OslcException{
if(isLoaded){
return this;
}
this.jo = this.mc.get(this.href);
isLoaded = true;
return this;
}
public AttachmentSet reload() throws IOException, OslcException{
isLoaded = false;
load();
return this;
}
/**
* Create a new attachment
* @param att
*
* @throws IOException
* @throws OslcException
*/
public Attachment create(Attachment att) throws IOException, OslcException{
JsonObject obj = this.mc.createAttachment(this.href,att.toDoc(), att.getName(),att.getDescription(),att.getMeta());
return new Attachment(obj,this.mc);
}
public Attachment create(String relation,Attachment att) throws IOException, OslcException{
if(!this.href.contains(relation.toLowerCase()) || !this.href.contains(relation.toUpperCase())){
this.href+="/"+relation;
}
JsonObject obj = this.mc.createAttachment(this.href,att.toDoc(), att.getName(),att.getDescription(),att.getMeta());
return new Attachment(obj,this.mc);
}
/**
* Get the member of attachmentset
* @param index
*
* @throws IOException
* @throws OslcException
*/
public Attachment member(int index) throws IOException, OslcException{
if(!isLoaded){
load();
}
JsonArray arr = null;
if(this.jo.containsKey("rdfs:member")){
arr = this.jo.getJsonArray("rdfs:member");
}else{
arr = this.jo.getJsonArray("member");
}
JsonObject obj = (JsonObject)arr.get(index-1);
return new Attachment(obj,this.mc);
}
public Attachment member(String id) throws IOException, OslcException{
if(!isLoaded){
load();
}
JsonObject obj = null;
JsonArray arr = null;
if(this.jo.containsKey("rdfs:member")){
arr = this.jo.getJsonArray("rdfs:member");
}else{
arr = this.jo.getJsonArray("member");
}
for(int i=0;i 0) {
strb.append(uri.contains("?") ? "" : "?").append(
"&oslc.properties=");
for (String property : properties) {
strb.append(property).append(",");
}
if (strb.toString().endsWith(",")) {
strb = strb.deleteCharAt(strb.length() - 1);
}
}
String[] strs = strb.toString().split("/");
String id = strs[strs.length-1];
String metaUri = strb.toString().replace(id, "meta");
metaUri = metaUri + "/" + id;
JsonObject jo = this.mc.get(metaUri);
return new Attachment(jo, this.mc);
}
}