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;
}
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;
}
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());
}
}
}
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);
}
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);
}
Aggregations