From cdf5d26f1efd43cdbb674c09aa462bcafad37c33 Mon Sep 17 00:00:00 2001 From: Dustin Colvin Date: Thu, 3 Oct 2024 17:03:13 -0500 Subject: [PATCH] Fixing syncing issue with multiple files --- current/core/pom.xml | 2 +- .../BrightcoveDeleteAssetWorkflowStep.java | 2 +- .../BrightcoveSyncAssetWorkflowStep.java | 110 +++++++++++------- current/pom.xml | 2 +- current/ui.apps/pom.xml | 4 +- 5 files changed, 76 insertions(+), 44 deletions(-) diff --git a/current/core/pom.xml b/current/core/pom.xml index 215284f..dfcca2b 100644 --- a/current/core/pom.xml +++ b/current/core/pom.xml @@ -46,7 +46,7 @@ com.coresecure.brightcove.cq5 brightcove_connector - 6.0.6 + 6.0.7 ../pom.xml brightcove-services diff --git a/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveDeleteAssetWorkflowStep.java b/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveDeleteAssetWorkflowStep.java index 7848ab9..cb9df64 100644 --- a/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveDeleteAssetWorkflowStep.java +++ b/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveDeleteAssetWorkflowStep.java @@ -55,7 +55,7 @@ public class BrightcoveDeleteAssetWorkflowStep implements WorkflowProcess{ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap processArguments) throws WorkflowException { // TODO Auto-generated method stub String payloadPath = workItem.getWorkflowData().getPayload().toString(); - LOG.error("********************* payloadpath:" + payloadPath); + LOG.info("********************* payloadpath:" + payloadPath); ResourceResolver rr = null; try { diff --git a/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveSyncAssetWorkflowStep.java b/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveSyncAssetWorkflowStep.java index 1e0f408..23fb498 100644 --- a/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveSyncAssetWorkflowStep.java +++ b/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveSyncAssetWorkflowStep.java @@ -46,26 +46,26 @@ @Service @Component(immediate = true, metatype = true) @Properties({ - @Property(name = org.osgi.framework.Constants.SERVICE_DESCRIPTION, value = "Brightcove: Sync Asset to Brightcove"), - @Property(name = org.osgi.framework.Constants.SERVICE_VENDOR, value = "Brightcove"), - @Property(name = "process.label", value = "Brightcove: Sync Asset to Brightcove") }) -public class BrightcoveSyncAssetWorkflowStep implements WorkflowProcess{ - - @Reference - private ResourceResolverFactory resourceResolverFactory; + @Property(name = org.osgi.framework.Constants.SERVICE_DESCRIPTION, value = "Brightcove: Sync Asset to Brightcove"), + @Property(name = org.osgi.framework.Constants.SERVICE_VENDOR, value = "Brightcove"), + @Property(name = "process.label", value = "Brightcove: Sync Asset to Brightcove")}) +public class BrightcoveSyncAssetWorkflowStep implements WorkflowProcess { private static final Logger LOG = LoggerFactory.getLogger(BrightcoveSyncAssetWorkflowStep.class); private static final String SERVICE_ACCOUNT_IDENTIFIER = "brightcoveWrite"; - private String brightcoveAssetId; + @Reference + private ResourceResolverFactory resourceResolverFactory; +// private String brightcoveAssetId; private Map paths = null; @Override public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap processArguments) throws WorkflowException { String payloadPath = workItem.getWorkflowData().getPayload().toString(); - LOG.error("********************* payloadpath:" + payloadPath); + LOG.info("********************* payloadpath:" + payloadPath); ResourceResolver rr = null; + String brightcoveAssetId = null; try { @@ -115,13 +115,13 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData Asset _asset = assetResource.adaptTo(Asset.class); ServiceUtil serviceUtil = new ServiceUtil(brightcoveAccountId); // upload or modify the asset - activateAsset(rr, _asset, serviceUtil); + brightcoveAssetId = activateAsset(rr, _asset, serviceUtil); TimeUnit.SECONDS.sleep(15); if (brightcoveAssetId != null && !brightcoveAssetId.isEmpty()) { - syncBrightcoveData(serviceUtil, _asset, rr, brightcoveAccountId); + syncBrightcoveData(brightcoveAssetId, serviceUtil, _asset, rr, brightcoveAccountId); } rr.commit(); - + } @@ -144,23 +144,25 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData } } } - - private void syncBrightcoveData(ServiceUtil serviceUtil, Asset _asset, ResourceResolver rr, String brightcoveAccountId) { - + + private void syncBrightcoveData(String brightcoveAssetId, ServiceUtil serviceUtil, Asset _asset, ResourceResolver rr, String brightcoveAccountId) { + try { JSONObject result = serviceUtil.getSelectedVideo(brightcoveAssetId); - - + + serviceUtil.updateAsset(_asset, result, rr, brightcoveAccountId); } catch (PersistenceException | JSONException | RepositoryException e) { LOG.error("Error when updating Brightcove metadata and renditions: {}", e.getMessage()); } } - private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) { + private String activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) { LOG.trace("brc_lastsync was null or zero : asset should be initialized"); LOG.info("activate new asset"); + String brightcoveAssetId = null; + try { // get the binary @@ -199,13 +201,16 @@ private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, Mod LOG.error("Error: {}", e.getMessage()); } + + return brightcoveAssetId; } - private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video, - ModifiableValueMap brc_lastsync_map) { + private String activateModified(Asset _asset, ServiceUtil serviceUtil, Video video, + ModifiableValueMap brc_lastsync_map) { LOG.info("Entering activateModified()"); + String brightcoveAssetId = null; try { @@ -216,7 +221,7 @@ private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video boolean sent = api_resp.getBoolean(Constants.SENT); if (sent) { - + brightcoveAssetId = api_resp.getString(Constants.VIDEOID); LOG.info("Brightcove video updated successfully: {}", _asset.getPath()); serviceUtil.updateRenditions(_asset, video); LOG.info("Updated renditions for Brightcove video: {}", _asset.getPath()); @@ -240,28 +245,44 @@ private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video LOG.error("General Error: {}", _asset.getName()); } + + return brightcoveAssetId; } - private void syncFolder(ServiceUtil serviceUtil, JSONObject api_resp, Node assetNode) { + private void syncFolder(ServiceUtil serviceUtil, JSONObject api_resp, Node assetNode) { try { LOG.trace("CHECKING PARENT FOR BRC_FOLDER_ID: " + assetNode.getParent().getPath()); Node parentNode = assetNode.getParent(); + String videoId = api_resp.getString(Constants.VIDEOID); + if (!parentNode.hasProperty("brc_folder_id")) { String folderId = serviceUtil.createFolder(assetNode.getParent().getName()); if (folderId != null && !folderId.isEmpty()) { - parentNode.getSession().move(parentNode.getPath(), parentNode.getParent().getPath() + "/" + folderId); - parentNode.getSession().save(); - parentNode.setProperty("brc_folder_id", folderId); - - LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + folderId + "'"); - serviceUtil.moveVideoToFolder(folderId, api_resp.getString(Constants.VIDEOID)); + setFolderIdMoveAssetInBC(serviceUtil, parentNode, videoId, folderId); + } else { + LOG.error("*************************** No folder created ***************************"); + TimeUnit.SECONDS.sleep(15); + parentNode.refresh(false); + if (!parentNode.hasProperty("brc_folder_id")) { + folderId = serviceUtil.createFolder(assetNode.getParent().getName()); + if (folderId != null && !folderId.isEmpty()) { + setFolderIdMoveAssetInBC(serviceUtil, parentNode, videoId, folderId); + } else { + LOG.error("*************************** No folder created attempt 2 ***************************"); + } + } else { + // this is in a subfolder so we need to formally move the asset to this folder + String brc_folder_id = parentNode.getProperty("brc_folder_id").getString(); + LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + brc_folder_id + "'"); + serviceUtil.moveVideoToFolder(brc_folder_id, videoId); + } } } else { // this is in a subfolder so we need to formally move the asset to this folder - String brc_folder_id = assetNode.getParent().getProperty("brc_folder_id").getString(); + String brc_folder_id = parentNode.getProperty("brc_folder_id").getString(); LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + brc_folder_id + "'"); - serviceUtil.moveVideoToFolder(brc_folder_id, api_resp.getString(Constants.VIDEOID)); + serviceUtil.moveVideoToFolder(brc_folder_id, videoId); } @@ -273,27 +294,36 @@ private void syncFolder(ServiceUtil serviceUtil, JSONObject api_resp, Node asset } } - private void activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil serviceUtil) { + private void setFolderIdMoveAssetInBC(ServiceUtil serviceUtil, Node parentNode, String videoId, String folderId) throws Exception { + parentNode.setProperty("brc_folder_id", folderId); + parentNode.getSession().save(); + + LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + folderId + "'"); + serviceUtil.moveVideoToFolder(folderId, videoId); + } + + private String activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil serviceUtil) { // need to either activate a new asset or an updated existing // ServiceUtil serviceUtil = new ServiceUtil(accountId); String path = _asset.getPath(); - + String brightcoveAssetId = null; + Video video = serviceUtil.createVideo(path, _asset, "ACTIVE"); Resource assetRes = _asset.adaptTo(Resource.class); if (assetRes == null) { - return; + return ""; } Resource metadataRes = assetRes.getChild(Constants.ASSET_METADATA_PATH); if (metadataRes == null) { - return; + return ""; } ModifiableValueMap brc_lastsync_map = metadataRes.adaptTo(ModifiableValueMap.class); if (brc_lastsync_map == null) { - return; + return ""; } Long jcr_lastmod = _asset.getLastModified(); @@ -305,13 +335,15 @@ private void activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil servic // we need to activate a new asset here LOG.info("Activating New Brightcove Asset: {}", _asset.getPath()); - activateNew(_asset, serviceUtil, video, brc_lastsync_map); + brightcoveAssetId = activateNew(_asset, serviceUtil, video, brc_lastsync_map); } else { - // we need to modify an existing asset here + // we need to modify an existing asset here LOG.info("Activating Modified Brightcove Asset: {}", _asset.getPath()); - activateModified(_asset, serviceUtil, video, brc_lastsync_map); + brightcoveAssetId = activateModified(_asset, serviceUtil, video, brc_lastsync_map); } + + return brightcoveAssetId; - } + } } \ No newline at end of file diff --git a/current/pom.xml b/current/pom.xml index 036a403..266ea56 100644 --- a/current/pom.xml +++ b/current/pom.xml @@ -45,7 +45,7 @@ Copyright (C) 2019 3|SHARE Inc. com.coresecure.brightcove.cq5 brightcove_connector pom - 6.0.6 + 6.0.7 Parent Maven POM for the 'Adobe CQ5 Brightcove Connector API' project. diff --git a/current/ui.apps/pom.xml b/current/ui.apps/pom.xml index 46218e1..eca260e 100644 --- a/current/ui.apps/pom.xml +++ b/current/ui.apps/pom.xml @@ -49,7 +49,7 @@ com.coresecure.brightcove.cq5 brightcove_connector - 6.0.6 + 6.0.7 ../pom.xml @@ -227,7 +227,7 @@ com.coresecure.brightcove.cq5 brightcove-services - 6.0.6 + 6.0.7