Search in sources :

Example 1 with ActivityPostEntity

use of org.alfresco.repo.domain.activities.ActivityPostEntity in project alfresco-repository by Alfresco.

the class PostLookup method lookupPosts.

private List<ActivityPostEntity> lookupPosts(final List<ActivityPostEntity> activityPosts) {
    for (final ActivityPostEntity activityPost : activityPosts) {
        if (logger.isDebugEnabled()) {
            logger.debug("Selected activity post: " + activityPost);
        }
        final String postUserId = activityPost.getUserId();
        try {
            // MT share
            String tenantDomain = TenantService.DEFAULT_DOMAIN;
            final JSONObject jo = new JSONObject(new JSONTokener(activityPost.getActivityData()));
            if (!jo.isNull(JSON_TENANT_DOMAIN)) {
                tenantDomain = jo.getString(JSON_TENANT_DOMAIN);
            }
            activityPost.setTenantDomain(tenantDomain);
            TenantUtil.runAsSystemTenant(new TenantUtil.TenantRunAsWork<Void>() {

                public Void doWork() throws Exception {
                    JSONObject joLookup = null;
                    if (!jo.isNull(JSON_NODEREF_LOOKUP)) {
                        String nodeRefStr = jo.getString(JSON_NODEREF_LOOKUP);
                        NodeRef nodeRef = new NodeRef(nodeRefStr);
                        // lookup additional node data
                        joLookup = lookupNode(nodeRef, postUserId, jo);
                    } else {
                        // lookup poster's firstname/lastname (if needed)
                        if ((jo.isNull(JSON_FIRSTNAME)) || (jo.isNull(JSON_LASTNAME))) {
                            Pair<String, String> firstLastName = lookupPerson(postUserId);
                            if (firstLastName != null) {
                                jo.put(JSON_FIRSTNAME, firstLastName.getFirst());
                                jo.put(JSON_LASTNAME, firstLastName.getSecond());
                                joLookup = jo;
                            }
                        }
                        // lookup parent nodeRef (if needed)
                        NodeRef parentNodeRef = activityPost.getParentNodeRef();
                        if (parentNodeRef == null) {
                            String parentNodeRefStr = null;
                            if (jo.isNull(JSON_PARENT_NODEREF)) {
                                if (!jo.isNull(JSON_NODEREF)) {
                                    parentNodeRef = lookupParentNodeRef(new NodeRef(jo.getString(JSON_NODEREF)));
                                    if (parentNodeRef != null) {
                                        parentNodeRefStr = parentNodeRef.toString();
                                        jo.put(JSON_PARENT_NODEREF, parentNodeRefStr);
                                    // note: currently only required during lookup/rollup
                                    // joLookup = jo;
                                    }
                                }
                            } else {
                                parentNodeRefStr = jo.getString(JSON_PARENT_NODEREF);
                            }
                            if (parentNodeRefStr != null) {
                                activityPost.setParentNodeRef(new NodeRef(parentNodeRefStr));
                            }
                        }
                        // lookup site (if needed)
                        String siteId = activityPost.getSiteNetwork();
                        if (siteId == null) {
                            if (!jo.isNull(JSON_NODEREF)) {
                                String nodeRefStr = jo.getString(JSON_NODEREF);
                                if (nodeRefStr != null) {
                                    siteId = lookupSite(new NodeRef(nodeRefStr));
                                    activityPost.setSiteNetwork(siteId);
                                }
                            }
                        }
                    }
                    if (joLookup != null) {
                        // extra data was looked-up
                        activityPost.setActivityData(joLookup.toString());
                    }
                    if ((activityPost.getActivityData() != null) && (activityPost.getActivityData().length() > ActivityPostDAO.MAX_LEN_ACTIVITY_DATA)) {
                        throw new IllegalArgumentException("Invalid activity data - exceeds " + ActivityPostDAO.MAX_LEN_ACTIVITY_DATA + " chars: " + activityPost.getActivityData());
                    }
                    if ((activityPost.getSiteNetwork() != null) && (activityPost.getSiteNetwork().length() > ActivityPostDAO.MAX_LEN_SITE_ID)) {
                        // belts-and-braces - should not get here since checked during post (and not modified)
                        throw new IllegalArgumentException("Invalid siteId - exceeds " + ActivityPostDAO.MAX_LEN_SITE_ID + " chars: " + activityPost.getSiteNetwork());
                    }
                    activityPost.setLastModified(new Date());
                    return null;
                }
            }, tenantDomain);
        } catch (Exception e) {
            // log error, but consume exception (skip this post)
            logger.error("Skipping activity post " + activityPost.getId() + ": " + e);
            activityPost.setStatus(ActivityPostEntity.STATUS.ERROR.toString());
        }
    }
    return activityPosts;
}
Also used : JSONTokener(org.json.JSONTokener) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.JSONObject) TenantUtil(org.alfresco.repo.tenant.TenantUtil) ActivityPostEntity(org.alfresco.repo.domain.activities.ActivityPostEntity) SQLException(java.sql.SQLException) JSONException(org.json.JSONException) JobExecutionException(org.quartz.JobExecutionException) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException) Date(java.util.Date) Pair(org.alfresco.util.Pair)

