Skip to content

Commit

Permalink
Updated applicationpatch for updating tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ashanthamara committed Oct 9, 2023
1 parent 9244ed8 commit 48ba85e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.wso2.carbon.identity.api.server.application.management.v1.ResidentApplication;
import org.wso2.carbon.identity.api.server.application.management.v1.SAML2Configuration;
import org.wso2.carbon.identity.api.server.application.management.v1.SAML2ServiceProvider;
import org.wso2.carbon.identity.api.server.application.management.v1.TagsPatchRequest;
import org.wso2.carbon.identity.api.server.application.management.v1.WSTrustConfiguration;
import org.wso2.carbon.identity.api.server.application.management.v1.core.functions.Utils;
import org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application.ApiModelToServiceProvider;
Expand All @@ -84,17 +83,13 @@
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo;
import org.wso2.carbon.identity.application.common.model.ApplicationTagsItem;
import org.wso2.carbon.identity.application.common.model.ApplicationTagsPatch;
import org.wso2.carbon.identity.application.common.model.ApplicationTagsPatch.ApplicationTagsPatchBuilder.OperationEnum;
import org.wso2.carbon.identity.application.common.model.AuthenticationStep;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.ImportResponse;
import org.wso2.carbon.identity.application.common.model.InboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig;
import org.wso2.carbon.identity.application.common.model.InboundConfigurationProtocol;
import org.wso2.carbon.identity.application.common.model.ListValue;
import org.wso2.carbon.identity.application.common.model.ListValue.ListValueBuilder;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.SpFileContent;
Expand Down Expand Up @@ -862,8 +857,6 @@ public String createApplication(ApplicationModel applicationModel, String templa
public void patchApplication(String applicationId, ApplicationPatchModel applicationPatchModel) {

ServiceProvider appToUpdate = cloneApplication(applicationId);
//Setting an empty array to avoid updating same tags again.
appToUpdate.setTags(new ArrayList<>());
if (applicationPatchModel != null) {
new UpdateServiceProvider().apply(appToUpdate, applicationPatchModel);
}
Expand All @@ -873,44 +866,12 @@ public void patchApplication(String applicationId, ApplicationPatchModel applica
String username = ContextLoader.getUsernameFromContext();
getApplicationManagementService()
.updateApplicationByResourceId(applicationId, appToUpdate, tenantDomain, username);
if (applicationPatchModel.getTags() != null) {
getApplicationManagementService().updateApplicationTags(appToUpdate.getApplicationID(),
getApplicationTagsPatchModel(applicationPatchModel.getTags()), tenantDomain);
}
} catch (IdentityApplicationManagementException e) {
String msg = "Error patching application with id: " + applicationId;
throw handleIdentityApplicationManagementException(e, msg);
}
}

private List<ApplicationTagsPatch> getApplicationTagsPatchModel(List<TagsPatchRequest> tagsPatchModel) {

List<ApplicationTagsPatch> applicationTagsPatch = new ArrayList<>();

for (TagsPatchRequest tagsPatch: tagsPatchModel) {
OperationEnum operation;
switch (tagsPatch.getOperation().toString()) {
case "ADD":
operation = OperationEnum.ADD;
break;
case "REMOVE":
operation = OperationEnum.REMOVE;
break;
default:
operation = null;
}

List<ListValue> patchTags = tagsPatch.getTags().stream()
.map(tag -> new ListValueBuilder().value(tag.getValue()).build())
.collect(Collectors.toList());

applicationTagsPatch.add(new ApplicationTagsPatch.ApplicationTagsPatchBuilder()
.operation(operation)
.tags(patchTags).build());
}
return applicationTagsPatch;
}

public void deleteApplication(String applicationId) {

String username = ContextLoader.getUsernameFromContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import org.wso2.carbon.identity.application.common.model.ApplicationTagsItem.ApplicationTagsItemBuilder;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.api.server.application.management.v1.core.functions.Utils.setIfNotNull;

Expand Down Expand Up @@ -104,10 +104,9 @@ private void addAdvancedConfigurationToApplication(ServiceProvider application,
private void addApplicationTagsToApplication(ServiceProvider application, List<ListValue> tags) {

if (tags != null) {
List<ApplicationTagsItem> tagsList = new ArrayList<>();
for (ListValue tag: tags) {
tagsList.add(new ApplicationTagsItemBuilder().id(tag.getValue()).build());
}
List<ApplicationTagsItem> tagsList = tags.stream()
.map(tag -> new ApplicationTagsItemBuilder().id(tag.getValue()).build())
.collect(Collectors.toList());
setIfNotNull(tagsList, application::setTags);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
*/
package org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application;

import org.springframework.util.CollectionUtils;
import org.wso2.carbon.identity.api.server.application.management.v1.AdvancedApplicationConfiguration;
import org.wso2.carbon.identity.api.server.application.management.v1.ApplicationPatchModel;
import org.wso2.carbon.identity.api.server.application.management.v1.AuthenticationSequence;
import org.wso2.carbon.identity.api.server.application.management.v1.ClaimConfiguration;
import org.wso2.carbon.identity.api.server.application.management.v1.ListValue;
import org.wso2.carbon.identity.api.server.application.management.v1.ProvisioningConfiguration;
import org.wso2.carbon.identity.api.server.application.management.v1.TagsPatchRequest;
import org.wso2.carbon.identity.api.server.application.management.v1.core.functions.UpdateFunction;
import org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application.provisioning.UpdateProvisioningConfiguration;
import org.wso2.carbon.identity.application.common.model.ApplicationTagsItem;
import org.wso2.carbon.identity.application.common.model.ApplicationTagsItem.ApplicationTagsItemBuilder;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;

import java.util.List;
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.api.server.application.management.v1.core.functions.Utils.setIfNotNull;

/**
Expand All @@ -44,6 +52,7 @@ public void apply(ServiceProvider serviceProvider, ApplicationPatchModel applica
patchAuthenticationSequence(applicationPatchModel.getAuthenticationSequence(), serviceProvider);
patchAdvancedConfiguration(serviceProvider, applicationPatchModel.getAdvancedConfigurations());
patchProvisioningConfiguration(applicationPatchModel.getProvisioningConfigurations(), serviceProvider);
patchApplicationTags(applicationPatchModel.getTags(), serviceProvider);
}

private void patchClaimConfiguration(ServiceProvider serviceProvider, ClaimConfiguration claimConfiguration) {
Expand Down Expand Up @@ -76,4 +85,28 @@ private void patchProvisioningConfiguration(ProvisioningConfiguration provisioni
new UpdateProvisioningConfiguration().apply(serviceProvider, provisioningConfigurations);
}
}

private void patchApplicationTags(List<TagsPatchRequest> tags, ServiceProvider serviceProvider) {

if (!CollectionUtils.isEmpty(tags)) {
List<String> tagIdList = serviceProvider.getTags().stream().map(ApplicationTagsItem::getId)
.collect(Collectors.toList());
for (TagsPatchRequest patchReq: tags) {

if (patchReq.getOperation().toString().equals("ADD")) {
List<String> addingTagIdList = patchReq.getTags().stream().map(ListValue::getValue)
.collect(Collectors.toList());
tagIdList.addAll(addingTagIdList);
continue;
}
if (patchReq.getOperation().toString().equals("REMOVE")) {
List<String> removingTagIdList = patchReq.getTags().stream().map(ListValue::getValue)
.collect(Collectors.toList());
tagIdList.removeAll(removingTagIdList);
}
}
serviceProvider.setTags(tagIdList.stream().map(id -> new ApplicationTagsItemBuilder().id(id).build())
.collect(Collectors.toList()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<parent>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.server.application.tag.management</artifactId>
<version>1.2.81-SNAPSHOT</version>
<version>1.2.85-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>org.wso2.carbon.identity.api.server.application.tag.management.common</artifactId>
<version>1.2.81-SNAPSHOT</version>
<version>1.2.85-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.server.application.tag.management</artifactId>
<version>1.2.81-SNAPSHOT</version>
<version>1.2.85-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public ApplicationTagsListResponse getAllApplicationTags(Integer offset, Integer
public ApplicationTagResponse getApplicationTagById(String tagId) {

try {
ApplicationTagsItem applicationTag = ApplicationTagManagementServiceHolder.getApplicationTagManager()
org.wso2.carbon.identity.application.common.model.ApplicationTagsListItem applicationTag =
ApplicationTagManagementServiceHolder.getApplicationTagManager()
.getApplicationTagById(tagId, CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
if (applicationTag == null) {
throw ApplicationTagMgtEndpointUtil.handleException(Response.Status.NOT_FOUND,
Expand Down Expand Up @@ -215,18 +216,19 @@ private ApplicationTag createApplicationTag(ApplicationTagModel applicationTag)
}

/**
* Build ApplicationTagsListItem from the ApplicationTagsItem.
* Build ApplicationTagResponse from the ApplicationTagsListItem.
*
* @param applicationTag Application Tag Item.
* @return ApplicationTagsListItem.
*/
private ApplicationTagResponse buildApplicationTagResponse(ApplicationTagsItem applicationTag) {
private ApplicationTagResponse buildApplicationTagResponse(
org.wso2.carbon.identity.application.common.model.ApplicationTagsListItem applicationTag) {

return new ApplicationTagResponse()
.id(applicationTag.getId())
.name(applicationTag.getName())
.colour(applicationTag.getColour())
.associatedAppsCount(0);
.associatedAppsCount(applicationTag.getAssociatedAppsCount());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Response deleteApplicationTag(String tagId) {
}

@Override
public Response getAllApplicationTags(Integer offset, Integer limit, String filter) {
public Response getAllApplicationTags(Integer limit, Integer offset, String filter) {

return Response.ok().entity(serverApplicationTagManagementService.getAllApplicationTags(offset, limit,
filter)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<parent>
<artifactId>identity-api-server</artifactId>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<version>1.2.81-SNAPSHOT</version>
<version>1.2.85-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.identity.api.server.application.tag.management</artifactId>
<version>1.2.81-SNAPSHOT</version>
<version>1.2.85-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,13 @@
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.server.api</groupId>

<artifactId>org.wso2.carbon.identity.api.server.api.resource.common</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.server.application.tag.management.common</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
Expand Down

0 comments on commit 48ba85e

Please sign in to comment.