Search in sources :

Example 1 with Authenticated

use of com.haulmont.cuba.security.app.Authenticated in project cuba by cuba-platform.

the class PersistenceManager method jpqlLoadList.

@Authenticated
@Override
public String jpqlLoadList(String queryString) {
    try {
        Transaction tx = persistence.createTransaction();
        try {
            EntityManager em = persistence.getEntityManager();
            Query query = em.createQuery(queryString);
            QueryParser parser = QueryTransformerFactory.createParser(queryString);
            Set<String> paramNames = parser.getParamNames();
            for (String paramName : paramNames) {
                security.setQueryParam(query, paramName);
            }
            List resultList = query.getResultList();
            tx.commit();
            StrBuilder sb = new StrBuilder();
            for (Object element : resultList) {
                if (element instanceof Object[]) {
                    sb.appendWithSeparators((Object[]) element, " | ");
                } else {
                    sb.append(element);
                }
                sb.append("\n");
            }
            return sb.toString();
        } finally {
            tx.end();
        }
    } catch (Throwable e) {
        log.error("jpqlLoadList error", e);
        return ExceptionUtils.getStackTrace(e);
    }
}
Also used : QueryParser(com.haulmont.cuba.core.global.QueryParser) List(java.util.List) StrBuilder(org.apache.commons.lang.text.StrBuilder) Authenticated(com.haulmont.cuba.security.app.Authenticated)

Example 2 with Authenticated

use of com.haulmont.cuba.security.app.Authenticated in project cuba by cuba-platform.

the class Emailer method sendTestEmail.

@Authenticated
@Override
public String sendTestEmail(String addresses) {
    try {
        String att = "<html><body><h1>Test attachment</h1></body></html>";
        EmailAttachment emailAtt = EmailAttachment.createTextAttachment(att, StandardCharsets.UTF_8.name(), "test attachment.html");
        emailer.sendEmail(addresses, "Test email", "<html><body><h1>Test email</h1></body></html>", emailAtt);
        return "Email to '" + addresses + "' sent successfully";
    } catch (Exception e) {
        return ExceptionUtils.getStackTrace(e);
    }
}
Also used : EmailAttachment(com.haulmont.cuba.core.global.EmailAttachment) Authenticated(com.haulmont.cuba.security.app.Authenticated)

Example 3 with Authenticated

use of com.haulmont.cuba.security.app.Authenticated in project cuba by cuba-platform.

the class PersistenceManager method refreshStatistics.

@Authenticated
@Override
public String refreshStatistics(String entityName) {
    if (StringUtils.isBlank(entityName))
        return "Pass an entity name (MetaClass name, e.g. sec$User) or 'all' to refresh statistics for all entities.\n" + "Be careful, it can take very long time.";
    try {
        log.info("Refreshing statistics for " + entityName);
        Consumer<MetaClass> refreshStatisticsForEntity = mc -> {
            MetaClass originalMetaClass = metadata.getExtendedEntities().getOriginalOrThisMetaClass(mc);
            Class javaClass = originalMetaClass.getJavaClass();
            Table annotation = (Table) javaClass.getAnnotation(Table.class);
            if (annotation != null) {
                persistenceManager.refreshStatisticsForEntity(originalMetaClass.getName());
            }
        };
        if ("all".equals(entityName)) {
            for (MetaClass metaClass : metadata.getSession().getClasses()) {
                refreshStatisticsForEntity.accept(metaClass);
            }
        } else {
            MetaClass metaClass = metadata.getSession().getClass(entityName);
            if (metaClass == null)
                return "MetaClass not found: " + entityName;
            refreshStatisticsForEntity.accept(metaClass);
        }
        return "Done";
    } catch (Exception e) {
        log.error("refreshStatistics error", e);
        return ExceptionUtils.getStackTrace(e);
    }
}
Also used : QueryTransformerFactory(com.haulmont.cuba.core.global.QueryTransformerFactory) StrBuilder(org.apache.commons.lang.text.StrBuilder) com.haulmont.cuba.core(com.haulmont.cuba.core) StringUtils(org.apache.commons.lang.StringUtils) LoggerFactory(org.slf4j.LoggerFactory) ServerConfig(com.haulmont.cuba.core.app.ServerConfig) MetaClass(com.haulmont.chile.core.model.MetaClass) Metadata(com.haulmont.cuba.core.global.Metadata) Inject(javax.inject.Inject) Table(javax.persistence.Table) EntityStatistics(com.haulmont.cuba.core.entity.EntityStatistics) Configuration(com.haulmont.cuba.core.global.Configuration) DbInitializationException(com.haulmont.cuba.core.sys.DbInitializationException) Map(java.util.Map) PersistenceConfig(com.haulmont.cuba.core.app.PersistenceConfig) Authenticated(com.haulmont.cuba.security.app.Authenticated) Logger(org.slf4j.Logger) ExceptionUtils(org.apache.commons.lang.exception.ExceptionUtils) QueryParser(com.haulmont.cuba.core.global.QueryParser) Set(java.util.Set) File(java.io.File) DbmsType(com.haulmont.cuba.core.sys.persistence.DbmsType) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) List(java.util.List) PersistenceManagerAPI(com.haulmont.cuba.core.app.PersistenceManagerAPI) DbUpdater(com.haulmont.cuba.core.sys.DbUpdater) Table(javax.persistence.Table) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) DbInitializationException(com.haulmont.cuba.core.sys.DbInitializationException) Authenticated(com.haulmont.cuba.security.app.Authenticated)

