IBM-Maximo-API

47 | Introduction
This module includes a set of fluent API's for interfacing with Maximo by providing a high level abstraction for the
48 | Maximo REST API's. The intent is to shield developers from low level details and help them focus on their own implementation
49 | logic.
50 | Installing
Assumes Node and Express are installed.
51 | $ npm install --save ibm-maximo-api
52 | Usage
There are three main components ....
53 | Requiring Maximo
Like any other Node.js module the "maximo" module has to be loaded and assigned a local reference in your code. The easiest
54 | way to do this is using the built-in require() function.
55 | var Maximo = require('ibm-maximo-api');
The require() function returns a prototype (class) and assigns it to the local variable Maximo in the example above.
56 | Constructor and Authentication
After a local reference has been assigned using the require() function you can easily instantiate an object like this:
57 | var maximo = new Maximo(options,[authcookie]);
The constructor accepts two parameters:
58 | options: This parameter is REQUIRED and is represented by an object like this:
59 | {
60 | protocol: 'http',
61 | hostname: 'qawin03.swg.usma.ibm.com',
62 | port: '9080',
63 | user: 'wilson',
64 | password: 'wilson',
65 | auth_scheme: '/maximo',
66 | authtype:'maxauth',
67 | islean:1
68 | }
69 |
70 | authcookie: This parameter is OPTIONAL.
71 | If this parameter is null the Create, Read, Update and Delete (CRUD) api's will attempt to
72 | authenticate with Maximo everytime a CRUD operation is invoked.
If CRUD's are invoked multiple times it is recommended to authenticate first via the authenticate() function. If the
73 | authentication is sucessful a token(jsessionID) will be returned. Save the token in the request session so it can be
74 | read and passed into the constructor for subsequent requests. The following code snippet illustrates a GET route that
75 | authenticates with maximo and stores the token inside the session.
76 | app.get('/authenticate', function(req, res)
77 | {
78 | var maximo = new Maximo(options);
79 | maximo.authenticate()
80 | .then(function(jsessionid)
81 | {
82 | req.session.authcookie = jsessionid; // Set the token in the session so we can use it for future requests
83 | res.json(jsessionid); // Handle the response after setting the token in the session.
84 | })
85 | .fail(function (error)
86 | {
87 | console.log('****** Error Code = '+error);
88 | });
89 | }
The authenticate() function is asynchronous therefore it returns a defered Promise
90 | which is fulfilled inside the then() function or the fail() function if the promise is rejected.
91 | In either case the response is handled inside the callback as illustrated in the code snippet above.
92 | Fetch
The following code snippet illustrates how to use the fetch API. This example returns a ResourceSet and uses all the basic
93 | expressions available.
94 | router.get('/test_resource_set', function(req, res)
95 | {
96 | var maximo = new Maximo(options);
97 | maximo.resourceobject("MXWODETAIL")
98 | .select(["wonum","description","location","status","assetnum.description"])
99 | .where("status").in(["WAPPR","APPR"])
100 | .and("worktype").equal('CM')
101 | .orderby('wonum','desc')
102 | .pagesize(20)
103 | .fetch()
104 | .then(function(resourceset)
105 | {
106 | jsondata = resourceset.thisResourceSet();
107 | res.json(jsondata);
108 | })
109 | .fail(function (error)
110 | {
111 | console.log('****** Error Code = '+error);
112 | });
113 | });
Next Page
The following code snippet illustrates the Paging api. In this example we are assuming that the initial set is fetched
114 | with the pagesize() set to a low number like 20 and stored in the session (req.session.resourcesetjson).
115 | router.get('/test_nextpage', function(req, res)
116 | {
117 | var authcookie = req.session.authcookie;
118 | var maximo = new Maximo(options,authcookie);
119 | maximo.resourceobject("MXWODETAIL")
120 | .nextpage(req.session.resourcesetjson) // The paged resource is stored in session
121 | .then(function(resourceset)
122 | {
123 | if(resourceset)
124 | {
125 | jsondata = resourceset.JSON();
126 | req.session.resourcesetjson = jsondata; /// Store it in the session
127 | res.json(jsondata);
128 | }
129 | res.json({"status":"End of page"})
130 | })
131 | .fail(function (error)
132 | {
133 | console.log('****** Error Code = '+error);
134 | });
135 | });
Create
The following code snippet illustrates the Create api. In this example we are creating a new Workorder.
136 | router.get('/test_create', function(req, res)
137 | {
138 | var wo = '';
139 | var required =
140 | {
141 | "spi:description":"Created from API",
142 | "spi:siteid":"BEDFORD"
143 | }
144 | var authcookie = req.session.authcookie;
145 | var maximo = new Maximo(options,authcookie);
146 |
147 | maximo.resourceobject("MXWODETAIL")
148 | .create(required,["spi:wonum","spi:description"])
149 | .then(function(resource)
150 | {
151 | jsondata = resource.JSON();
152 | res.json(jsondata);
153 | })
154 | .fail(function (error)
155 | {
156 | console.log('****** Error Code = '+error);
157 | });
158 | });
Update
The following code snippet illustrates the Update api. In this example we are assuming the resourceset is saved
159 | in the session (req.session.myresourceset) and we are updating the first record in the set by passing
160 | the resource URL (req.session.myresourceset[0]["rdf:about"]). The resource URL for the update is contained in "rdf:about".
161 | router.get('/test_update', function(req, res)
162 | {
163 | var wo = '';
164 | var updates =
165 | {
166 | "spi:description":"Updated from Node API - test crudconnector",
167 | "spi:siteid":"BEDFORD"
168 | }
169 | // Assuming myresourceset was previously placed in session
170 | var authcookie = req.session.authcookie;
171 | var maximo = new Maximo(options,authcookie);
172 | maximo.resourceobject("MXWODETAIL")
173 | .resource(req.session.myresourceset[0]["rdf:about"]) //Pass the URI
174 | .update(updates,["spi:wonum","spi:description"])
175 | .then(function(resource)
176 | {
177 | var jsondata = resource.JSON();
178 | res.json(jsondata);
179 | })
180 | .fail(function (error)
181 | {
182 | console.log('****** Error Code = '+error);
183 | });
184 | });
Delete
The following code snippet illustrates the Delete api. In this example we are assuming the resourceset is saved
185 | in the session (req.session.myresourceset) and we are updating the first record in the set by passing
186 | the resource URL (req.session.myresourceset[0]["rdf:about"]). The resource URL for the update is contained in "rdf:about".
187 | router.get('/test_update', function(req, res)
188 | {
189 | // Assuming myresourceset was previously placed in session
190 | var authcookie = req.session.authcookie;
191 | var maximo = new Maximo(options,authcookie);
192 | maximo.resourceobject("MXWODETAIL")
193 | .resource(req.session.myresourceset[0]["rdf:about"]) //Pass the URI
194 | .delete(["spi:wonum","spi:description"])
195 | .then(function(resource)
196 | {
197 | var jsondata = resource.JSON();
198 | res.json(jsondata);
199 | })
200 | .fail(function (error)
201 | {
202 | console.log('****** Error Code = '+error);
203 | });
204 | });
Attachments
The following code snippet illustrates the Attachments api.
205 | router.get('/test_attachments', function(req, res)
206 | {
207 | getFileBytes('attachtestt.doc')
208 | .then(function(fileBuffer)
209 | {
210 | console.log("fileBuffer "+fileBuffer.length);
211 | var authcookie = req.session.authcookie;
212 | console.log("********* AuthCookie "+authcookie);
213 | var maximo = new Maximo(options,authcookie);
214 | //var maximo = new Maximo(options);
215 | maximo.resourceobject("MXWODETAIL")
216 | .select(["wonum","description","reportedby","location","status","assetnum.assetnum"])
217 | .where("wonum").equal('1459')
218 | .pagesize(20)
219 | .fetch()
220 | .then(function(resourceset)
221 | {
222 | req.session.myresourceset = resourceset.thisResourceSet();
223 | var rsrc = resourceset.resource(0);
224 | var meta = {
225 | name: 'pmr.doc',
226 | description: 'PMR Recreation Steps',
227 | type: 'FILE',
228 | storeas: 'Attachment',
229 | contentype: 'application/msword'
230 |
231 | };
232 | var attch = rsrc.attachment(meta);
233 | attch.create(fileBuffer)
234 | .then(function(resc)
235 | {
236 | console.log("Writing Attachment response ");
237 | //jsondata = rsrc.JSON();
238 | //res.json(jsondata);
239 | });
240 |
241 | })
242 | .fail(function (error)
243 | {
244 | console.log('****** Error Code = '+error);
245 | });
246 | });
247 | });
Contact
250 | License
(C) Copyright IBM Corp. 2015 All Rights Reserved
251 |