1 /**
  2  * Workflow to update/add/remove dictionary words
  3  */
  4 var REST = library.REST.REST();
  5 
  6 function updateWord(theData) {
  7 	logger.info("starting updateWord");
  8 	if (theData.uuid != null && theData.dictionaryid == null)
  9 	{
 10 		theData.dictionaryid = dictionaryManager.findDictionaryIdByUUID(theData.uuid);
 11 	}
 12 	if (theData.dictionaryname == null && theData.dictionaryid == null)
 13 	{
 14 		logger.warning("missing dictionary identifier (dictionaryname or dictionaryid)");
 15 		REST.pushError(
 16 				REST.errors.e400,
 17 				"missing dictionary identifier (dictionaryname or dictionaryid)"
 18 		);
 19 		return;
 20 	}
 21 	if (theData.word == null && theData.uuid == null)
 22 	{
 23 		logger.warning("missing word identifier (uuid or word)");
 24 		REST.pushError(
 25 				REST.errors.e400,
 26 				"missing word identifier (uuid or word)"
 27 		);
 28 		return;
 29 	}
 30 	if (theData.action != null && theData.action == "delete")
 31 	{
 32 		logger.info("delete requested");
 33 		if (theData.dictionaryname == null)
 34 		{
 35 			theData.dictionaryname = dictionaryManager.getDictionaryName(theData.dictionaryid);
 36 			if (theData.dictionaryname == '')
 37 			{
 38 				logger.warning("incorrect dictionary identifier (dictionaryid)");
 39 				REST.pushError(
 40 						REST.errors.e400,
 41 						"incorrect dictionary identifier (dictionaryid)"
 42 				);
 43 				return;
 44 			}
 45 		}
 46 		if (!dictionaryManager.removeWord(theData.dictionaryname, theData.word == null ? theData.uuid : theData.word))
 47 		{
 48 			logger.warning("unable to delete word " + theData.word + " " + theData.uuid);
 49 			REST.pushError(
 50 					REST.errors.e400,
 51 					"unable to delete word, see system logs"
 52 			);
 53 			return;
 54 		}
 55 		logger.info("word removed successfully " + theData.word + " " + theData.uuid);
 56 		return;
 57 	}
 58 	logger.info("updating word");
 59 	if (theData.uuid != null && dictionaryManager.findDictionaryIdByUUID(theData.uuid) > -1)
 60 	{
 61 		logger.info("updating word");
 62 		if (theData.word != null)
 63 		{
 64 			dictionaryManager.renameWordByUUID(theData.uuid, theData.word);
 65 		}
 66 		if (theData.thesaurus != null || theData.synonyms != null)
 67 		{
 68 			dictionaryManager.updateWordByUUID(theData.uuid, theData.synonyms, theData.thesaurus);
 69 		}
 70 		return;
 71 	}
 72 	logger.info("adding word");
 73 	if (theData.word == null)
 74 	{
 75 		logger.warning("missing word identifier (word)");
 76 		REST.pushError(
 77 				REST.errors.e400,
 78 				"missing word identifier (word)"
 79 		);
 80 		return;
 81 	}
 82 	if (theData.dictionaryname == null)
 83 	{
 84 		theData.dictionaryname = dictionaryManager.getDictionaryName(theData.dictionaryid);
 85 		if (theData.dictionaryname == '')
 86 		{
 87 			logger.warning("incorrect dictionary identifier (dictionaryid)");
 88 			REST.pushError(
 89 					REST.errors.e400,
 90 					"incorrect dictionary identifier (dictionaryid)"
 91 			);
 92 			return;
 93 		}
 94 	}
 95 	var aWordUUID = dictionaryManager.addWord(theData.dictionaryname, theData.word, theData.synonyms, theData.thesaurus);
 96 	if (aWordUUID == null)
 97 	{
 98 		logger.warning("unable to add word " + theData.word);
 99 		REST.pushError(
100 				REST.errors.e400,
101 				"unable to add word, see system logs"
102 		);
103 		return;
104 	}
105 	logger.info("word added successfully " + theData.word + " " + aWordUUID);
106 	REST.push(null, {"word":theData.word, "uuid":aWordUUID});
107 }
108 
109 /**
110  * @name Word
111  * @class Update/Add/Remove dictionary words
112  * @description Endpoint for managing words within dictionaries.  New terms can be created as well as updating existing
113  * @param [dictionaryname] the name of the dictionary to update/create the word from.  This or dictionaryid are required
114  * @param [dictionaryid] the id of the dictionary to update/create the word from.  This or dictionaryname are required
115  * @param [word] The word to create or find to update
116  * @param [uuid] The uuid to look for the word with.  If not found, will be applied as the uuid for the new word
117  * @param [thesaurus] thesaurus content to apply
118  * @param [synonyms] synonym content to apply
119  * @param [action] to remove a word, include this parameter with the value "delete"
120  * @returns ["UUID of added word"]
121  * @example /wf/restapi/v2/word (delete UK from dictionary with ID 4)
122  *
123  * Parameters:
124  * data={"word":"UK","dictionaryid":4,"action":"delete"}
125  *
126  * Response:
127  * []
128  *
129  * @example /wf/restapi/v2/word (delete UK from region dictionary)
130  *
131  * Parameters:
132  * data={"word":"UK","dictionaryname":"region","action":"delete"}
133  *
134  * Response:
135  * []
136  *
137  * @example /wf/restapi/v2/word (delete word with UUID of 56B58674-10CD-43DD-A76A-4F54B495F332 from dictionary)
138  *
139  * Parameters:
140  * data={"uuid":"56B58674-10CD-43DD-A76A-4F54B495F332","action":"delete"}
141  *
142  * Response:
143  * []
144  *
145  * @example /wf/restapi/v2/word (update synonyms and thesaurus columns for a word with UUID of 56B58674-10CD-43DD-A76A-4F54B495F332)
146  *
147  * Parameters:
148  * data={"uuid":"8D083C34-743C-469D-8C3F-E94F76D00705","synonyms":"United Kingdom, England","thesaurus":"Paradise"}
149  *
150  * Response:
151  * []
152  *
153  * @example /wf/restapi/v2/word (update a word based on the UUID assigned.  If the UUID does not match an existing, the word will be created)
154  *
155  * Parameters:
156  * data={"uuid":"8D083C34-743C-469D-8C3F-E94F76D00705","word":"tacos"}
157  *
158  * Response:
159  * []
160  *
161  * @example /wf/restapi/v2/word (Create a new word in the dictionary passed (can also use dictionaryid parameter).  If the word is successfully created, the UUID will be returned.
162  *
163  * Parameters:
164  * data={"word":"tacos2","dictionaryname":"region"}
165  *
166  * Response:
167  * [{"word":"tacos2","uuid":"EF49FF93-ECB5-4533-AC7E-A9C5438FBC82"}]
168  *
169  * @example /wf/restapi/v2/word (Create new words in the dictionary passed (can also use dictionaryid parameter).  If the words are successfully created, UUIDs for each will be returned.
170  *
171  * Parameters:
172  * data=[{"word":"North%20America","dictionaryname":"region"},{"word":"APAC","dictionaryname":"region"},{"word":"EURO","dictionaryname":"region"}]
173  *
174  * Response:
175  * [{"word": "North America","uuid": "598D65DE-CEBC-438C-B1C4-936A88FFA7B3"},
176  * {"word": "APAC","uuid": "34A101F0-0A36-4E31-93ED-ECF196F27241"},
177  * {"word": "EURO","uuid": "D247992F-7720-4E7A-9891-8A9D89AA9230"}]
178  */
179 function main() {
180 	if (!context.getUser().isPermissionEnabled("dictionaries")) {
181 		REST.pushError(REST.errors.e403, "dictionaries permission is not enabled");
182 		return REST.execute();
183 	}
184 	REST.setCallback(updateWord);
185 	return REST.execute("data");
186 }
187 main();
188