Skip to content

Commit

Permalink
Merge pull request #745 from sachindakumara/life-cycle-module
Browse files Browse the repository at this point in the history
INTGW-1240 fix new version email send issue
  • Loading branch information
aushaniU authored Jan 1, 2021
2 parents bbb7288 + 26e718b commit c181495
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public APICustomExecutor(){
public boolean execute(RequestContext context, String currentState, String targetState) {

boolean superExecuted = super.execute(context, currentState, targetState);
if(superExecuted && currentState.equalsIgnoreCase(Constants.STATE_CREATED) && targetState.equalsIgnoreCase(Constants.STATE_PUBLISHED)){
if(superExecuted && currentState.equalsIgnoreCase(Constants.STATE_CREATED) && targetState.equalsIgnoreCase(Constants.STATE_PUBLISHED) && notificationService.validateIsNotNewApiVersion(context)){
notificationService.sendApiProviderEmail(context);
}


return superExecuted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public interface NotificationService {

void sendApiProviderEmail(RequestContext context);

boolean validateIsNotNewApiVersion(RequestContext context);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import com.wso2telco.dep.lifecycleextension.util.EmailNotificationUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.api.APIProvider;
import org.wso2.carbon.apimgt.api.model.API;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.apimgt.impl.utils.APIVersionComparator;
import org.wso2.carbon.governance.api.generic.GenericArtifactManager;
import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact;
import org.wso2.carbon.registry.core.jdbc.handlers.RequestContext;
Expand Down Expand Up @@ -53,12 +56,8 @@ public void sendApiProviderEmail(RequestContext context) {

try {

GenericArtifactManager artifactManager = APIUtil.getArtifactManager(context.getSystemRegistry(),
APIConstants.API_KEY);
API api = getApiDetails(context);
log.info("Starting email trigger functionality in API creation");
String artifactId = context.getResource().getUUID();
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(artifactId);
API api = APIUtil.getAPI(apiArtifact);

List<String> userList = Arrays.asList(remoteUserStoreManager.getUserListOfRole(Constants.ADMIN_ROLE));

Expand Down Expand Up @@ -87,4 +86,41 @@ public void sendApiProviderEmail(RequestContext context) {
log.error("Failed to validate user details for send email ", e);
}
}
}

@Override
public boolean validateIsNotNewApiVersion(RequestContext context) {

try {

API api = getApiDetails(context);

String provider = APIUtil.replaceEmailDomain(api.getId().getProviderName());
APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(provider);
List<API> oldApiList = apiProvider.getAPIsByProvider(provider);

if(!oldApiList.isEmpty()){
APIVersionComparator versionComparator = new APIVersionComparator();
for (API oldApi : oldApiList) {
if(oldApi.getId().getApiName().equalsIgnoreCase(api.getId().getApiName()) && versionComparator.compare(oldApi, api) < 0 &&
(APIConstants.PUBLISHED.equals(oldApi.getStatus()))){
return false;
}
}
}

return true;
}catch (Exception e){
log.error("Failed to validate api version details ", e);
return false;
}
}

private API getApiDetails(RequestContext context)throws Exception{

GenericArtifactManager artifactManager = APIUtil.getArtifactManager(context.getSystemRegistry(),
APIConstants.API_KEY);
String artifactId = context.getResource().getUUID();
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(artifactId);
return APIUtil.getAPI(apiArtifact);
}
}

0 comments on commit c181495

Please sign in to comment.