Example 2 with ActivityPostEntity

use of org.alfresco.repo.domain.activities.ActivityPostEntity in project alfresco-repository by Alfresco.

the class PostLookup method rollupPosts.

private List<ActivityPostEntity> rollupPosts(List<ActivityPostEntity> activityPosts) throws SQLException {
    Map<UserRollupActivity, List<ActivityPostEntity>> rollupPosts = new HashMap<UserRollupActivity, List<ActivityPostEntity>>();
    List<ActivityPostEntity> result = new ArrayList<ActivityPostEntity>(activityPosts.size());
    for (final ActivityPostEntity post : activityPosts) {
        if (rollupTypes.containsKey(post.getActivityType()) && (post.getParentNodeRef() != null)) {
            UserRollupActivity key = new UserRollupActivity(post.getUserId(), post.getActivityType(), post.getParentNodeRef());
            List<ActivityPostEntity> posts = rollupPosts.get(key);
            if (posts == null) {
                posts = new ArrayList<ActivityPostEntity>();
                rollupPosts.put(key, posts);
            }
            posts.add(post);
        } else {
            result.add(post);
        }
    }
    for (final Map.Entry<UserRollupActivity, List<ActivityPostEntity>> entry : rollupPosts.entrySet()) {
        final int count = entry.getValue().size();
        if (count >= rollupCount) {
            final ActivityPostEntity oldPost = entry.getValue().get(0);
            final String tenantDomain = oldPost.getTenantDomain();
            TenantUtil.runAsSystemTenant(new TenantUtil.TenantRunAsWork<Void>() {

                public Void doWork() throws Exception {
                    String postUserId = oldPost.getUserId();
                    // rollup - create a new 'posted' event that represents the rolled-up activity (and set others to 'processed')
                    ActivityPostEntity newPost = new ActivityPostEntity();
                    newPost.setActivityType(rollupTypes.get(oldPost.getActivityType()));
                    newPost.setPostDate(oldPost.getPostDate());
                    newPost.setUserId(postUserId);
                    newPost.setSiteNetwork(oldPost.getSiteNetwork());
                    newPost.setAppTool(oldPost.getAppTool());
                    newPost.setLastModified(oldPost.getLastModified());
                    newPost.setTenantDomain(tenantDomain);
                    newPost.setJobTaskNode(1);
                    try {
                        JSONObject jo = new JSONObject();
                        jo.put(JSON_NODEREF_PARENT, oldPost.getParentNodeRef().toString());
                        jo.put(JSON_TENANT_DOMAIN, tenantDomain);
                        jo.put(JSON_TITLE, "" + count);
                        Pair<String, String> firstLastName = lookupPerson(postUserId);
                        if (firstLastName != null) {
                            jo.put(JSON_FIRSTNAME, firstLastName.getFirst());
                            jo.put(JSON_LASTNAME, firstLastName.getSecond());
                        }
                        Path path = lookupPath(oldPost.getParentNodeRef());
                        if (path != null) {
                            String displayPath = PathUtil.getDisplayPath(path, true);
                            if (displayPath != null) {
                                // note: PathUtil.getDisplayPath returns prefix path as: '/company_home/sites/' rather than /Company Home/Sites'
                                String prefix = "/company_home/sites/" + tenantService.getBaseName(oldPost.getSiteNetwork()) + "/documentLibrary";
                                int idx = displayPath.indexOf(prefix);
                                if (idx == 0) {
                                    displayPath = displayPath.substring(prefix.length());
                                }
                                // Share-specific
                                jo.put(JSON_PAGE, "documentlibrary?path=" + displayPath);
                            }
                        }
                        newPost.setActivityData(jo.toString());
                        newPost.setStatus(ActivityPostEntity.STATUS.POSTED.toString());
                    } catch (JSONException e) {
                        logger.warn("Unable to create activity data: " + e);
                        newPost.setStatus(ActivityPostEntity.STATUS.ERROR.toString());
                    }
                    for (ActivityPostEntity post : entry.getValue()) {
                        post.setStatus(ActivityPostEntity.STATUS.PROCESSED.toString());
                    }
                    // add the new POSTED
                    entry.getValue().add(newPost);
                    return null;
                }
            }, tenantDomain);
        }
        result.addAll(entry.getValue());
    }
    return result;
}
Also used : Path(org.alfresco.service.cmr.repository.Path) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) ActivityPostEntity(org.alfresco.repo.domain.activities.ActivityPostEntity) SQLException(java.sql.SQLException) JSONException(org.json.JSONException) JobExecutionException(org.quartz.JobExecutionException) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) TenantUtil(org.alfresco.repo.tenant.TenantUtil) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.alfresco.util.Pair)

