Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</developers>

<properties>
<revision>1.6.1-SNAPSHOT</revision>
<revision>1.0.0-RC1</revision>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,6 @@ private void processAttachment(
List<String> noSDMRoles)
throws IOException {
String id = (String) attachment.get("ID");
String fileNameInDB;
fileNameInDB =
dbQuery.getAttachmentForID(
attachmentEntity.get(),
persistenceService,
id); // Fetching the name of the file from DB
String filenameInRequest =
(String) attachment.get("fileName"); // Fetching the name of the file from request
String objectId = (String) attachment.get("objectId");
Expand Down Expand Up @@ -238,68 +232,61 @@ private void processAttachment(
CmisDocument cmisDocument = new CmisDocument();
cmisDocument.setFileName(filenameInRequest);
cmisDocument.setObjectId(objectId);
if (fileNameInDB
== null) { // If the file name in DB is null, it means that the file is being created for
// the first time
if (filenameInRequest != null) {
updatedSecondaryProperties.put("filename", filenameInRequest);
} else {
throw new ServiceException("Filename cannot be empty");
}
} else {
if (filenameInRequest == null) {
throw new ServiceException("Filename cannot be empty");
} else if (!fileNameInDB.equals(
filenameInRequest)) { // If the file name in DB is not equal to the file name in
// request, it means that the file name has been modified
updatedSecondaryProperties.put("filename", filenameInRequest);
}

if (filenameInRequest == null) {
throw new ServiceException("Filename cannot be empty");
} else if (!filenameInRequest.equals(
fileNameInSDM)) { // If the file name in DB is not equal to the file name in
// request, it means that the file name has been modified
updatedSecondaryProperties.put("filename", filenameInRequest);
}
try {
int responseCode =
sdmService.updateAttachments(
sdmCredentials,
cmisDocument,
updatedSecondaryProperties,
secondaryPropertiesWithInvalidDefinitions,
context.getUserInfo().isSystemUser());
switch (responseCode) {
case 403:
// SDM Roles for user are missing
noSDMRoles.add(fileNameInSDM);
replacePropertiesInAttachment(
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
break;
case 409:
duplicateFileNameList.add(filenameInRequest);
if (!updatedSecondaryProperties.isEmpty()) {
try {
int responseCode =
sdmService.updateAttachments(
sdmCredentials,
cmisDocument,
updatedSecondaryProperties,
secondaryPropertiesWithInvalidDefinitions,
context.getUserInfo().isSystemUser());
switch (responseCode) {
case 403:
// SDM Roles for user are missing
noSDMRoles.add(fileNameInSDM);
replacePropertiesInAttachment(
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
break;
case 409:
duplicateFileNameList.add(filenameInRequest);
replacePropertiesInAttachment(
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
break;
case 404:
filesNotFound.add(filenameInRequest);
replacePropertiesInAttachment(
attachment, filenameInRequest, propertiesInDB, secondaryTypeProperties);
break;
case 200:
case 201:
// Success cases, do nothing
break;

default:
throw new ServiceException(SDMConstants.SDM_ROLES_ERROR_MESSAGE, null);
}
} catch (ServiceException e) {
// This exception is thrown when there are unsupported properties in the request
if (e.getMessage().startsWith(SDMConstants.UNSUPPORTED_PROPERTIES)) {
String unsupportedDetails =
e.getMessage().substring(SDMConstants.UNSUPPORTED_PROPERTIES.length()).trim();
filesWithUnsupportedProperties.add(unsupportedDetails);
replacePropertiesInAttachment(
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
break;
case 404:
filesNotFound.add(filenameInRequest);
} else {
badRequest.put(filenameInRequest, e.getMessage());
replacePropertiesInAttachment(
attachment, filenameInRequest, propertiesInDB, secondaryTypeProperties);
break;
case 200:
case 201:
// Success cases, do nothing
break;

default:
throw new ServiceException(SDMConstants.SDM_ROLES_ERROR_MESSAGE, null);
}
} catch (ServiceException e) {
// This exception is thrown when there are unsupported properties in the request
if (e.getMessage().startsWith(SDMConstants.UNSUPPORTED_PROPERTIES)) {
String unsupportedDetails =
e.getMessage().substring(SDMConstants.UNSUPPORTED_PROPERTIES.length()).trim();
filesWithUnsupportedProperties.add(unsupportedDetails);
replacePropertiesInAttachment(
attachment, fileNameInSDM, propertiesInDB, secondaryTypeProperties);
} else {
badRequest.put(filenameInRequest, e.getMessage());
replacePropertiesInAttachment(
attachment, filenameInRequest, propertiesInDB, secondaryTypeProperties);
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions sdm/src/main/java/com/sap/cds/sdm/model/AttachmentLogContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sap.cds.sdm.model;

import com.sap.cds.services.EventContext;
import com.sap.cds.services.EventName;
import org.json.JSONObject;

@EventName("changelog")
public interface AttachmentLogContext extends EventContext {

static AttachmentLogContext create() {
return (AttachmentLogContext) EventContext.create(AttachmentLogContext.class, null);
}

void setResult(JSONObject res);

JSONObject getResult();
}
3 changes: 3 additions & 0 deletions sdm/src/main/java/com/sap/cds/sdm/service/SDMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ public List<String> copyAttachment(
public JSONObject editLink(
CmisDocument cmisDocument, SDMCredentials sdmCredentials, boolean isSystemUser)
throws IOException;

public JSONObject getChangeLog(
String objectId, SDMCredentials sdmCredentials, boolean isSystemUser);
}
77 changes: 70 additions & 7 deletions sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static com.sap.cds.sdm.constants.SDMConstants.NAMED_USER_FLOW;
import static com.sap.cds.sdm.constants.SDMConstants.TECHNICAL_USER_FLOW;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sap.cds.Result;
import com.sap.cds.feature.attachments.service.model.servicehandler.AttachmentReadEventContext;
import com.sap.cds.sdm.caching.*;
Expand Down Expand Up @@ -274,17 +276,23 @@ public int updateAttachments(
// Prepare the request body parts
Map<String, String> updateRequestBody = new HashMap<>();
updateRequestBody.put("cmisaction", "update");
updateRequestBody.put(
"propertyId[0]",
"cmis:secondaryObjectTypeIds"); // Creating request body for update properties

for (int index = 0; index < secondaryTypes.size(); index++) {
boolean isFilenameUpdated = secondaryProperties.containsKey("filename");
boolean isSecondaryPropertiesUpdated = secondaryProperties.size() > (isFilenameUpdated ? 1 : 0);
if (isSecondaryPropertiesUpdated) {
updateRequestBody.put(
"propertyValue[0][" + index + "]",
secondaryTypes.get(index)); // Adding Secondary Types to the request body
"propertyId[0]",
"cmis:secondaryObjectTypeIds"); // Creating request body for update properties

for (int index = 0; index < secondaryTypes.size(); index++) {
updateRequestBody.put(
"propertyValue[0][" + index + "]",
secondaryTypes.get(index)); // Adding Secondary Types to the request body
}
}

SDMUtils.prepareSecondaryProperties(updateRequestBody, secondaryProperties, fileName);
SDMUtils.prepareSecondaryProperties(
updateRequestBody, secondaryProperties, fileName, isSecondaryPropertiesUpdated);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
SDMUtils.assembleRequestBodySecondaryTypes(
builder, updateRequestBody, objectId); // Adding Secondary Properties to the request body
Expand Down Expand Up @@ -702,4 +710,59 @@ public List<String> copyAttachment(
throw new ServiceException(SDMConstants.FAILED_TO_COPY_ATTACHMENT, e);
}
}

private String getRepositoryId(String jsonString) {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode rootNode = objectMapper.readTree(jsonString);
JsonNode repoInfos = rootNode.path("repoAndConnectionInfos");

// Iterate through the array to find the correct externalId and retrieve the id
for (JsonNode repoInfo : repoInfos) {
JsonNode repository = repoInfo.path("repository");
if (repository.path("externalId") != null
&& repository.path("externalId").asText().equals(SDMConstants.REPOSITORY_ID)) {
return repository.path("id").asText();
}
}
} catch (Exception e) {
throw new ServiceException(String.format(e.getMessage()));
}
return null;
}

@Override
public JSONObject getChangeLog(
String objectId, SDMCredentials sdmCredentials, boolean isSystemUser) {
String grantType = isSystemUser ? TECHNICAL_USER_FLOW : NAMED_USER_FLOW;
logger.info("This is a :" + grantType + " flow");
var httpClient = tokenHandler.getHttpClient(binding, connectionPool, null, grantType);
String sdmUrl = sdmCredentials.getUrl() + SDMConstants.REST_V2_REPOSITORIES + "/";
HttpGet getRepos = new HttpGet(sdmUrl);
String repoId = "";
try (var response = (CloseableHttpResponse) httpClient.execute(getRepos)) {
repoId = getRepositoryId(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
logger.error("Error in offboarding repository : " + e.getMessage());
throw new ServiceException("Error in offboarding ", e.getMessage());
}
sdmUrl =
sdmUrl
+ (repoId == null ? SDMConstants.REPOSITORY_ID : repoId)
+ "/objects/"
+ objectId
+ "/changeLogs?includeAll=true";

HttpGet getChangeLogRequest = new HttpGet(sdmUrl);
try (var response = (CloseableHttpResponse) httpClient.execute(getChangeLogRequest)) {
if (response.getStatusLine().getStatusCode() != 200) {
return null;
}
String responseString = EntityUtils.toString(response.getEntity());
return new JSONObject(responseString);

} catch (IOException e) {
throw new ServiceException(SDMConstants.ATTACHMENT_NOT_FOUND, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ public SDMServiceGenericHandler(
this.tokenHandler = tokenHandler;
}

@On(event = "changelog")
public void changelog(AttachmentLogContext context) throws IOException {
CdsModel cdsModel = context.getModel();
CqnAnalyzer cqnAnalyzer = CqnAnalyzer.create(cdsModel);
Optional<CdsEntity> attachmentDraftEntity =
cdsModel.findEntity(context.getTarget().getQualifiedName() + "_drafts");
Map<String, Object> targetKeys =
cqnAnalyzer.analyze((CqnSelect) context.get("cqn")).targetKeyValues();
// get the objectId against the Id
String ID = targetKeys.get("ID").toString();
CmisDocument cmisDocument =
dbQuery.getObjectIdForAttachmentID(attachmentDraftEntity.get(), persistenceService, ID);
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();

JSONObject jsonObject =
sdmService.getChangeLog(
cmisDocument.getObjectId(), sdmCredentials, context.getUserInfo().isSystemUser());
jsonObject.put("filename", cmisDocument.getFileName());
context.setResult(jsonObject);
}

@On(event = "copyAttachments")
public void copyAttachments(EventContext context) throws IOException {
String upID = context.get("up__ID").toString();
Expand Down
7 changes: 5 additions & 2 deletions sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ public static boolean isRestrictedCharactersInName(String cmisName) {
}

public static void prepareSecondaryProperties(
Map<String, String> requestBody, Map<String, String> secondaryProperties, String fileName) {
Map<String, String> requestBody,
Map<String, String> secondaryProperties,
String fileName,
boolean isSecondaryPropertiesUpdated) {
Iterator<Map.Entry<String, String>> iterator = secondaryProperties.entrySet().iterator();

int index = 1;
int index = isSecondaryPropertiesUpdated ? 1 : 0;
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if ("filename".equals(entry.getKey())) {
Expand Down
Loading
Loading