Search in sources :

Example 41 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class UserCSVUploadGet method buildPropertiesForHeader.

@Override
protected List<Pair<QName, Boolean>> buildPropertiesForHeader(Object resource, String format, WebScriptRequest req) {
    List<Pair<QName, Boolean>> properties = new ArrayList<Pair<QName, Boolean>>(UserCSVUploadPost.COLUMNS.length);
    boolean required = true;
    for (QName qname : UserCSVUploadPost.COLUMNS) {
        Pair<QName, Boolean> p = null;
        if (qname != null) {
            p = new Pair<QName, Boolean>(qname, required);
        } else {
            required = false;
        }
        properties.add(p);
    }
    return properties;
}
Also used : QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) Pair(org.alfresco.util.Pair)

Example 42 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class SiteAdminSitesGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
    // check the current user access rights
    if (!siteService.isSiteAdmin(currentUser)) {
        // Note: security, no message to indicate why
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Resource not found.");
    }
    // Create paging
    final ScriptPagingDetails paging = new ScriptPagingDetails(getIntParameter(req, MAX_ITEMS, DEFAULT_MAX_ITEMS_PER_PAGE), getIntParameter(req, SKIP_COUNT, 0));
    // request a total count of found items
    paging.setRequestTotalCountMax(Integer.MAX_VALUE);
    final List<FilterProp> filterProp = getFilterProperties(req.getParameter(NAME_FILTER));
    final List<Pair<QName, Boolean>> sortProps = new ArrayList<Pair<QName, Boolean>>();
    sortProps.add(new Pair<QName, Boolean>(ContentModel.PROP_NAME, true));
    PagingResults<SiteInfo> pagingResults = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<PagingResults<SiteInfo>>() {

        public PagingResults<SiteInfo> doWork() throws Exception {
            return siteService.listSites(filterProp, sortProps, paging);
        }
    }, AuthenticationUtil.getAdminUserName());
    List<SiteInfo> result = pagingResults.getPage();
    List<SiteState> sites = new ArrayList<SiteState>(result.size());
    for (SiteInfo info : result) {
        sites.add(SiteState.create(info, siteService.listMembers(info.getShortName(), null, SiteModel.SITE_MANAGER, 0), currentUser, nodeService, personService));
    }
    Map<String, Object> sitesData = new HashMap<String, Object>(6);
    // Site data
    sitesData.put("items", sites);
    // Paging data
    sitesData.put("count", result.size());
    sitesData.put("hasMoreItems", pagingResults.hasMoreItems());
    sitesData.put("totalItems", (pagingResults.getTotalResultCount() == null ? -1 : pagingResults.getTotalResultCount().getFirst()));
    sitesData.put("skipCount", paging.getSkipCount());
    sitesData.put("maxItems", paging.getMaxItems());
    // Create the model from the site and pagination data
    Map<String, Object> model = new HashMap<String, Object>(1);
    model.put("data", sitesData);
    return model;
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) PagingResults(org.alfresco.query.PagingResults) FilterProp(org.alfresco.repo.node.getchildren.FilterProp) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString) FilterTypeString(org.alfresco.repo.node.getchildren.FilterPropString.FilterTypeString) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) ScriptPagingDetails(org.alfresco.util.ScriptPagingDetails) Pair(org.alfresco.util.Pair)

Example 43 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class WebDAVLockServiceImpl method sessionDestroyed.

@Override
@SuppressWarnings("unchecked")
public void sessionDestroyed() {
    HttpSession session = currentSession.get();
    if (session == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Couldn't find current session.");
        }
        return;
    }
    // look for locked documents list in http session
    final List<Pair<String, NodeRef>> lockedResources = (List<Pair<String, NodeRef>>) session.getAttribute(LOCKED_RESOURCES);
    if (lockedResources != null && lockedResources.size() > 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("Found " + lockedResources.size() + " locked resources for session: " + session.getId());
        }
        for (Pair<String, NodeRef> lockedResource : lockedResources) {
            String runAsUser = lockedResource.getFirst();
            final NodeRef nodeRef = lockedResource.getSecond();
            // there are some document that should be forcibly unlocked
            AuthenticationUtil.runAs(new RunAsWork<Void>() {

                @Override
                public Void doWork() throws Exception {
                    return transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

                        @Override
                        public Void execute() throws Throwable {
                            // check whether this document still exists in repo
                            if (nodeService.exists(nodeRef)) {
                                if (logger.isDebugEnabled()) {
                                    logger.debug("Trying to release lock for: " + nodeRef);
                                }
                                // check the lock status of document
                                LockStatus lockStatus = lockService.getLockStatus(nodeRef);
                                // check if document was checked out
                                boolean hasWorkingCopy = checkOutCheckInService.getWorkingCopy(nodeRef) != null;
                                boolean isWorkingCopy = nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WORKING_COPY);
                                // forcibly unlock document if it is still locked and not checked out
                                if ((lockStatus.equals(LockStatus.LOCKED) || lockStatus.equals(LockStatus.LOCK_OWNER)) && !hasWorkingCopy && !isWorkingCopy) {
                                    try {
                                        // try to unlock it
                                        lockService.unlock(nodeRef);
                                        if (logger.isDebugEnabled()) {
                                            logger.debug("Lock was successfully released for: " + nodeRef);
                                        }
                                    } catch (Exception e) {
                                        if (logger.isDebugEnabled()) {
                                            logger.debug("Unable to unlock " + nodeRef + " cause: " + e.getMessage());
                                        }
                                    }
                                } else {
                                    // document is not locked or is checked out
                                    if (logger.isDebugEnabled()) {
                                        logger.debug("Skip lock releasing for: " + nodeRef + " as it is not locked or is checked out");
                                    }
                                }
                            } else {
                                // document no longer exists in repo
                                if (logger.isDebugEnabled()) {
                                    logger.debug("Skip lock releasing for an unexisting node: " + nodeRef);
                                }
                            }
                            return null;
                        }
                    }, transactionService.isReadOnly());
                }
            }, runAsUser == null ? AuthenticationUtil.getSystemUserName() : runAsUser);
        }
    } else {
        // there are no documents with unexpected lock left on it
        if (logger.isDebugEnabled()) {
            logger.debug("No locked resources were found for session: " + session.getId());
        }
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) LockStatus(org.alfresco.service.cmr.lock.LockStatus) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.alfresco.util.Pair)

