Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package oneoff;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Objects;
- import java.util.Optional;
- import java.util.Set;
- import brightspot.core.vanityurlredirect.VanityUrlRedirect;
- import com.opencsv.CSVIterator;
- import com.opencsv.CSVReader;
- import com.psddev.cms.db.Directory;
- import com.psddev.cms.db.Site;
- import com.psddev.dari.db.Record;
- import com.psddev.dari.db.Recordable;
- import com.psddev.dari.util.ObjectUtils;
- import com.psddev.dari.util.StorageItem;
- import com.psddev.dari.util.StringUtils;
- import com.psddev.migration.MigrationTool;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import california.times.cms.AddVanityUrlRedirectScriptSettings;
- import california.times.cms.RedirectScriptSettings;
- import catmigration.p2p.utils.P2PMigrationContext;
- import catmigration.p2p.utils.P2PUtils;
- import catmigration.p2p.utils.SiteCode;
- public class FindContentWithMissingPermalinksFromFileTask {
- public static final Logger LOGGER = LoggerFactory.getLogger(FindContentWithMissingPermalinksFromFileTask.class);
- private static Set<String> readCsv() {
- final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- AddVanityUrlRedirectScriptSettings settings = RedirectScriptSettings.getInstance().getAddVanityUrlRedirectScriptSettings();
- StorageItem file = settings.getFile();
- Set<String> recordsToFix = new HashSet<>();
- if ((file == null) || !StringUtils.equalsIgnoreCase(file.getContentType(), "text/csv")) {
- LOGGER.info("Please upload a CSV file in Site Settings > Redirect Script Settings > Vanity Redirect Script Settings\n");
- return null;
- }
- Site site;
- String siteUrl;
- // ---CSV file upload ---
- if (StringUtils.equalsIgnoreCase(file.getContentType(), "text/csv")) {
- CSVReader reader;
- CSVIterator iterator;
- try (InputStream inputStream = file.getData()) {
- reader = new CSVReader(new BufferedReader(new InputStreamReader(inputStream)));
- iterator = new CSVIterator(reader);
- if (ObjectUtils.isBlank(reader)) {
- LOGGER.info("File is blank! Please upload a new file");
- return null;
- }
- // Get the P2PMigrationContext to interpret the site code into a Site
- P2PMigrationContext context = MigrationTool.getInstance().getContexts().stream().findFirst().map(P2PMigrationContext.class::cast).orElse(null);
- if (context == null) {
- LOGGER.info("Migration context must be set in the CMS. Exiting.");
- return null;
- }
- iterator.next();
- while (iterator.hasNext()) {
- String[] csvRow = iterator.next();
- String redirectPath = csvRow[2];
- redirectPath = StringUtils.ensureStart(redirectPath, "/");
- if (StringUtils.isBlank(redirectPath)) {
- LOGGER.info("SKIPPING: Empty redirect entry");
- continue;
- }
- String permalinkPath = csvRow[3];
- String siteOwner = csvRow[11];
- SiteCode siteCode = context.getSiteCodes().stream().filter(s -> !P2PUtils.isEmptyString(s.getLegacySiteCode())).filter(siteAffiliateCode -> siteAffiliateCode.getLegacySiteCode().equals(siteOwner)).findFirst().orElse(null);
- if (ObjectUtils.isBlank(siteCode)) {
- LOGGER.info("SKIPPING " + redirectPath + ". Site code " + siteOwner + " not found");
- continue;
- }
- site = Optional.ofNullable(siteCode).map(SiteCode::getSite).orElse(null);
- if (!ObjectUtils.isBlank(site)) {
- siteUrl = site.getPrimaryUrl();
- } else {
- LOGGER.info("SKIPPING: Cannot add redirect for " + redirectPath + ". Site not found for site code " + siteOwner);
- continue;
- }
- // Skip lines where the permalink path entry is the text "NULL" or empty
- if ("null".equalsIgnoreCase(permalinkPath) || StringUtils.isBlank(permalinkPath)) {
- LOGGER.info("SKIPPING: Permalink for redirect " + redirectPath + " is null");
- continue;
- }
- boolean hasNoPermalink = false;
- // Search for an existing record by permalink path (excluding VanityUrlsRedirects)
- Object object = Directory.Static.findByPath(site, permalinkPath);
- if (object instanceof Recordable && !(object instanceof VanityUrlRedirect)) {
- // Check if the record has a permalink
- Record record = (Record) object;
- List<Directory.Path> paths = record.as(Directory.ObjectModification.class).getPaths();
- hasNoPermalink = Optional.ofNullable(paths).map(ps -> ps.stream().filter(Objects::nonNull).noneMatch(p -> Directory.PathType.PERMALINK.equals(p.getType()))).orElse(false);
- if (hasNoPermalink) {
- LOGGER.error("*****ALERT*****: " + site.getName() + ": Matching record found with no permalink: " + record.getLabel() + "\nValues from file: Redirect path: " + redirectPath + ", Permalink path: " + permalinkPath);
- recordsToFix.add(site.getName() + " - " + record.getLabel());
- } else {
- 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);
- }
- } else {
- // If no object is found, log that and move on
- LOGGER.info("SKIPPING: " + site.getName() + ": No object found for permalink path " + permalinkPath);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- LOGGER.info("\n\nFinished reading CSV file");
- }
- return recordsToFix;
- }
- public static Object main() throws Throwable {
- return readCsv();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement