Skip to content

Commit

Permalink
Merge branch 'aiccra-dev' into aiccra-dev-A2-392-IPI-2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
MetalPrime authored May 31, 2024
2 parents 81e2e42 + 5ecc81e commit 8152fec
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*****************************************************************
* This file is part of Managing Agricultural Research for Learning &
* Outcomes Platform (MARLO).
* MARLO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
* MARLO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with MARLO. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************/

package org.cgiar.ccafs.marlo.action.json.global;

import org.cgiar.ccafs.marlo.action.BaseAction;
import org.cgiar.ccafs.marlo.utils.APConfig;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.apache.struts2.dispatcher.Parameter;


public class ProgressActiveValidationAction extends BaseAction {


/**
*
*/
private static final long serialVersionUID = 8027160696406597679L;
/**
*
*/
private Map<String, String> status;

@Inject
public ProgressActiveValidationAction(APConfig config) {
super(config);
}

@Override
public String execute() throws Exception {

status = new HashMap<>();

if (this.isUpKeepActive()) {
status.put("isProgress", "true");
} else {
status.put("isProgress", "false");
}

return SUCCESS;
}

public Map<String, String> getStatus() {
return status;
}

@Override
public void prepare() throws Exception {
Map<String, Parameter> parameters = this.getParameters();
}


public void setStatus(Map<String, String> keyOutputs) {
this.status = keyOutputs;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,21 @@
import org.cgiar.ccafs.marlo.utils.SendMailS;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.stream.Collectors;

import javax.inject.Inject;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -448,13 +452,6 @@ public String save() {
boolean postFailed = false;
if (this.isAiccra()) {

Map<String, Object> request = new HashMap<>();
request.put("name", partnerRequest.getPartnerName());
request.put("acronym", partnerRequest.getAcronym());
request.put("websiteLink", partnerRequest.getWebPage());
request.put("institutionTypeCode", partnerRequest.getInstitutionType().getId());
request.put("hqCountryIso", partnerRequest.getLocElement().getIsoAlpha2());

// setting requestSource CLARISA parameter
String requestSource;
if (project != null && project.getAcronym() != null) {
Expand All @@ -464,30 +461,8 @@ public String save() {
} else {
requestSource = "Request made from AICCRA";
}
request.put("requestSource", requestSource);

request.put("misAcronym", "aiccra");
request.put("userId", this.getCurrentUser().getId());
request.put("externalUserMail", this.getCurrentUser().getEmail());
request.put("externalUserName", this.getCurrentUser().getUsername());
request.put("externalUserComments", "Request made from AICCRA");

ObjectMapper mapper = new ObjectMapper();

try {
String requestStr = mapper.writeValueAsString(request);

ExternalPostUtils epu = new ExternalPostUtils();
epu.setUsername(config.getClarisaAPIUsername());
epu.setPassword(config.getClarisaAPIPassword());
String responseStr = epu.postJson(config.getClarisaAPIHost() + "/api/partner-requests/create", requestStr);

if (responseStr.isEmpty()) {
postFailed = true;
}
} catch (JsonProcessingException ex) {
java.util.logging.Logger.getLogger(PartnersSaveAction.class.getName()).log(Level.SEVERE, null, ex);
}
this.sendPartnerRequest(partnerRequestModifications, requestSource, postFailed);

} else {
partnerRequest = partnerRequestManager.savePartnerRequest(partnerRequest);
Expand Down Expand Up @@ -535,6 +510,60 @@ public String save() {
}
}

/**
* send request to CLARISA,to create a partner
*
* @param PartnerRequest partnerRequest
* @param String requestSource
* @param boolean postFailed
* @author IBD
*/
public void sendPartnerRequest(PartnerRequest partnerRequest, String requestSource, boolean postFailed) {

try {

ExternalPostUtils epu = new ExternalPostUtils();
epu.setUsername(config.getClarisaAPIUsername());
epu.setPassword(config.getClarisaAPIPassword());
JSONObject jsonObject = new JSONObject();


jsonObject.put("name", partnerRequest.getPartnerName());
jsonObject.put("acronym", partnerRequest.getAcronym());
jsonObject.put("websiteLink", partnerRequest.getWebPage());
jsonObject.put("institutionTypeCode", partnerRequest.getInstitutionType().getId());
jsonObject.put("hqCountryIso", partnerRequest.getLocElement().getIsoAlpha2());

jsonObject.put("requestSource", requestSource);

jsonObject.put("misAcronym", "aiccra");
jsonObject.put("userId", this.getCurrentUser().getId());
jsonObject.put("externalUserMail", this.getCurrentUser().getEmail());
jsonObject.put("externalUserName", this.getCurrentUser().getUsername());
jsonObject.put("externalUserComments", "Request made from AICCRA");

HttpPost httpPost = new HttpPost(config.getClarisaAPIHost() + "/api/partner-requests/create");

httpPost.setEntity(new StringEntity(jsonObject.toString()));

httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
httpPost.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(epu.getUsername(), epu.getPassword()),
"UTF-8", false));

CloseableHttpClient httpClient = HttpClients.createDefault();

CloseableHttpResponse response = httpClient.execute(httpPost);

LOG.info(" Class [" + PartnersSaveAction.class.getName()
+ "] - function [sendPartnerRequest] - variable [response] " + response);
} catch (Exception e) {
postFailed = true;
java.util.logging.Logger.getLogger(PartnersSaveAction.class.getName()).log(Level.SEVERE, null, e);
}

}

public void setActivityID(int activityID) {
this.activityID = activityID;
}
Expand Down
8 changes: 8 additions & 0 deletions marlo-web/src/main/resources/struts-json.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
<param name="excludeNullProperties">true</param>
</result>
</action>

<action name="isProgressActive"
class="org.cgiar.ccafs.marlo.action.json.global.ProgressActiveValidationAction">
<result type="json">
<param name="noCache">true</param>
<param name="excludeNullProperties">true</param>
</result>
</action>

<action name="uploadFundingSource"
class="org.cgiar.ccafs.marlo.action.json.project.UploadFundingSourceFileAction">
Expand Down
48 changes: 44 additions & 4 deletions marlo-web/src/main/webapp/global/js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ $.widget.bridge('uitooltip', $.ui.tooltip);

// Global Vars
var yesnoEvent;
var isProgress;
var notyDefaultOptions = {
text: '',
layout: 'bottomRight',
Expand All @@ -20,13 +21,17 @@ var notyDefaultOptions = {
]
};


/**
* Global javascript must be here.
*/
$(document).ready(function () {

showNotificationMessages();

showHelpText();
validatePhase().then(function(isProgress){
showNotificationMessages();
}).catch(function(error){});

// Set elementsListComponent
setElementsListComponent();
Expand Down Expand Up @@ -176,6 +181,22 @@ $(document).ready(function () {
$buttons.find('.buttons-content').removeClass('positionFixedBot animated flipInX');
}
}

function validatePhase() {
return new Promise(function(resolve, reject) {
// Ajax
$.ajax({
url: baseURL + '/isProgressActive.do',
success: function(data) {
isProgress = data.status.isProgress;
resolve();
},
error: function(xhr, status, error) {
reject(error);
}
});
});
}

// Animate help text
function showHelpText() {
Expand All @@ -200,8 +221,11 @@ $(document).ready(function () {
} else if (messageSelector.length >= 1 && messageSelector.html().split(":")[0] != "message" && messageSelector.html().split(":")[1] === " deliverable.status.remaining") {
// SHOW CLUSTER SUBMITTED BASED ON THE DISABLED INPUT
var $clusterSubmitted = $(`.clusterSubmitted`);
var $clusterSubmittedFilter = $clusterSubmitted.filter((index, ele) => $(ele).attr("issubmit") === "true").get();
var message = "";
if ($clusterSubmitted.length > 0) {
console.log("ClusterSubmitted Lenght",$clusterSubmitted.length);
console.log("ClusterSubmitted", $clusterSubmitted);
if ($clusterSubmittedFilter.length > 0) {
// $clusterSubmitted exists, do something
const $mapClusterSubmit = $clusterSubmitted.filter((index, ele) => $(ele).attr("issubmit") === "true").get();
const $stringClusterSubmit = $mapClusterSubmit.reduce((prev,curr) => prev +$(curr).attr("name")+",","");
Expand All @@ -223,9 +247,17 @@ $(document).ready(function () {
} else if (messageSelector.length >= 1 && messageSelector.html().split(":")[0] != "message") {
// WARNING MESSAGE
var message = ""
message += "The Information was correctly saved. <br> ";
message += "Please keep in mind that the fields highlighted below are missing or incorrect.";
var messageType = "warning";
if(isProgress == 'true'){
message += "The Information was correctly saved. <br> ";
message += "Some of the fields could be missing or incorrect. <br>";
message += "Don't worry! Some information is not necessary at this phase, but it will be required in the next phase.";
messageType = "info";
}else{
message += "The Information was correctly saved. <br> ";
message += "Please keep in mind that the fields highlighted below are missing or incorrect.";
}

notifyErrorMessage(messageType, message);
}
} else if ($(messageSelector).hasClass("error")) {
Expand Down Expand Up @@ -262,6 +294,14 @@ $(document).ready(function () {
$(containerIcon).addClass("alertColorWarning");
$(messages).removeClass("displayNone");
break;
case "info":
$(element).find('.alertText').html(message);
$(iconAlert).attr("src", baseURL + '/global/images/icon-info.png');
$(containerAlert).addClass("alertColorBackgroundInfo");
$(containerLine).addClass("alertColorInfo");
$(containerIcon).addClass("alertColorInfo");
$(messages).removeClass("displayNone");
break;
case "error":
//Declaraciones ejecutadas cuando el resultado de expresión coincide con valorN
break;
Expand Down

0 comments on commit 8152fec

Please sign in to comment.