Example 3 with ActivityPostEntity

use of org.alfresco.repo.domain.activities.ActivityPostEntity in project alfresco-repository by Alfresco.

the class PostLookup method execute.

public void execute() throws JobExecutionException {
    checkProperties();
    // Avoid running when in read-only mode
    if (!transactionService.getAllowWrite()) {
        if (logger.isTraceEnabled()) {
            logger.trace("Post lookup not running due to read-only server");
        }
        return;
    }
    long start = System.currentTimeMillis();
    String lockToken = null;
    LockCallback lockCallback = new LockCallback();
    try {
        if (jobLockService != null) {
            lockToken = acquireLock(lockCallback);
        }
        ActivityPostEntity params = new ActivityPostEntity();
        params.setStatus(ActivityPostEntity.STATUS.PENDING.toString());
        if (logger.isDebugEnabled()) {
            logger.debug("Selecting activity posts with status: " + ActivityPostEntity.STATUS.PENDING.toString());
        }
        // get all pending post (for this job run)
        final List<ActivityPostEntity> activityPosts = postDAO.selectPosts(params, maxItemsPerCycle);
        if (activityPosts.size() > 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("Update: " + activityPosts.size() + " activity post" + (activityPosts.size() == 1 ? "s" : ""));
            }
            // execute in READ txn
            transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

                public Object execute() throws Throwable {
                    // lookup any additional data
                    lookupPosts(activityPosts);
                    return null;
                }
            }, true);
            // execute in WRITE txn
            List<ActivityPostEntity> activityPostsToUpdate = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<List<ActivityPostEntity>>() {

                public List<ActivityPostEntity> execute() throws Throwable {
                    // collapse (ie. rollup) and relevant posts
                    return rollupPosts(activityPosts);
                }
            }, false);
            // update posts + status (note: will also add any new rolled-up posts)
            updatePosts(activityPostsToUpdate);
            if (logger.isInfoEnabled()) {
                int cnt = activityPostsToUpdate.size();
                logger.info("Updated: " + cnt + " activity post" + (cnt == 1 ? "" : "s") + " (in " + (System.currentTimeMillis() - start) + " msecs)");
            }
        }
    } catch (LockAcquisitionException e) {
        // Job being done by another process
        if (logger.isDebugEnabled()) {
            logger.debug("execute: Can't get lock. Assume post lookup job already underway: " + e);
        }
    } catch (SQLException e) {
        logger.error("Exception during select of posts: ", e);
        throw new JobExecutionException(e);
    } catch (Throwable e) {
        // If the VM is shutting down, then ignore
        if (vmShutdownListener.isVmShuttingDown()) {
        // Ignore
        } else {
            logger.error("Exception during update of posts: ", e);
        }
    } finally {
        releaseLock(lockCallback, lockToken);
    }
}
Also used : SQLException(java.sql.SQLException) ActivityPostEntity(org.alfresco.repo.domain.activities.ActivityPostEntity) JobExecutionException(org.quartz.JobExecutionException) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException)

