DPSEntityImporterImpl.java 25.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.dps.client.producer.EntityListOptions
 *  com.adobe.dps.client.producer.EntityProducerDAO
 *  com.adobe.dps.client.producer.EntityType
 *  com.adobe.dps.client.producer.api.ProducerApi
 *  com.adobe.dps.client.producer.utils.AccessToken
 *  com.adobe.dps.client.producer.utils.Session
 *  com.adobe.dps.producer.entity.dto.EntityLink
 *  com.adobe.dps.producer.entity.dto.EntityList
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.sling.api.adapter.AdapterManager
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.commons.threads.ThreadPool
 *  org.osgi.service.event.EventAdmin
 *  org.slf4j.Logger
 */
package com.adobe.cq.mobile.dps.impl.service;

import com.adobe.cq.mobile.dps.DPSArticle;
import com.adobe.cq.mobile.dps.DPSBanner;
import com.adobe.cq.mobile.dps.DPSCollection;
import com.adobe.cq.mobile.dps.DPSEntity;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.DPSProject;
import com.adobe.cq.mobile.dps.impl.DPSConnection;
import com.adobe.cq.mobile.dps.impl.DPSEntityImporter;
import com.adobe.cq.mobile.dps.impl.ImportStatus;
import com.adobe.cq.mobile.dps.impl.service.AbstractDPSClient;
import com.adobe.cq.mobile.dps.impl.service.DPSConnectionAdapterFactory;
import com.adobe.cq.mobile.dps.impl.service.actions.GetEntity;
import com.adobe.cq.mobile.dps.impl.ui.PerfTimer;
import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
import com.adobe.cq.mobile.dps.impl.utils.MetadataJSONUtil;
import com.adobe.dps.client.producer.EntityListOptions;
import com.adobe.dps.client.producer.EntityProducerDAO;
import com.adobe.dps.client.producer.EntityType;
import com.adobe.dps.client.producer.api.ProducerApi;
import com.adobe.dps.client.producer.utils.AccessToken;
import com.adobe.dps.producer.entity.dto.EntityLink;
import com.adobe.dps.producer.entity.dto.EntityList;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.net.URI;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.threads.ThreadPool;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;

public class DPSEntityImporterImpl
extends AbstractDPSClient
implements DPSEntityImporter {
    private static final String IMPORT_TEMPLATE_ARTICLE = "/libs/mobileapps/dps/templates/article/imported";
    private static final String IMPORT_TEMPLATE_BANNER = "/libs/mobileapps/dps/templates/banner/imported";
    private static final String IMPORT_TEMPLATE_COLLECTION = "/libs/mobileapps/dps/templates/collection/imported";
    private static final String INVALID_DOT = ".";
    private static final String INVALID_DOT_REPLACEMENT = "__d0t__";
    private PageManager pageManager = null;

    public DPSEntityImporterImpl(Session userSession, DPSConnectionAdapterFactory dpsConnectionAdapterFactory, PageManager pageManager, ConfigurationManager configurationManagerService, EventAdmin eventAdminService, AdapterManager adapterManager, ThreadPool threadPool) {
        super(userSession, dpsConnectionAdapterFactory, configurationManagerService, eventAdminService, adapterManager, threadPool);
        this.pageManager = pageManager;
    }

    @Override
    public ImportStatus importDPSArticles(DPSProject dpsProject) throws DPSException {
        PerfTimer perfTimer = PerfTimer.startTimer(this, "importDPSArticles");
        this.LOGGER.info("Import Articles for project {}", (Object)dpsProject.getPath());
        try {
            ImportStatus importStatus = new ImportStatus();
            String publicationId = DPSUtil.getAndAssertProjectId(dpsProject);
            DPSConnection dpsConnection = this.dpsConnectionAdapterFactory.getAdapter(dpsProject, DPSConnection.class);
            EntityProducerDAO entityAccess = dpsConnection.getEntityAccess();
            ProducerApi producerAPI = dpsConnection.getProducerAPI(publicationId);
            AccessToken accessToken = dpsConnection.getAccessToken();
            com.adobe.dps.client.producer.utils.Session session = dpsConnection.getSession();
            EntityListOptions filter = new EntityListOptions(EntityType.ARTICLE);
            filter.setPageSize(100);
            EntityList entityList = producerAPI.listEntities(filter, session);
            int pagecount = 0;
            while (entityList != null) {
                JSONArray articles = this.getDPSArticles(dpsProject, entityList, entityAccess, accessToken, session, false, false);
                this.importArticlesImpl(dpsProject, importStatus, articles);
                this.userSession.save();
                ++pagecount;
                this.LOGGER.debug("############################ ");
                entityList = producerAPI.listNextEntites(entityList, session);
            }
            this.LOGGER.debug("Loaded #pages: " + pagecount);
            perfTimer.end();
            this.LOGGER.debug("Import Articles Status " + importStatus);
            return importStatus;
        }
        catch (Exception ex) {
            throw new DPSException("Failed to import DPS articles for project: " + dpsProject.getName(), ex);
        }
    }

    @Override
    public ImportStatus importDPSEntity(DPSEntity dpsEntity) throws DPSException {
        try {
            PerfTimer perfTimer = PerfTimer.startTimer(this, "importDPSEntity-" + dpsEntity.getClass().getName());
            this.LOGGER.info("Sync {}", (Object)dpsEntity.getPath());
            ImportStatus importStatus = new ImportStatus();
            if (dpsEntity.getId() != null) {
                DPSConnection dpsConnection = this.dpsConnectionAdapterFactory.getAdapter(dpsEntity, DPSConnection.class);
                EntityProducerDAO entityAccess = dpsConnection.getEntityAccess();
                AccessToken accessToken = dpsConnection.getAccessToken();
                com.adobe.dps.client.producer.utils.Session session = dpsConnection.getSession();
                EntityType entityType = DPSUtil.getEntityType(dpsEntity);
                String versionLessId = MetadataJSONUtil.stripVersionFromURI(dpsEntity.getId());
                GetEntity getEntity = new GetEntity(entityAccess, accessToken, session, versionLessId, false, false, true, entityType);
                JSONObject entityJSONObject = getEntity.call();
                String uri = entityJSONObject.getString("entityURL");
                if (this.needsImport(dpsEntity, uri)) {
                    JSONArray entitisJSONArray = new JSONArray();
                    entitisJSONArray.put((Object)entityJSONObject);
                    if (dpsEntity instanceof DPSArticle) {
                        this.importArticlesImpl(dpsEntity.getProject(), importStatus, entitisJSONArray);
                    } else if (dpsEntity instanceof DPSBanner) {
                        this.importBannersImpl(dpsEntity.getProject(), importStatus, entitisJSONArray);
                    } else if (dpsEntity instanceof DPSCollection) {
                        this.importCollectionsImpl(dpsEntity.getProject(), importStatus, entitisJSONArray);
                    } else {
                        throw new DPSException("Invalid type for import " + dpsEntity.getClass().getName());
                    }
                    this.userSession.save();
                }
                perfTimer.end();
                return importStatus;
            }
            throw new DPSException("Entity cannot be imported. It has not been uploaded or imported.");
        }
        catch (Exception ex) {
            throw new DPSException("Failed to import Experience Manager Mobile entity " + dpsEntity.getClass().getName() + ": " + dpsEntity.getName(), ex);
        }
    }

    @Override
    public ImportStatus importDPSBanners(DPSProject dpsProject) throws DPSException {
        PerfTimer perfTimer = PerfTimer.startTimer(this, "importDPSBanners");
        this.LOGGER.info("Import Banners for project {}", (Object)dpsProject.getPath());
        ImportStatus importStatus = new ImportStatus();
        try {
            String publicationId = DPSUtil.getAndAssertProjectId(dpsProject);
            DPSConnection dpsConnection = this.dpsConnectionAdapterFactory.getAdapter(dpsProject, DPSConnection.class);
            EntityProducerDAO entityAccess = dpsConnection.getEntityAccess();
            ProducerApi producerAPI = dpsConnection.getProducerAPI(publicationId);
            AccessToken accessToken = dpsConnection.getAccessToken();
            com.adobe.dps.client.producer.utils.Session session = dpsConnection.getSession();
            EntityListOptions filter = new EntityListOptions(EntityType.BANNER);
            filter.setPageSize(100);
            EntityList entityList = producerAPI.listEntities(filter, session);
            int pagecount = 0;
            while (entityList != null) {
                JSONArray entitiesJSONArray = this.getDPSBanners(dpsProject, entityList, entityAccess, accessToken, session, false, false);
                this.importBannersImpl(dpsProject, importStatus, entitiesJSONArray);
                this.userSession.save();
                ++pagecount;
                this.LOGGER.debug("############################ ");
                entityList = producerAPI.listNextEntites(entityList, session);
            }
            this.LOGGER.debug("Loaded #pages: " + pagecount);
            perfTimer.end();
            this.LOGGER.debug("Import Banners Status " + importStatus);
            return importStatus;
        }
        catch (Exception ex) {
            throw new DPSException("Failed to import Experience Manager Mobile banners for project: " + dpsProject.getName(), ex);
        }
    }

    @Override
    public ImportStatus importDPSCollections(DPSProject dpsProject) throws DPSException {
        try {
            PerfTimer perfTimer = PerfTimer.startTimer(this, "importDPSCollections");
            this.LOGGER.info("Import Collections for project {}", (Object)dpsProject.getPath());
            ImportStatus importStatus = new ImportStatus();
            String publicationId = DPSUtil.getAndAssertProjectId(dpsProject);
            DPSConnection dpsConnection = this.dpsConnectionAdapterFactory.getAdapter(dpsProject, DPSConnection.class);
            EntityProducerDAO entityAccess = dpsConnection.getEntityAccess();
            ProducerApi producerAPI = dpsConnection.getProducerAPI(publicationId);
            AccessToken accessToken = dpsConnection.getAccessToken();
            com.adobe.dps.client.producer.utils.Session session = dpsConnection.getSession();
            EntityListOptions filter = new EntityListOptions(EntityType.COLLECTION);
            filter.setPageSize(100);
            EntityList entityList = producerAPI.listEntities(filter, session);
            int pagecount = 0;
            while (entityList != null) {
                JSONArray collections = this.getDPSCollections(dpsProject, entityList, entityAccess, accessToken, session, false, false, true);
                this.importCollectionsImpl(dpsProject, importStatus, collections);
                this.userSession.save();
                ++pagecount;
                this.LOGGER.debug("############################ ");
                entityList = producerAPI.listNextEntites(entityList, session);
            }
            this.LOGGER.debug("Loaded #pages: " + pagecount);
            perfTimer.end();
            this.LOGGER.debug("Import Collections Status " + importStatus);
            return importStatus;
        }
        catch (Exception ex) {
            throw new DPSException("Failed to import Experience Manager Mobile collections for project: " + dpsProject.getName(), ex);
        }
    }

    private boolean assertImportedName(String name) {
        return JcrUtil.isValidName((String)name);
    }

    private void importArticlesImpl(DPSProject dpsProject, ImportStatus importStatus, JSONArray result) throws JSONException, RepositoryException, DPSException {
        for (int i = 0; i < result.length(); ++i) {
            JSONObject articleJSONObject = result.getJSONObject(i);
            String name = articleJSONObject.getString("name");
            String uri = articleJSONObject.getString("entityURL");
            DPSArticle inAEMDPSArticle = dpsProject.getArticle(this.getSafeName(name));
            if (inAEMDPSArticle != null) {
                this.LOGGER.debug("UPDATE " + articleJSONObject.toString(5));
                importStatus.addUpdated(uri);
                this.updateAEMEntityUsingDPSMetadata(inAEMDPSArticle, articleJSONObject);
                continue;
            }
            this.LOGGER.debug("IMPORT " + articleJSONObject.toString(5));
            importStatus.addImported(uri);
            String title = articleJSONObject.has("title") ? articleJSONObject.getString("title") : null;
            DPSArticle importedArticle = this.importArticleImpl(dpsProject, name, title, "/libs/mobileapps/dps/templates/article/imported");
            this.updateAEMEntityUsingDPSMetadata(importedArticle, articleJSONObject);
        }
    }

    private void importBannersImpl(DPSProject dpsProject, ImportStatus importStatus, JSONArray result) throws JSONException, RepositoryException, DPSException {
        for (int i = 0; i < result.length(); ++i) {
            JSONObject bannerJSONObject = result.getJSONObject(i);
            String name = bannerJSONObject.getString("name");
            String uri = bannerJSONObject.getString("entityURL");
            DPSBanner inAEMDPSBanner = dpsProject.getBanner(this.getSafeName(name));
            if (inAEMDPSBanner != null) {
                this.LOGGER.debug("UPDATE " + bannerJSONObject.toString(5));
                importStatus.addUpdated(uri);
                this.updateAEMEntityUsingDPSMetadata(inAEMDPSBanner, bannerJSONObject);
                continue;
            }
            this.LOGGER.debug("IMPORT " + bannerJSONObject.toString(5));
            importStatus.addImported(uri);
            String title = bannerJSONObject.has("title") ? bannerJSONObject.getString("title") : null;
            DPSBanner importedBanner = this.importBannerImpl(dpsProject, name, title, "/libs/mobileapps/dps/templates/banner/imported");
            this.updateAEMEntityUsingDPSMetadata(importedBanner, bannerJSONObject);
        }
    }

    private void importCollectionsImpl(DPSProject dpsProject, ImportStatus importStatus, JSONArray result) throws JSONException, RepositoryException, DPSException {
        for (int i = 0; i < result.length(); ++i) {
            JSONObject collectionJSONObject = result.getJSONObject(i);
            String name = collectionJSONObject.getString("name");
            String uri = collectionJSONObject.getString("entityURL");
            DPSCollection inAEMDPSCollection = dpsProject.getCollection(this.getSafeName(name));
            if (inAEMDPSCollection != null) {
                this.LOGGER.debug("UPDATE " + collectionJSONObject.toString(5));
                importStatus.addUpdated(uri);
                this.updateAEMEntityUsingDPSMetadata(inAEMDPSCollection, collectionJSONObject);
                continue;
            }
            this.LOGGER.debug("IMPORT " + collectionJSONObject.toString(5));
            importStatus.addImported(uri);
            String title = collectionJSONObject.has("title") ? collectionJSONObject.getString("title") : null;
            DPSCollection importedCollection = this.importCollectionImpl(dpsProject, name, title, "/libs/mobileapps/dps/templates/collection/imported");
            this.updateAEMEntityUsingDPSMetadata(importedCollection, collectionJSONObject);
        }
    }

    private JSONArray getDPSArticles(DPSProject dpsProject, EntityList entityList, EntityProducerDAO entityAccess, AccessToken accessToken, com.adobe.dps.client.producer.utils.Session session, boolean includeReferrers, boolean incStatus) throws Exception {
        PerfTimer perfTimer = PerfTimer.startTimer(this, "getArticles");
        JSONArray articles = new JSONArray();
        ArrayList<Future> futureList = new ArrayList<Future>();
        for (EntityLink link : entityList) {
            if (!this.needsImport(dpsProject, link)) continue;
            Future future = this.threadPool.submit((Callable)new GetEntity(entityAccess, accessToken, session, link.getHref().toString(), includeReferrers, incStatus, false, EntityType.ARTICLE));
            futureList.add(future);
        }
        for (Future future : futureList) {
            try {
                articles.put(future.get());
                continue;
            }
            catch (Exception ex) {
                throw new DPSException("Failed result inclusion.", ex);
            }
        }
        perfTimer.end();
        return articles;
    }

    private JSONArray getDPSBanners(DPSProject dpsProject, EntityList entityList, EntityProducerDAO entityAccess, AccessToken accessToken, com.adobe.dps.client.producer.utils.Session session, boolean includeReferrers, boolean incStatus) throws Exception {
        PerfTimer perfTimer = PerfTimer.startTimer(this, "getDPSBanners");
        JSONArray banners = new JSONArray();
        ArrayList<Future> futureList = new ArrayList<Future>();
        for (EntityLink link : entityList) {
            if (!this.needsImport(dpsProject, link)) continue;
            Future future = this.threadPool.submit((Callable)new GetEntity(entityAccess, accessToken, session, link.getHref().toString(), includeReferrers, incStatus, false, EntityType.BANNER));
            futureList.add(future);
        }
        for (Future future : futureList) {
            try {
                banners.put(future.get());
                continue;
            }
            catch (Exception ex) {
                throw new DPSException("Failed result inclusion.", ex);
            }
        }
        perfTimer.end();
        return banners;
    }

    private JSONArray getDPSCollections(DPSProject dpsProject, EntityList entityList, EntityProducerDAO entityAccess, AccessToken accessToken, com.adobe.dps.client.producer.utils.Session session, boolean includeReferrers, boolean incStatus, boolean incContent) throws Exception {
        PerfTimer perfTimer = PerfTimer.startTimer(this, "getDPSCollections");
        JSONArray collections = new JSONArray();
        ArrayList<Future> futureList = new ArrayList<Future>();
        for (EntityLink link : entityList) {
            if (!this.needsImport(dpsProject, link)) continue;
            Future future = this.threadPool.submit((Callable)new GetEntity(entityAccess, accessToken, session, link.getHref().toString(), includeReferrers, incStatus, incContent, EntityType.COLLECTION));
            futureList.add(future);
        }
        for (Future future : futureList) {
            try {
                collections.put(future.get());
                continue;
            }
            catch (Exception ex) {
                throw new DPSException("Failed result inclusion.", ex);
            }
        }
        perfTimer.end();
        return collections;
    }

    private boolean needsImport(DPSProject dpsProject, EntityLink entityLink) {
        String type = entityLink.getType();
        String href = entityLink.getHref().getPath();
        String name = MetadataJSONUtil.getLinkName(href);
        EntityType entityType = EntityType.fromValue((String)type);
        DPSEntity inAEMDPSEntity = null;
        if (entityType == EntityType.COLLECTION) {
            inAEMDPSEntity = dpsProject.getCollection(name);
        } else if (entityType == EntityType.ARTICLE) {
            inAEMDPSEntity = dpsProject.getArticle(name);
        } else if (entityType == EntityType.BANNER) {
            inAEMDPSEntity = dpsProject.getBanner(name);
        }
        return this.needsImport(inAEMDPSEntity, href);
    }

    private boolean needsImport(DPSEntity inAEMDPSEntity, String href) {
        boolean needsImport = false;
        if (inAEMDPSEntity == null) {
            needsImport = true;
            this.LOGGER.debug("[Import check] Need to import, not in AEM yet " + href + ":");
        } else if (inAEMDPSEntity.getId() != null) {
            if (inAEMDPSEntity.getId().equals(href)) {
                this.LOGGER.debug("[Import check] Skipping update, no changed detected " + href + ":" + inAEMDPSEntity.getPath());
            } else {
                needsImport = true;
                this.LOGGER.debug("[Import check] Updating " + href + ":" + inAEMDPSEntity.getPath());
            }
        } else {
            this.LOGGER.warn("[Import check] Skipping update, orphan state detected. Local entity not linked however a remote instance exists " + href + ":" + inAEMDPSEntity.getPath());
        }
        return needsImport;
    }

    private void updateAEMEntityUsingDPSMetadata(DPSEntity dpsEntity, JSONObject entityJSON) throws JSONException, RepositoryException {
        Page entityPage = (Page)dpsEntity.adaptTo(Page.class);
        ModifiableValueMap addPropertiesVM = (ModifiableValueMap)entityPage.getContentResource().adaptTo(ModifiableValueMap.class);
        if (dpsEntity instanceof DPSArticle) {
            MetadataJSONUtil.fillValueMapFromArticleJSON(addPropertiesVM, entityJSON);
        } else if (dpsEntity instanceof DPSBanner) {
            MetadataJSONUtil.fillValueMapFromBannerJSON(addPropertiesVM, entityJSON);
        } else if (dpsEntity instanceof DPSCollection) {
            boolean isDPSOwned = dpsEntity.isImported();
            MetadataJSONUtil.fillValueMapFromCollectionJSON(addPropertiesVM, entityJSON, !isDPSOwned);
        }
        Node updatedContentNode = ((Node)dpsEntity.adaptTo(Node.class)).getNode("jcr:content");
        updatedContentNode.setProperty("cq:lastModified", Calendar.getInstance());
        updatedContentNode.setProperty("dps-lastSynced", Calendar.getInstance());
    }

    private DPSArticle importArticleImpl(DPSProject dpsProject, String name, String title, String template) throws DPSException {
        try {
            String articleRoot = DPSUtil.getArticleImportRoot(dpsProject);
            this.ensureFolderExists(articleRoot);
            return (DPSArticle)this.createAEMEntityPage(dpsProject, articleRoot, name, title, template);
        }
        catch (Exception ex) {
            throw new DPSException("Failed to import articles for project: " + dpsProject.getName(), ex);
        }
    }

    private DPSBanner importBannerImpl(DPSProject dpsProject, String name, String title, String template) throws DPSException {
        try {
            String bannerRoot = DPSUtil.getBannerImportRoot(dpsProject);
            this.ensureFolderExists(bannerRoot);
            return (DPSBanner)this.createAEMEntityPage(dpsProject, bannerRoot, name, title, template);
        }
        catch (Exception ex) {
            throw new DPSException("Failed to import banners for project: " + dpsProject.getName(), ex);
        }
    }

    private DPSCollection importCollectionImpl(DPSProject dpsProject, String name, String title, String template) throws DPSException {
        try {
            String collectionRoot = DPSUtil.getCollectionImportRoot(dpsProject);
            this.ensureFolderExists(collectionRoot);
            return (DPSCollection)this.createAEMEntityPage(dpsProject, collectionRoot, name, title, template);
        }
        catch (Exception ex) {
            throw new DPSException("Failed to import collection for project: " + dpsProject.getName(), ex);
        }
    }

    private DPSEntity createAEMEntityPage(DPSProject dpsProject, String parentPath, String name, String title, String template) throws WCMException, DPSException, RepositoryException {
        String safeName = this.getSafeName(name);
        Page page = this.pageManager.create(parentPath, safeName, template, title);
        Node updatedContentNode = ((Node)page.adaptTo(Node.class)).getNode("jcr:content");
        updatedContentNode.setProperty("jcr:title", title);
        updatedContentNode.setProperty("dps-lastSynced", Calendar.getInstance());
        updatedContentNode.setProperty("dps-source", "DPS");
        updatedContentNode.setProperty("dps-entityName", name);
        DPSEntity createdEntity = (DPSEntity)DPSUtil.getDPSObject(page);
        if (createdEntity == null) {
            throw new DPSException("Failed to import " + name + ". Imported node is not valid " + page.getPath());
        }
        return createdEntity;
    }

    private String getSafeName(String name) {
        String safeName = name;
        if (!JcrUtil.isValidName((String)name)) {
            safeName = safeName.replace(".", "__d0t__");
        }
        return safeName;
    }

    private void ensureFolderExists(String path) throws DPSException {
        try {
            if (!this.userSession.nodeExists(path)) {
                JcrUtil.createPath((String)path, (String)"sling:Folder", (Session)this.userSession);
                this.userSession.save();
            }
        }
        catch (RepositoryException repEx) {
            throw new DPSException("Failed to create '" + path + "'. Imported failed.", (Throwable)repEx);
        }
    }
}