com.ibm.maximo.oslc.AttachmentSet Maven / Gradle / Ivy
/*
* 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 java.util.Map;
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 JsonArray ja;
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(this.jo.containsKey("rdfs:member")){
this.ja = this.jo.getJsonArray("rdfs:member");
}else{
this.ja = this.jo.getJsonArray("member");
}
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(this.jo.containsKey("rdfs:member")){
this.ja = this.jo.getJsonArray("rdfs:member");
}else{
this.ja = this.jo.getJsonArray("member");
}
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{
this.load(null);
return this;
}
public AttachmentSet load(Map headers) throws IOException, OslcException{
if(isLoaded){
return this;
}
if(headers!=null && !headers.isEmpty()){
this.jo = this.mc.get(this.href,headers);
}else{
this.jo = this.mc.get(this.href);
}
if(this.jo.containsKey("rdfs:member")){
this.ja = this.jo.getJsonArray("rdfs:member");
}else{
this.ja = this.jo.getJsonArray("member");
}
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());
this.reload();
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());
this.reload();
return new Attachment(obj,this.mc);
}
public Attachment create(String relation,Attachment att, Map headers) throws IOException, OslcException{
if(!this.href.contains(relation.toLowerCase()) || !this.href.contains(relation.toUpperCase())){
this.href+="/"+relation;
}
JsonObject obj;
if(headers != null && !headers.isEmpty()){
obj = this.mc.createAttachment(this.href,att.toDoc(), att.getName(),att.getDescription(),att.getMeta(), headers);
}else{
obj = this.mc.createAttachment(this.href,att.toDoc(), att.getName(),att.getDescription(),att.getMeta());
}
this.reload();
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();
}
if(index>=this.ja.size()){
return null;
}
JsonObject obj = (JsonObject)this.ja.get(index);
return new Attachment(obj,this.mc);
}
public Attachment member(String id) throws IOException, OslcException{
if(!isLoaded){
load();
}
JsonObject obj = null;
for(int i=0;i headers, String... properties)throws IOException, OslcException {
StringBuilder strb = new StringBuilder().append(uri);
if (properties.length > 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;
if(headers != null && !headers.isEmpty()){
jo = this.mc.get(metaUri, headers);
}else{
jo = this.mc.get(metaUri);
}
return new Attachment(jo, this.mc);
}
}