FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
change-license-common.js
1 /*
2  Copyright (C) 2014-2018, Siemens AG
3  Author: Daniele Fognini, Johannes Najjar, Steffen Weber
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
20 var magicNumberNoLicenseFoundInt = 507;
21 var magicNumberNoLicenseFound = "507";
22 var noLicenseString = "No_license_found";
23 
24 
25 function jsArrayFromHtmlOptions(pListBox) {
26  var options = new Array(pListBox.options.length);
27  for (var i = 0; i < options.length; i++) {
28  if (pListBox.options[i].value === magicNumberNoLicenseFound) {
29  continue;
30  }
31  options[i] = pListBox.options[i];
32  }
33  return options;
34 }
35 
36 function htmlOptionsFromJsArray(pListBox, options) {
37  pListBox.options.length = 0;
38  if(options===undefined) { return;
39  }
40  for (var i = 0; i < options.length; i++) {
41  if(options[i]===undefined) { continue;
42  }
43  pListBox.options[i] = options[i];
44  }
45 }
46 
47 function sortLicenseList(pListBox) {
48  var options = jsArrayFromHtmlOptions(pListBox);
49  options.sort(compareText);
50  if (options.length == 0) {
51  options[0] = (new Option(noLicenseString, magicNumberNoLicenseFound));
52  }
53  htmlOptionsFromJsArray(pListBox, options);
54 }
55 
56 function compareText(opt1, opt2) {
57  return opt1.text < opt2.text ? -1 : opt1.text > opt2.text ? 1 : 0;
58 }
59 
60 function moveLicense(theSelFrom, theSelTo) {
61  var selLength = theSelFrom.length;
62  for (var i = selLength - 1; i >= 0; i--) {
63  if (theSelFrom.options[i].selected) {
64  theSelTo.appendChild(theSelFrom.options[i].cloneNode(true));
65  theSelFrom[i] = null;
66  }
67  }
68 
69  sortLicenseList(theSelFrom);
70  sortLicenseList(theSelTo);
71 }
72 
73 
74 function selectNoLicenseFound(left, right) {
75  var selLength = right.length;
76  var i;
77  for (i = selLength - 1; i >= 0; i--) {
78  left.options[left.options.length] = (new Option(right.options[i].text, right.options[i].value));
79  right[i] = null;
80  }
81  selLength = left.length;
82  for (i = selLength - 1; i >= 0; i--) {
83  if (left.options[i].value == magicNumberNoLicenseFound) {
84  left[i] = null;
85  }
86  }
87  right.options[right.options.length] = (new Option(noLicenseString, magicNumberNoLicenseFound));
88  sortList(left);
89 }
90 
91 
92 function scheduledDeciderSuccess (data, resultEntity, callbackSuccess, callbackCloseModal) {
93  var jqPk = data.jqid;
94  if (jqPk) {
95  resultEntity.html("scan scheduled as " + linkToJob(jqPk));
96  if (callbackSuccess) {
97  queueUpdateCheck(jqPk, callbackSuccess);
98  }
99  callbackCloseModal();
100  } else {
101  resultEntity.html("bad response from server");
102  }
103  resultEntity.show();
104 }
105 
106 function scheduledDeciderError (responseobject, resultEntity) {
107  var error = responseobject.responseJSON.error;
108  if (error) {
109  resultEntity.text("error: " + error);
110  } else {
111  resultEntity.text("error");
112  }
113  resultEntity.show();
114 }
115 
116 function isUserError(bulkActions, refText) {
117  var errorText = "";
118  if(bulkActions.length < 1) {
119  errorText += "No licenses to bulk scan selected\n";
120  }
121 
122  if(refText.trim().split(" ").length < 2) {
123  errorText += "Reference text needs to be at least 2 words long"
124  }
125 
126  //show errors to user
127  if(errorText.length > 0) {
128  alert("Bulk scan not scheduled: \n\n"+errorText);
129  return true;
130  }
131  return false;
132 }
133 
134 function scheduleBulkScanCommon(resultEntity, callbackSuccess) {
135  var bulkActions = getBulkFormTableContent();
136  var refText = $('#bulkRefText').val();
137  var i;
138  for (i = 0; i < bulkActions.length; i++) {
139  bulkActions[i]["reportinfo"] = $("#"+bulkActions[i].licenseId+"reportinfoBulk").attr('title');
140  bulkActions[i]["acknowledgement"] = $("#"+bulkActions[i].licenseId+"acknowledgementBulk").attr('title');
141  bulkActions[i]["comment"] = $("#"+bulkActions[i].licenseId+"commentBulk").attr('title');
142  }
143 
144  //checks for user errors
145  if(isUserError(bulkActions, refText)) {
146  return;
147  }
148 
149  var post_data = {
150  "bulkAction": bulkActions,
151  "refText": refText,
152  "bulkScope": $('#bulkScope').val(),
153  "uploadTreeId": $('#uploadTreeId').val(),
154  "forceDecision": $('#forceDecision').is(':checked')?1:0
155  };
156 
157  resultEntity.hide();
158 
159  $.ajax({
160  type: "POST",
161  url: "?mod=change-license-bulk",
162  data: post_data,
163  success: function(data) { scheduledDeciderSuccess(data, resultEntity, callbackSuccess, closeBulkModal); },
164  error: function(responseobject) { scheduledDeciderError(responseobject, resultEntity); }
165  });
166 
167 }
168 
169 function performPostRequestCommon(resultEntity, callbackSuccess) {
170  var txt = [];
171  $('#licenseRight option').each(function () {
172  txt.push(this.value);
173  });
174 
175  var data = {
176  "licenseNumbersToBeSubmitted": txt,
177  "uploadTreeId": $('#uploadTreeId').val(),
178  "removed": removed
179  };
180 
181  $.ajax({
182  type: "POST",
183  url: "?mod=change-license-processPost",
184  data: data,
185  success: function (data) { scheduledDeciderSuccess(data,resultEntity, callbackSuccess, closeUserModal); },
186  error: function(responseobject) { scheduledDeciderError(responseobject, resultEntity); }
187  });
188 
189 }
190 
191 function popUpLicenseText(popUpUri, title) {
192  sel = $("#bulkLicense :selected").val();
193  window.open(popUpUri + sel, title, 'width=600,height=400,toolbar=no,scrollbars=yes,resizable=yes');
194 }
195 
196 function modifyLicense(doWhat ,uploadId, uploadTreeId, licenseId) {
197  $.getJSON("?mod=conclude-license&do=" + doWhat + "&upload=" + uploadId + "&item=" + uploadTreeId + "&licenseId=" + licenseId)
198  .done(function (data) {
199  if(data) {
200  $('#decTypeSet').addClass('decTypeWip');
201  }
202  var table = createClearingTable();
203  table.fnDraw(false);
204  })
205  .fail(failed);
206 }
207 
208 function addLicense(uploadId, uploadTreeId, licenseId) {
209  modifyLicense('addLicense', uploadId, uploadTreeId, licenseId);
210 }
211 
212 function removeLicense(uploadId, uploadTreeId, licenseId) {
213  modifyLicense('removeLicense', uploadId, uploadTreeId, licenseId);
214 }
215 
216 
217 function makeMainLicense(uploadId, licenseId) {
218  $.getJSON("?mod=conclude-license&do=makeMainLicense&upload=" + uploadId + "&licenseId=" + licenseId)
219  .done(function (data) {
220  var table = createClearingTable();
221  table.fnDraw(false);
222  })
223  .fail(failed);
224 }
225 
226 function removeMainLicense(uploadId,licenseId) {
227  if(confirm("Remove this license from the main license list?"))
228  {
229  $.getJSON("?mod=conclude-license&do=removeMainLicense&upload=" + uploadId + "&licenseId=" + licenseId)
230  .done(function (data) {
231  var table = createClearingTable();
232  table.fnDraw(false);
233  })
234  .fail(failed);
235  }
236 }
237 function openTextModel(uploadTreeId, licenseId, what, type) {
238  if(type === undefined) {
239  type = 0;
240  }
241  if(type == 0) {
242  let clearingsForSingleFile = $("#clearingsForSingleFile"+licenseId+what).attr("title");
243  idLicUploadTree = uploadTreeId+','+licenseId;
244  whatCol = what;
245  $("#referenceText").val(clearingsForSingleFile);
246  if (what == 4 || what == "comment") {
247  createDropDown($("#textModal > form > div"), $("#referenceText"));
248  } else {
249  $("#licenseStdCommentDropDown-text").hide();
250  $("#licenseStdCommentDropDown").next(".select2-container").hide();
251  }
252  textModal.plainModal('open');
253  } else {
254  $("#referenceText").val($("#"+licenseId+what+type).attr('title'));
255  whatCol = what;
256  whatLicId = licenseId;
257  if (what == 4 || what == "comment") {
258  createDropDown($("#textModal > form > div"), $("#referenceText"));
259  } else {
260  $("#licenseStdCommentDropDown-text").hide();
261  $("#licenseStdCommentDropDown").next(".select2-container").hide();
262  }
263  textModal.plainModal('open');
264  }
265  whatType = type;
266 }
267 
268 function closeTextModal() {
269  textModal.plainModal('close');
270 }
271 
272 function submitTextModal(){
273  if(whatType == 0) {
274  var post_data = {
275  "id": idLicUploadTree,
276  "columnId": whatCol,
277  "value": $("#referenceText").val()
278  };
279  $.ajax({
280  type: "POST",
281  url: "?mod=conclude-license&do=updateClearings",
282  data: post_data,
283  success: doOnSuccess
284  });
285  } else {
286  textModal.plainModal('close');
287  $("#"+ whatLicId + whatCol +"Bulk").attr('title', $("#referenceText").val());
288  referenceText = $("#referenceText").val().trim();
289  if(referenceText !== null && referenceText !== '') {
290  $("#"+ whatLicId + whatCol + whatType).html($("#"+ whatLicId + whatCol + whatType).attr('title').slice(0, 10) + "...");
291  } else {
292  $("#"+ whatLicId + whatCol +"Bulk").attr('title','');
293  }
294  }
295 }
296 
297 function checkIfEligibleForGlobalDecision()
298 {
299  var checkBox = document.getElementById("globalDecision");
300  if (checkBox.checked == true) {
301  $.ajax({
302  type: "POST",
303  url: "?mod=conclude-license&do=checkCandidateLicense",
304  data: {"item": $('#uploadTreeId').val()},
305  success: function(data) {
306  if (data != 'success') {
307  alert(data);
308  checkBox.checked = false;
309  }
310  },
311  error: function(data) {
312  }
313  });
314  }
315 }
316 
317 function doOnSuccess() {
318  textModal.plainModal('close');
319  $('#decTypeSet').addClass('decTypeWip');
320  oTable = $('#licenseDecisionsTable').dataTable(selectedLicensesTableConfig).makeEditable(editableConfiguration);
321  oTable.fnDraw(false);
322 }
323 
324 $(document).ready(function () {
325  textModal = $('#textModal').plainModal();
326  $('#textModal').draggable({
327  stop: function(){
328  $(this).css({'width':'','height':''});
329  }
330  });
331 });
332 
333 function createDropDown(element, textBox) {
334  let dropDown = null;
335  if ($("#licenseStdCommentDropDown").length) {
336  // The dropdown already exists
337  $("#licenseStdCommentDropDown-text").show();
338  dropDown = $("#licenseStdCommentDropDown");
339  dropDown.val(null).trigger('change');
340  dropDown.next(".select2-container").show();
341  return;
342  }
343  dropDown = $("<select />", {
344  "id": "licenseStdCommentDropDown",
345  "class": "ui-render-select2"
346  }).on("select2:select", function(e) {
347  let id = e.params.data.id;
348  getStdLicenseComments(id, function (comment) {
349  if (comment.hasOwnProperty("error")) {
350  console.log("Error while fetching standard comments: " + comment.error);
351  } else {
352  textBox.val(textBox.val() + "\n" + comment.comment);
353  }
354  });
355  });
356  dropDown.insertBefore(element);
357  $("<p />", {
358  "id": "licenseStdCommentDropDown-text"
359  }).html("Select standard license comments:").insertBefore(dropDown);
360  getStdLicenseComments("visible", function (data) {
361  // Add a placeholder for select2
362  data.splice(0, 0, {
363  "lsc_pk": -1,
364  "name": ""});
365  dropDown.select2({
366  selectOnClose: true,
367  dropdownParent: textModal,
368  placeholder: {
369  id: '-1',
370  text: "Select a comment"
371  },
372  data: $.map(data, function(obj) {
373  return {
374  "id": obj.lsc_pk,
375  "text": obj.name
376  };
377  })
378  });
379  });
380 }
381 
382 function getStdLicenseComments(scope, callback) {
383  $.ajax({
384  type: "GET",
385  url: "?mod=ajax_license_std_comments",
386  data: {"scope": scope},
387  success: function(data) {
388  callback(data);
389  },
390  error: function(data) {
391  callback(data.error);
392  }
393  });
394 }
stop($application)
stop the application Assumes application is restartable via /etc/init.d/<script>. The application pas...