Example 44 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class StatsGet method getStartAndEndDates.

/**
 * Parses ISO8601 formatted Date Strings.
 * @param start If start is null then defaults to 1 month
 * @param end If end is null then it defaults to now();
 */
public static Pair<LocalDate, LocalDate> getStartAndEndDates(String start, String end) {
    if (start == null)
        return null;
    LocalDate startDate = LocalDate.parse(start);
    LocalDate endDate = end != null ? LocalDate.parse(end) : LocalDate.now();
    return new Pair<LocalDate, LocalDate>(startDate, endDate);
}
Also used : LocalDate(org.joda.time.LocalDate) Pair(org.alfresco.util.Pair)

Example 45 with Pair

use of org.alfresco.util.Pair in project alfresco-remote-api by Alfresco.

the class AbstractDiscussionWebScript method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    if (templateVars == null) {
        String error = "No parameters supplied";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Parse the JSON, if supplied
    JSONObject json = null;
    String contentType = req.getContentType();
    if (contentType != null && contentType.indexOf(';') != -1) {
        contentType = contentType.substring(0, contentType.indexOf(';'));
    }
    if (MimetypeMap.MIMETYPE_JSON.equals(contentType)) {
        JSONParser parser = new JSONParser();
        try {
            json = (JSONObject) parser.parse(req.getContent().getContent());
        } catch (IOException io) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + io.getMessage());
        } catch (ParseException pe) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + pe.getMessage());
        }
    }
    // Did they request it by node reference or site?
    NodeRef nodeRef = null;
    SiteInfo site = null;
    TopicInfo topic = null;
    PostInfo post = null;
    if (templateVars.containsKey("site")) {
        // Site, and optionally topic
        String siteName = templateVars.get("site");
        site = siteService.getSite(siteName);
        if (site == null) {
            String error = "Could not find site: " + siteName;
            throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
        }
        // Did they give a topic name too?
        if (templateVars.containsKey("path")) {
            String name = templateVars.get("path");
            topic = discussionService.getTopic(site.getShortName(), name);
            if (topic == null) {
                String error = "Could not find topic '" + name + "' for site '" + site.getShortName() + "'";
                throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
            }
            nodeRef = topic.getNodeRef();
        } else {
            // The NodeRef is the container (if it exists)
            if (siteService.hasContainer(siteName, DiscussionServiceImpl.DISCUSSION_COMPONENT)) {
                nodeRef = siteService.getContainer(siteName, DiscussionServiceImpl.DISCUSSION_COMPONENT);
            }
        }
    } else if (templateVars.containsKey("store_type") && templateVars.containsKey("store_id") && templateVars.containsKey("id")) {
        // NodeRef, normally Topic or Discussion
        StoreRef store = new StoreRef(templateVars.get("store_type"), templateVars.get("store_id"));
        nodeRef = new NodeRef(store, templateVars.get("id"));
        if (!nodeService.exists(nodeRef)) {
            String error = "Could not find node: " + nodeRef;
            throw new WebScriptException(Status.STATUS_NOT_FOUND, error);
        }
        // Try to build the appropriate object for it
        Pair<TopicInfo, PostInfo> objects = discussionService.getForNodeRef(nodeRef);
        if (objects != null) {
            topic = objects.getFirst();
            post = objects.getSecond();
        }
        // See if it's actually attached to a site
        if (topic != null) {
            NodeRef container = topic.getContainerNodeRef();
            if (container != null) {
                NodeRef maybeSite = nodeService.getPrimaryParent(container).getParentRef();
                if (maybeSite != null) {
                    // Try to make it a site, will return Null if it isn't one
                    site = siteService.getSite(maybeSite);
                }
            }
        }
    } else {
        String error = "Unsupported template parameters found";
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, error);
    }
    // Have the real work done
    return executeImpl(site, nodeRef, topic, post, req, json, status, cache);
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) StoreRef(org.alfresco.service.cmr.repository.StoreRef) IOException(java.io.IOException) TopicInfo(org.alfresco.service.cmr.discussion.TopicInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) PostInfo(org.alfresco.service.cmr.discussion.PostInfo) ParseException(org.json.simple.parser.ParseException) Pair(org.alfresco.util.Pair)

Aggregations

Pair (org.alfresco.util.Pair)61 ArrayList (java.util.ArrayList)34 NodeRef (org.alfresco.service.cmr.repository.NodeRef)23 QName (org.alfresco.service.namespace.QName)22 HashMap (java.util.HashMap)16 PagingRequest (org.alfresco.query.PagingRequest)13 Paging (org.alfresco.rest.framework.resource.parameters.Paging)12 List (java.util.List)11 Serializable (java.io.Serializable)10 HashSet (java.util.HashSet)9 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)9 Set (java.util.Set)8 Map (java.util.Map)7 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)7 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)7 AbstractList (java.util.AbstractList)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 PagingResults (org.alfresco.query.PagingResults)6 Node (org.alfresco.rest.api.model.Node)6