1 /*
  2  * Copyright © [2008-2009] Novell, Inc.  All Rights Reserved.
  3  * 
  4  * USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE DEVELOPER LICENSE AGREEMENT
  5  * OR OTHER AGREEMENT THROUGH WHICH NOVELL, INC. MAKES THE WORK AVAILABLE.  THIS WORK 
  6  * MAY NOT BE ADAPTED WITHOUT NOVELL'S PRIOR WRITTEN CONSENT.
  7  * 
  8  * NOVELL PROVIDES THE WORK "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, 
  9  * INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR 
 10  * A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  NOVELL, THE AUTHORS OF THE WORK, AND THE 
 11  * OWNERS OF COPYRIGHT IN THE WORK ARE NOT LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER 
 12  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, 
 13  * OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS IN THE WORK.
 14  */
 15 
 16 /**
 17  * @fileoverview
 18  * This file defines internal routines to manage Identity class objects.
 19  * @see Identity
 20  */
 21 
 22 
 23 /**
 24  * Creates an identity JS Dataobject.
 25  * Implements the Identity API, contains additional helper fucntions and used the JSDataObject framework.
 26  * @private
 27  */
 28 JSDataObject(IdentityImpl, Packages.esecurity.db.object.IdentityFactory.getInstance(), {
 29     "IdentityGuid": {},
 30     "DistinguishedName": {
 31         required: true
 32     },
 33     "FullName": {},
 34     "FirstName": {},
 35     "LastName": {},
 36     "Department": {},
 37     "JobTitle": {},
 38     "OfficeCode": {},
 39     "Photo": {},
 40     "WorkForceId": {},
 41     "PrimaryEmail": {},
 42     "PrimaryPhone": {},
 43     "ManagerGuid": {},
 44     "SrcIdtyId": {},
 45     "CustId": {},
 46     "VaultName": {}
 47 });
 48 
 49 // Workaround for a deadlock caused by an invocation of a service method from within another service method.
 50 // By instantiating identity wrapper class avoids the deadlock caused by the service manager. 
 51 Packages.esecurity.db.object.IdentityFactory.getInstance().create();
 52         
 53 /**
 54  * Constructs an instance of the IdentityImpl class.
 55  * @class
 56  * Internal Identity implementation.
 57  * @constructor
 58  * @param {Hash} properties Intial properties to define identity
 59  * @private
 60  */
 61 function IdentityImpl(props) {
 62     try {
 63         if (props && props.constructor != Object && props.constructor != IdentityImpl) {
 64             throw "Identity must be initialized with an object";
 65         }
 66         this.toDataObject(props);
 67         if (props) {
 68             this.ExtAttrs = props.ExtAttrs;
 69         }
 70         this.OriginalExtAttrs = [];
 71     } 
 72     catch (e) {
 73         throw String(e.toString());
 74     }
 75 }
 76 
 77 /**
 78  * Attaches the extensible attributes, if any to the identity object that's passed in.
 79  * @param {Object} dobj Java Identity DataObject
 80  * @private
 81  */
 82 IdentityImpl.prototype.preSave = function(dobj) {
 83     try {
 84         var extAttrs = this.OriginalExtAttrs;
 85         var exts = {};
 86         
 87         for (var ext in this.ExtAttrs) {
 88             exts[ext] = this.ExtAttrs[ext];
 89         }
 90         
 91         // clean the existing extensible attributes first
 92         for (var j = 0; j < extAttrs.length; j++) {
 93             var attName = extAttrs[j].getName();
 94             if (attName in exts) {
 95                 extAttrs[j].setValue(exts[attName]);
 96                 delete exts[attName];
 97             }
 98             else {
 99                 dobj.removeIdentityExtAttributes(extAttrs[j]);
100             }
101         }
102         
103         // add all the new extensible attributes to the identity
104         for (ext in exts) {
105             var idtyExt = Packages.esecurity.db.object.IdentityExtAttributesFactory.create();
106             idtyExt.setName(ext);
107             idtyExt.setValue(exts[ext]);
108             dobj.addIdentityExtAttributes(idtyExt);
109         }
110     } 
111     catch (e) {
112         throw String(e.toString());
113     }
114 };
115 
116 /**
117  * Retrieves the extensible attributes asscoiated to the dataobject and add as a property to the Identity JS object.
118  * @param {Object} dobj Java Identity DataObject
119  * @private
120  */
121 IdentityImpl.prototype.postFind = function(dobj) {
122     try {
123         // get all the extensible attributes associated with the identity object
124         var extAttrs = dobj.getExtAttr().toArray();
125         
126         // create the JSON objects of the extensible attibutes
127         var atts = {};
128         for (var j = 0; j < extAttrs.length; j++) {
129             var extAtr = extAttrs[j];
130             var attName = String(extAtr.getName());
131             var attValue = String(extAtr.getValue());
132             atts[attName] = attValue;
133         }
134         
135         // attach the ext attributes to the identity
136         this.ExtAttrs = atts;
137         this.OriginalExtAttrs = extAttrs;
138     } 
139     catch (e) {
140         throw String(e.toString());
141     }
142 };
143 
144 /**
145  * Retrieves the identity guid from the object that was saved and adds to the identity JS object and also returns the guid
146  * @param {Object} dobj Java Identity DataObject
147  * @return {String} guid identity guid associated with the object
148  * @private
149  */
150 IdentityImpl.prototype.postSave = function(dobj) {
151     try {
152         var guid = String(dobj.getIdentityGuid());
153         // set the identity guid back on to the JS object
154         this.IdentityGuid = guid;
155         // return the GUID of the object that was just saved
156         return guid;
157     } 
158     catch (e) {
159         throw String(e.toString());
160     }
161 };
162 
163 /**
164  * UUID Generator
165  * @return {String} guid A new guid
166  * @private
167  */
168 IdentityImpl.genUUID = function(){
169     return String(Packages.com.esecurity.uuid.UUIDGenerator.generate());
170 };
171 
172 /**
173  * Attaches accounts to an identity.
174  * @param {Object} acctArr
175  * @private
176  */
177 IdentityImpl.prototype.attachAccounts = function(acctArr) {
178     try {
179         // get the java dataobject associated with the identity JS object
180         var dobj = this.unwrap();
181         
182         // add accounts to the identity
183         for (var acctCnt = 0; acctCnt < acctArr.length; acctCnt++) {
184             var acctObj = acctArr[acctCnt];
185             dobj.addAccount(acctObj.unwrap());
186         }
187     } 
188     catch (e) {
189         throw String(e.toString());
190     }
191 };
192 
193 /**
194  * Saves a batch of identities.
195  * @private
196  */
197 IdentityImpl.saveBatch = function() {
198 	var serviceName = Packages.esecurity.db.object.IdentityService.SERVICE_NAME;
199 	var serviceManager = Packages.esecurity.base.datamodel.service.ServiceManager.getInstance();
200 	var service = serviceManager.getService(serviceName);
201         
202     return function(idenarr, callback) {
203     	if (!(idenarr instanceof Array)) {
204 			throw "Expecting an array of objects as input";		
205     	}
206     	
207 		if (idenarr.length > 100) {
208 			throw "Batch size cannot exceed 100";
209 		}
210 
211     	var results = [];
212       var idenDataObjs = new java.util.LinkedList();
213 
214         for (var i = 0; i < idenarr.length; i++) {
215             var id = idenarr[i];
216             if (!id.IdentityGuid) {
217 							id.IdentityGuid = IdentityImpl.genUUID();
218 						}
219             results.push(id.IdentityGuid);
220             var dataObj = id.unwrap();
221             id.preSave(dataObj);
222             idenDataObjs.add(dataObj);
223 		}
224 
225 		var cback = new Packages.esecurity.db.object.IdentitySvcCallbackImpl({
226 	            setResults: function(){
227 	               	serviceManager.unregisterCallback(this);
228 	            },
229 	            setExceptionResults: function(error, origList){
230 	            	serviceManager.unregisterCallback(this);
231 	            	callback(idenarr,error);
232 	            	
233 	            }
234 	        });
235 		  
236         service.loadIdentities(idenDataObjs, cback);
237         return results;
238     };
239 }();