Example 4 with ActivityPostEntity

use of org.alfresco.repo.domain.activities.ActivityPostEntity in project alfresco-repository by Alfresco.

the class ActivityServiceImplTest method testLongName_ALF_10362.

public void testLongName_ALF_10362() throws Exception {
    byte[] namePattern = new byte[1024];
    Arrays.fill(namePattern, (byte) 'A');
    ActivityPostEntity params = new ActivityPostEntity();
    params.setStatus(ActivityPostEntity.STATUS.PENDING.toString());
    int cnt = postDAO.selectPosts(params, -1).size();
    activityService.postActivity("org.alfresco.testActivityType4", "site2", "appToolA", "{\"title\":\"" + new String(namePattern, "UTF-8") + "\"}");
    assertEquals(cnt + 1, postDAO.selectPosts(params, -1).size());
}
Also used : ActivityPostEntity(org.alfresco.repo.domain.activities.ActivityPostEntity)

Example 5 with ActivityPostEntity

use of org.alfresco.repo.domain.activities.ActivityPostEntity in project alfresco-repository by Alfresco.

the class ActivityPostDAOImpl method selectPosts.

@SuppressWarnings("unchecked")
public List<ActivityPostEntity> selectPosts(ActivityPostEntity activityPost, int maxItems) throws SQLException {
    int rowLimit = maxItems < 0 ? RowBounds.NO_ROW_LIMIT : maxItems;
    RowBounds rowBounds = new RowBounds(RowBounds.NO_ROW_OFFSET, rowLimit);
    if ((activityPost.getJobTaskNode() != -1) && (activityPost.getMinId() != -1) && (activityPost.getMaxId() != -1) && (activityPost.getStatus() != null)) {
        return template.selectList("alfresco.activities.select_activity_posts_by_params", activityPost, rowBounds);
    } else if (activityPost.getStatus() != null) {
        return template.selectList("alfresco.activities.select_activity_posts_by_status", activityPost, rowBounds);
    } else {
        return new ArrayList<ActivityPostEntity>(0);
    }
}
Also used : RowBounds(org.apache.ibatis.session.RowBounds) ActivityPostEntity(org.alfresco.repo.domain.activities.ActivityPostEntity)

Aggregations

ActivityPostEntity (org.alfresco.repo.domain.activities.ActivityPostEntity)11 SQLException (java.sql.SQLException)6 JSONObject (org.json.JSONObject)6 Date (java.util.Date)5 JSONException (org.json.JSONException)5 LockAcquisitionException (org.alfresco.repo.lock.LockAcquisitionException)4 JobExecutionException (org.quartz.JobExecutionException)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 TenantUtil (org.alfresco.repo.tenant.TenantUtil)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 Pair (org.alfresco.util.Pair)3 HashMap (java.util.HashMap)2 JSONTokener (org.json.JSONTokener)2 TemplateException (freemarker.template.TemplateException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1