1 /**
  2  * Utilities to help the /wf/restapi/v2/admin/* endpoints
  3  */
  4 var build = function() {
  5 	var AdminUtils = {};
  6 
  7 	/**
  8 	 * Converts a Java object to a real js object so we can run it through JSON.stringify
  9 	 * Strips out the functions and object type
 10 	 * @param theObject
 11 	 * @returns {*}
 12 	 */
 13 	function toNative(theObject) {
 14 		var anObject = context.toNative(theObject);
 15 		for (var aKey in anObject) {
 16 			if (aKey === "<type>" || ((typeof anObject[aKey]) === 'string' && anObject[aKey].indexOf("function(") === 0)) {
 17 				delete anObject[aKey]
 18 			}
 19 		}
 20 		return anObject
 21 	}
 22 
 23 	/**
 24 	 * Converts a Group object to a native js object
 25 	 * @param theGroup
 26 	 * @returns {*}
 27 	 */
 28 	AdminUtils.jsGroup = function(theGroup) {
 29 		var aNative = toNative(theGroup);
 30 		aNative.acls = AdminUtils.jsArray(theGroup.getACLs(), function(theAcl){return theAcl.id});
 31 		aNative.users = AdminUtils.jsArray(theGroup.getUsers(), function(theUser){return theUser.userId});
 32 		if (aNative.primary != undefined) {
 33 			delete aNative.primary;
 34 		}
 35 		return aNative;
 36 	};
 37 
 38 	/**
 39 	 * Converts an ACL object to a native js object
 40 	 * @param theAcl
 41 	 * @returns {*}
 42 	 */
 43 	AdminUtils.jsAcl = function(theAcl) {
 44 	  if (!Boolean(theAcl)) {
 45       return null;
 46     }
 47 		var aNative = toNative(theAcl);
 48 		if (aNative.type !== undefined) {
 49 			delete aNative.type;
 50 		}
 51 		// Renaming isMasked to hidden because its better
 52 		if (aNative.isMasked !== undefined) {
 53 			delete aNative.isMasked;
 54 			aNative.hidden = theAcl.isMasked;
 55 		}
 56 		// Renaming userOrGroupId to groupId because its more accurate
 57 		if (aNative.userOrGroupId !== undefined) {
 58 			delete aNative.userOrGroupId;
 59 			aNative.groupId = theAcl.userOrGroupId;
 60 		}
 61 		aNative.revokedPermissions = AdminUtils.jsArray(theAcl.getRevokedPermissions(), function(theString) {return theString + ""});
 62 		try {
 63       aNative.rootPath = theAcl.getRoot().path;
 64     } catch (e) {
 65       aNative.rootPath = "";
 66     }
 67 		aNative.workspaces = AdminUtils.jsArray(theAcl.getWorkspaces(), function(theWorkspace) {return theWorkspace.getId()});
 68 		aNative.searchFilter = theAcl.getSearchFilter();
 69 		aNative.forms = AdminUtils.jsArray(theAcl.getVisibleMetaforms(), function(theString) {return theString + ""});
 70 		var anUploadForms = AdminUtils.jsArray(theAcl.getUploadMetaforms(), function(theString) {return theString + ""});
 71 		if (anUploadForms.length === 1) {
 72 			aNative.uploadForm = anUploadForms[0];
 73 		}
 74 		return aNative;
 75 	};
 76 
 77 	/**
 78 	 * Converts a User object to a native js object
 79 	 * @param theUser
 80 	 * @returns {*}
 81 	 */
 82 	AdminUtils.jsUser = function(theUser) {
 83 		var aNative = toNative(theUser);
 84 		if (!!aNative.login) {
 85 			delete aNative.login;
 86 		}
 87 		if (!!aNative.fullName) {
 88 			delete aNative.fullName;
 89 		}
 90 		aNative.type = theUser.getType();
 91 		aNative.status = theUser.getStatus();
 92 		aNative.primaryGroup = theUser.getPrimaryGroup() != null ? theUser.getPrimaryGroup().id : undefined;
 93 		aNative.groups = AdminUtils.jsArray(theUser.getGroups(), function(theGroup){return theGroup.id});
 94 		return aNative;
 95 	};
 96 
 97 	/**
 98 	 * Converts a java array to a js array and runs the objects in the array through the mapper function
 99 	 * @param theJavaArray
100 	 * @param theMapper
101 	 * @returns {Array}
102 	 */
103 	AdminUtils.jsArray = function(theJavaArray, theMapper) {
104 		var aJsArray = [];
105 		for (var i in theJavaArray) {
106 			if (theJavaArray[i] == null || theJavaArray[i] === undefined) {
107 				continue;
108 			}
109 			aJsArray.push(theMapper(theJavaArray[i]));
110 		}
111 		return aJsArray;
112 	};
113 
114 	/**
115 	 * Walks through 2 arrays and calls theAdder and theRemover to make theSrcArray match theCurrentArray
116 	 * @param theSrcArray
117 	 * @param theCurrentArray
118 	 * @param theAdder
119 	 * @param theRemover
120 	 * @returns {boolean}
121 	 */
122 	AdminUtils.resolveArrayDifferences = function(theSrcArray, theCurrentArray, theAdder, theRemover) {
123 		var aModified = false;
124 		// Find the objects we need to add
125 		for (var i in theSrcArray) {
126 			var anId = theSrcArray[i];
127 			if (theCurrentArray.indexOf(anId) < 0) {
128 				theAdder(anId);
129 				aModified = true;
130 			}
131 		}
132 		// Find the objects we need to remove
133 		for (var i in theCurrentArray) {
134 			var anId = theCurrentArray[i];
135 			if (theSrcArray.indexOf(anId) < 0) {
136 				theRemover(anId);
137 				aModified = true;
138 			}
139 		}
140 		return aModified;
141 	};
142 
143 	return AdminUtils;
144 };