Advertisement
Guest User

Untitled

a guest
May 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. package oneoff;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.HashSet;
  8. import java.util.List;
  9. import java.util.Objects;
  10. import java.util.Optional;
  11. import java.util.Set;
  12.  
  13. import brightspot.core.vanityurlredirect.VanityUrlRedirect;
  14. import com.opencsv.CSVIterator;
  15. import com.opencsv.CSVReader;
  16. import com.psddev.cms.db.Directory;
  17. import com.psddev.cms.db.Site;
  18. import com.psddev.dari.db.Record;
  19. import com.psddev.dari.db.Recordable;
  20. import com.psddev.dari.util.ObjectUtils;
  21. import com.psddev.dari.util.StorageItem;
  22. import com.psddev.dari.util.StringUtils;
  23. import com.psddev.migration.MigrationTool;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26.  
  27. import california.times.cms.AddVanityUrlRedirectScriptSettings;
  28. import california.times.cms.RedirectScriptSettings;
  29. import catmigration.p2p.utils.P2PMigrationContext;
  30. import catmigration.p2p.utils.P2PUtils;
  31. import catmigration.p2p.utils.SiteCode;
  32.  
  33. public class FindContentWithMissingPermalinksFromFileTask {
  34.  
  35. public static final Logger LOGGER = LoggerFactory.getLogger(FindContentWithMissingPermalinksFromFileTask.class);
  36.  
  37. private static Set<String> readCsv() {
  38. final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  39.  
  40. AddVanityUrlRedirectScriptSettings settings = RedirectScriptSettings.getInstance().getAddVanityUrlRedirectScriptSettings();
  41.  
  42. StorageItem file = settings.getFile();
  43.  
  44. Set<String> recordsToFix = new HashSet<>();
  45.  
  46. if ((file == null) || !StringUtils.equalsIgnoreCase(file.getContentType(), "text/csv")) {
  47. LOGGER.info("Please upload a CSV file in Site Settings > Redirect Script Settings > Vanity Redirect Script Settings\n");
  48. return null;
  49. }
  50.  
  51. Site site;
  52. String siteUrl;
  53.  
  54. // ---CSV file upload ---
  55. if (StringUtils.equalsIgnoreCase(file.getContentType(), "text/csv")) {
  56. CSVReader reader;
  57. CSVIterator iterator;
  58. try (InputStream inputStream = file.getData()) {
  59. reader = new CSVReader(new BufferedReader(new InputStreamReader(inputStream)));
  60. iterator = new CSVIterator(reader);
  61.  
  62. if (ObjectUtils.isBlank(reader)) {
  63. LOGGER.info("File is blank! Please upload a new file");
  64. return null;
  65. }
  66.  
  67. // Get the P2PMigrationContext to interpret the site code into a Site
  68. P2PMigrationContext context = MigrationTool.getInstance().getContexts().stream().findFirst().map(P2PMigrationContext.class::cast).orElse(null);
  69.  
  70. if (context == null) {
  71. LOGGER.info("Migration context must be set in the CMS. Exiting.");
  72. return null;
  73. }
  74.  
  75. iterator.next();
  76. while (iterator.hasNext()) {
  77.  
  78. String[] csvRow = iterator.next();
  79.  
  80. String redirectPath = csvRow[2];
  81. redirectPath = StringUtils.ensureStart(redirectPath, "/");
  82.  
  83. if (StringUtils.isBlank(redirectPath)) {
  84. LOGGER.info("SKIPPING: Empty redirect entry");
  85. continue;
  86. }
  87.  
  88. String permalinkPath = csvRow[3];
  89.  
  90. String siteOwner = csvRow[11];
  91.  
  92. SiteCode siteCode = context.getSiteCodes().stream().filter(s -> !P2PUtils.isEmptyString(s.getLegacySiteCode())).filter(siteAffiliateCode -> siteAffiliateCode.getLegacySiteCode().equals(siteOwner)).findFirst().orElse(null);
  93.  
  94. if (ObjectUtils.isBlank(siteCode)) {
  95. LOGGER.info("SKIPPING " + redirectPath + ". Site code " + siteOwner + " not found");
  96. continue;
  97. }
  98.  
  99. site = Optional.ofNullable(siteCode).map(SiteCode::getSite).orElse(null);
  100.  
  101. if (!ObjectUtils.isBlank(site)) {
  102. siteUrl = site.getPrimaryUrl();
  103. } else {
  104. LOGGER.info("SKIPPING: Cannot add redirect for " + redirectPath + ". Site not found for site code " + siteOwner);
  105. continue;
  106. }
  107.  
  108. // Skip lines where the permalink path entry is the text "NULL" or empty
  109. if ("null".equalsIgnoreCase(permalinkPath) || StringUtils.isBlank(permalinkPath)) {
  110. LOGGER.info("SKIPPING: Permalink for redirect " + redirectPath + " is null");
  111. continue;
  112. }
  113.  
  114. boolean hasNoPermalink = false;
  115.  
  116. // Search for an existing record by permalink path (excluding VanityUrlsRedirects)
  117. Object object = Directory.Static.findByPath(site, permalinkPath);
  118.  
  119. if (object instanceof Recordable && !(object instanceof VanityUrlRedirect)) {
  120.  
  121. // Check if the record has a permalink
  122. Record record = (Record) object;
  123.  
  124. List<Directory.Path> paths = record.as(Directory.ObjectModification.class).getPaths();
  125.  
  126. hasNoPermalink = Optional.ofNullable(paths).map(ps -> ps.stream().filter(Objects::nonNull).noneMatch(p -> Directory.PathType.PERMALINK.equals(p.getType()))).orElse(false);
  127.  
  128. if (hasNoPermalink) {
  129. LOGGER.error("*****ALERT*****: " + site.getName() + ": Matching record found with no permalink: " + record.getLabel() + "\nValues from file: Redirect path: " + redirectPath + ", Permalink path: " + permalinkPath);
  130. recordsToFix.add(site.getName() + " - " + record.getLabel());
  131.  
  132. } else {
  133. LOGGER.info("SUCCESS: " + site.getName() + ": Matching record found with a permalink: " + record.getLabel() + "\nRecord's permalink: " + record.as(Directory.ObjectModification.class).getPermalink() + "\nValues from file: Redirect path: " + redirectPath + ", Permalink path: " + permalinkPath);
  134. }
  135. } else {
  136. // If no object is found, log that and move on
  137. LOGGER.info("SKIPPING: " + site.getName() + ": No object found for permalink path " + permalinkPath);
  138. }
  139. }
  140. } catch (IOException e) {
  141. e.printStackTrace();
  142. }
  143. LOGGER.info("\n\nFinished reading CSV file");
  144. }
  145. return recordsToFix;
  146. }
  147.  
  148. public static Object main() throws Throwable {
  149. return readCsv();
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement