Search in sources :

Example 1 with DependencyEntity

use of org.craftercms.studio.api.v1.dal.DependencyEntity in project studio by craftercms.

the class DependencyServiceImpl method createDependencyEntities.

public List<DependencyEntity> createDependencyEntities(String site, String path, Set<String> dependencyPaths, String dependencyType, Set<String> extractedPaths) {
    logger.debug("Create dependency entity TO for site: " + site + " path: " + path);
    List<DependencyEntity> dependencyEntities = new ArrayList<>();
    if (dependencyPaths != null && dependencyPaths.size() > 0) {
        for (String file : dependencyPaths) {
            DependencyEntity dependencyObj = new DependencyEntity();
            dependencyObj.setSite(site);
            dependencyObj.setSourcePath(getCleanPath(path));
            dependencyObj.setTargetPath(getCleanPath(file));
            dependencyObj.setType(dependencyType);
            dependencyEntities.add(dependencyObj);
            extractedPaths.add(file);
        }
    }
    return dependencyEntities;
}
Also used : DependencyEntity(org.craftercms.studio.api.v1.dal.DependencyEntity) ArrayList(java.util.ArrayList)

Example 2 with DependencyEntity

use of org.craftercms.studio.api.v1.dal.DependencyEntity in project studio by craftercms.

the class DependencyServiceImpl method upsertDependencies.

@Override
public Set<String> upsertDependencies(String site, List<String> paths) throws SiteNotFoundException, ContentNotFoundException, ServiceLayerException {
    Set<String> toRet = new HashSet<String>();
    List<DependencyEntity> dependencyEntities = new ArrayList<>();
    StringBuilder sbPaths = new StringBuilder();
    logger.debug("Resolving dependencies for list of paths.");
    for (String path : paths) {
        sbPaths.append("\n").append(path);
        logger.debug("Resolving dependencies for content site: " + site + " path: " + path);
        Map<String, Set<String>> dependencies = dependencyResolver.resolve(site, path);
        if (dependencies != null) {
            logger.debug("Found " + dependencies.size() + " dependencies. " + "Create entities to insert into database.");
            for (String type : dependencies.keySet()) {
                dependencyEntities.addAll(createDependencyEntities(site, path, dependencies.get(type), type, toRet));
            }
        }
    }
    logger.debug("Preparing transaction for database updates.");
    DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
    defaultTransactionDefinition.setName("upsertDependencies");
    String lock = site + ":upsertDependencies";
    generalLockService.lock(lock);
    logger.debug("Starting transaction.");
    TransactionStatus txStatus = transactionManager.getTransaction(defaultTransactionDefinition);
    try {
        logger.debug("Delete all source dependencies for list of paths site: " + site);
        for (String path : paths) {
            deleteAllSourceDependencies(site, path);
        }
        logger.debug("Insert all extracted dependencies entries lof list of paths for site: " + site);
        insertDependenciesIntoDatabase(dependencyEntities);
        logger.debug("Committing transaction.");
        transactionManager.commit(txStatus);
    } catch (Exception e) {
        logger.debug("Rolling back transaction.", e);
        transactionManager.rollback(txStatus);
        throw new ServiceLayerException("Failed to upsert dependencies for site: " + site + " paths: " + sbPaths.toString(), e);
    } finally {
        generalLockService.unlock(lock);
    }
    return toRet;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) DependencyEntity(org.craftercms.studio.api.v1.dal.DependencyEntity) HashSet(java.util.HashSet)

Example 3 with DependencyEntity

use of org.craftercms.studio.api.v1.dal.DependencyEntity in project studio by craftercms.

the class DependencyServiceImpl method upsertDependencies.

@Override
public Set<String> upsertDependencies(String site, String path) throws SiteNotFoundException, ContentNotFoundException, ServiceLayerException {
    Set<String> toRet = new HashSet<String>();
    logger.debug("Resolving dependencies for content site: " + site + " path: " + path);
    Map<String, Set<String>> dependencies = dependencyResolver.resolve(site, path);
    List<DependencyEntity> dependencyEntities = new ArrayList<>();
    if (dependencies != null) {
        logger.debug("Found " + dependencies.size() + " dependencies. Create entities to insert into database.");
        for (String type : dependencies.keySet()) {
            dependencyEntities.addAll(createDependencyEntities(site, path, dependencies.get(type), type, toRet));
        }
        logger.debug("Preparing transaction for database updates.");
        DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
        defaultTransactionDefinition.setName("upsertDependencies");
        String lock = site + ":upsertDependencies";
        generalLockService.lock(lock);
        logger.debug("Starting transaction.");
        TransactionStatus txStatus = transactionManager.getTransaction(defaultTransactionDefinition);
        try {
            logger.debug("Delete all source dependencies for site: " + site + " path: " + path);
            deleteAllSourceDependencies(site, path);
            logger.debug("Insert all extracted dependencies entries for site: " + site + " path: " + path);
            insertDependenciesIntoDatabase(dependencyEntities);
            logger.debug("Committing transaction.");
            transactionManager.commit(txStatus);
        } catch (Exception e) {
            logger.debug("Rolling back transaction.", e);
            transactionManager.rollback(txStatus);
            throw new ServiceLayerException("Failed to upsert dependencies for site: " + site + " path: " + path, e);
        } finally {
            generalLockService.unlock(lock);
        }
    }
    return toRet;
}
Also used : DependencyEntity(org.craftercms.studio.api.v1.dal.DependencyEntity) Set(java.util.Set) HashSet(java.util.HashSet) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)3 DependencyEntity (org.craftercms.studio.api.v1.dal.DependencyEntity)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)2 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)2 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)2