Example 4 with Authenticated

use of com.haulmont.cuba.security.app.Authenticated in project cuba by cuba-platform.

the class PersistenceManager method enterStatistics.

@Authenticated
@Override
public synchronized String enterStatistics(String name, Long instanceCount, Integer fetchUI, Integer maxFetchUI, Integer lazyCollectionThreshold, Integer lookupScreenThreshold) {
    if (StringUtils.isBlank(name))
        return "Entity name is required";
    try {
        EntityStatistics es = persistenceManager.enterStatistics(name, instanceCount, fetchUI, maxFetchUI, lazyCollectionThreshold, lookupScreenThreshold);
        StringBuilder sb = new StringBuilder("Statistics for ").append(name).append(" changed:\n");
        sb.append("instanceCount=").append(es.getInstanceCount()).append("\n");
        sb.append("fetchUI=").append(es.getFetchUI()).append("\n");
        sb.append("maxFetchUI=").append(es.getMaxFetchUI()).append("\n");
        sb.append("lazyCollectionThreshold=").append(es.getLazyCollectionThreshold()).append("\n");
        sb.append("lookupScreenThreshold=").append(es.getLookupScreenThreshold()).append("\n");
        return sb.toString();
    } catch (Exception e) {
        log.error("enterStatistics error", e);
        return ExceptionUtils.getStackTrace(e);
    }
}
Also used : EntityStatistics(com.haulmont.cuba.core.entity.EntityStatistics) DbInitializationException(com.haulmont.cuba.core.sys.DbInitializationException) Authenticated(com.haulmont.cuba.security.app.Authenticated)

Example 5 with Authenticated

use of com.haulmont.cuba.security.app.Authenticated in project cuba by cuba-platform.

the class Scheduling method removeExecutionHistory.

@Authenticated
@Override
public String removeExecutionHistory(String age, String maxPeriod) {
    List<UUID> list;
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String jpql = "select e.id from sys$ScheduledExecution e where e.startTime < ?1";
        if (maxPeriod != null) {
            jpql += " and e.task.period <= ?2";
        }
        jpql += " order by e.startTime";
        Query query = em.createQuery(jpql);
        Date startDate = DateUtils.addHours(timeSource.currentTimestamp(), -Integer.parseInt(age));
        query.setParameter(1, startDate);
        if (maxPeriod != null) {
            query.setParameter(2, Integer.parseInt(maxPeriod) * 3600);
        }
        list = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    for (int i = 0; i < list.size(); i += 100) {
        final List<UUID> subList = list.subList(i, Math.min(i + 100, list.size()));
        persistence.createTransaction().execute(new Transaction.Runnable() {

            @Override
            public void run(EntityManager em) {
                Query query = em.createQuery("delete from sys$ScheduledExecution e where e.id in ?1");
                query.setParameter(1, subList);
                query.executeUpdate();
            }
        });
    }
    return "Deleted " + list.size();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) UUID(java.util.UUID) Date(java.util.Date) Authenticated(com.haulmont.cuba.security.app.Authenticated)

Aggregations

Authenticated (com.haulmont.cuba.security.app.Authenticated)6 EntityStatistics (com.haulmont.cuba.core.entity.EntityStatistics)2 QueryParser (com.haulmont.cuba.core.global.QueryParser)2 DbInitializationException (com.haulmont.cuba.core.sys.DbInitializationException)2 List (java.util.List)2 StrBuilder (org.apache.commons.lang.text.StrBuilder)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 com.haulmont.cuba.core (com.haulmont.cuba.core)1 EntityManager (com.haulmont.cuba.core.EntityManager)1 Query (com.haulmont.cuba.core.Query)1 Transaction (com.haulmont.cuba.core.Transaction)1 PersistenceConfig (com.haulmont.cuba.core.app.PersistenceConfig)1 PersistenceManagerAPI (com.haulmont.cuba.core.app.PersistenceManagerAPI)1 ServerConfig (com.haulmont.cuba.core.app.ServerConfig)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 EmailAttachment (com.haulmont.cuba.core.global.EmailAttachment)1 Metadata (com.haulmont.cuba.core.global.Metadata)1 QueryTransformerFactory (com.haulmont.cuba.core.global.QueryTransformerFactory)1 DbUpdater (com.haulmont.cuba.core.sys.DbUpdater)1 DbmsType (com.haulmont.cuba.core.sys.persistence.DbmsType)1