Skip to content

Commit

Permalink
Merge branch 'master' into contentsync
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgonzalez authored Sep 8, 2023
2 parents 137f68f + e9ad276 commit f31b3b4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
<!-- Keep this up to date! After a release, change the tag name to the latest release -->-

## Unreleased ([details][unreleased changes details])
- #3151 - New ContentSync utility

#3170 - Added a new MCP tool to bulk tag AEM content pages via an Excel file input.

- #3151 - New ContentSync utility
- #3147 - Fixed setting initial content-type when importing CFs from a spreadsheet
- #3170 - Added a new MCP tool to bulk tag AEM content pages via an Excel file input.

## Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.adobe.cq.dam.cfm.FragmentTemplate;
import com.day.cq.commons.jcr.JcrConstants;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
Expand All @@ -48,22 +47,17 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Import a series of content fragments from a spreadsheet
*/
public class ContentFragmentImport extends ProcessDefinition {

private static final Logger LOG = LoggerFactory.getLogger(ContentFragmentImport.class);

public enum ReportColumns {
ITEM, ACTION, DESCRIPTION, COUNT
}
Expand Down Expand Up @@ -313,8 +307,14 @@ private void setContentElements(ContentFragment cf, Map<String, CompositeVariant
String value = getString(row, contentElement);
String currentValue = contentElement.getContent();

String contentType;
if(StringUtils.isBlank(currentValue)) { // Workaround issue #3147
contentType = cf.getTemplate().getForElement(contentElement).getInitialContentType();
} else {
contentType = contentElement.getContentType();
}
if (!String.valueOf(value).equals(String.valueOf(currentValue))) {
contentElement.setContent(value, contentElement.getContentType());
contentElement.setContent(value, contentType);
}
}
}
Expand Down Expand Up @@ -351,14 +351,8 @@ private String getString(Map<String, CompositeVariant> row, ContentElement conte
protected ContentFragment getOrCreateFragment(Resource parent, Resource template, String name, String title) throws ContentFragmentException {
Resource fragmentResource = parent.getChild(name);
if (fragmentResource == null) {
try {
FragmentTemplate fragmentTemplate = template.adaptTo(FragmentTemplate.class);
// TODO: Replace this reflection hack with the proper method once ACS Commons doesn't support 6.2 anymore
return (ContentFragment) MethodUtils.invokeMethod(fragmentTemplate, "createFragment", parent, name, title);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
LOG.error("Unable to call createFragment method -- Is this 6.3 or newer?", ex);
return null;
}
FragmentTemplate fragmentTemplate = template.adaptTo(FragmentTemplate.class);
return fragmentTemplate.createFragment(parent, name, title);
} else {
return fragmentResource.adaptTo(ContentFragment.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

import java.util.Date;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
import org.apache.sling.api.resource.Resource;
import org.jetbrains.annotations.NotNull;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

/**
* Incomplete mock that provides just enough for basic testing
*/
Expand All @@ -47,6 +51,14 @@ public class MockContentFragment implements ContentFragment {
String path;
HashMap<String, String> elements = new HashMap<>();
HashMap<String, Object> metadata = new HashMap<>();
FragmentTemplate template;

public MockContentFragment(){
template = mock(FragmentTemplate.class);
ElementTemplate elementTemplate = mock(ElementTemplate.class);
doReturn("text/html").when(elementTemplate).getInitialContentType();
doReturn(elementTemplate).when(template).getForElement(any(ContentElement.class));
}

@Override
public Iterator<ContentElement> getElements() {
Expand Down Expand Up @@ -114,7 +126,7 @@ public Iterator<VariationDef> listAllVariations() {

@Override
public FragmentTemplate getTemplate() {
return null;
return template;
}

@Override
Expand Down

0 comments on commit f31b3b4

Please sign in to comment.