use of org.alfresco.repo.security.authority.AuthorityDAO in project acs-community-packaging by Alfresco.
the class BaseAssociationEditor method getAvailableOptions.
/**
* Retrieves the available options for the current association
*
* @param context Faces Context
* @param contains The contains part of the query
*/
protected void getAvailableOptions(FacesContext context, String contains) {
AssociationDefinition assocDef = getAssociationDefinition(context);
if (assocDef != null) {
// find and show all the available options for the current association
String type = assocDef.getTargetClass().getName().toString();
if (type.equals(ContentModel.TYPE_AUTHORITY_CONTAINER.toString())) {
UserTransaction tx = null;
try {
tx = Repository.getUserTransaction(context, true);
tx.begin();
String safeContains = null;
if (contains != null && contains.length() > 0) {
safeContains = Utils.remove(contains.trim(), "\"");
safeContains = safeContains.toLowerCase();
}
// get all available groups
AuthorityService authorityService = Repository.getServiceRegistry(context).getAuthorityService();
Set<String> groups = authorityService.getAllAuthoritiesInZone(AuthorityService.ZONE_APP_DEFAULT, AuthorityType.GROUP);
this.availableOptions = new ArrayList<NodeRef>(groups.size());
// get the NodeRef for each matching group
AuthorityDAO authorityDAO = (AuthorityDAO) FacesContextUtils.getRequiredWebApplicationContext(context).getBean("authorityDAO");
if (authorityDAO != null) {
List<String> matchingGroups = new ArrayList<String>();
String groupDisplayName;
for (String group : groups) {
// get display name, if not present strip prefix from group id
groupDisplayName = authorityService.getAuthorityDisplayName(group);
if (groupDisplayName == null || groupDisplayName.length() == 0) {
groupDisplayName = group.substring(PermissionService.GROUP_PREFIX.length());
}
// otherwise just add the group name to the sorted set
if (safeContains != null) {
if (groupDisplayName.toLowerCase().indexOf(safeContains) != -1) {
matchingGroups.add(group);
}
} else {
matchingGroups.add(group);
}
}
// sort the group names
Collections.sort(matchingGroups, new SimpleStringComparator());
// go through the sorted set and get the NodeRef for each group
for (String groupName : matchingGroups) {
NodeRef groupRef = authorityDAO.getAuthorityNodeRefOrNull(groupName);
if (groupRef != null) {
this.availableOptions.add(groupRef);
}
}
}
// commit the transaction
tx.commit();
} catch (Throwable err) {
Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_GENERIC), err.getMessage()), err);
this.availableOptions = Collections.<NodeRef>emptyList();
try {
if (tx != null) {
tx.rollback();
}
} catch (Exception tex) {
}
}
} else if (type.equals(ContentModel.TYPE_PERSON.toString())) {
List<Pair<QName, String>> filter = (contains != null && contains.trim().length() > 0) ? Utils.generatePersonFilter(contains.trim()) : null;
// Always sort by last name, then first name
List<Pair<QName, Boolean>> sort = new ArrayList<Pair<QName, Boolean>>();
sort.add(new Pair<QName, Boolean>(ContentModel.PROP_LASTNAME, true));
sort.add(new Pair<QName, Boolean>(ContentModel.PROP_FIRSTNAME, true));
// Log the filtering
if (logger.isDebugEnabled())
logger.debug("Query filter: " + filter);
// How many to limit too?
int maxResults = Application.getClientConfig(context).getSelectorsSearchMaxResults();
if (maxResults <= 0) {
maxResults = Utils.getPersonMaxResults();
}
List<PersonInfo> persons = Repository.getServiceRegistry(context).getPersonService().getPeople(filter, true, sort, new PagingRequest(maxResults, null)).getPage();
// Save the results
List<NodeRef> nodes = new ArrayList<NodeRef>(persons.size());
for (PersonInfo person : persons) {
nodes.add(person.getNodeRef());
}
this.availableOptions = nodes;
} else {
// for all other types/aspects perform a lucene search
StringBuilder query = new StringBuilder("+TYPE:\"");
if (assocDef.getTargetClass().isAspect()) {
query = new StringBuilder("+ASPECT:\"");
} else {
query = new StringBuilder("+TYPE:\"");
}
query.append(type);
query.append("\"");
if (contains != null && contains.trim().length() != 0) {
String safeContains = null;
if (contains != null && contains.length() > 0) {
safeContains = Utils.remove(contains.trim(), "\"");
safeContains = safeContains.toLowerCase();
}
query.append(" AND +@");
String nameAttr = Repository.escapeQName(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "name"));
query.append(nameAttr);
query.append(":\"*" + safeContains + "*\"");
}
int maxResults = Application.getClientConfig(context).getSelectorsSearchMaxResults();
if (logger.isDebugEnabled()) {
logger.debug("Query: " + query.toString());
logger.debug("Max results size: " + maxResults);
}
SearchParameters searchParams = new SearchParameters();
searchParams.addStore(Repository.getStoreRef());
searchParams.setLanguage(SearchService.LANGUAGE_LUCENE);
searchParams.setQuery(query.toString());
if (maxResults > 0) {
searchParams.setLimit(maxResults);
searchParams.setLimitBy(LimitBy.FINAL_SIZE);
}
ResultSet results = null;
try {
results = Repository.getServiceRegistry(context).getSearchService().query(searchParams);
this.availableOptions = results.getNodeRefs();
} catch (SearcherException se) {
logger.info("Search failed for: " + query, se);
Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_QUERY));
} finally {
if (results != null) {
results.close();
}
}
}
if (logger.isDebugEnabled())
logger.debug("Found " + this.availableOptions.size() + " available options");
}
}
use of org.alfresco.repo.security.authority.AuthorityDAO in project records-management by Alfresco.
the class BaseRMTestCase method setupTestDataImpl.
/**
* Impl of test data setup
*/
protected void setupTestDataImpl() {
AuthorityDAO authDao = (AuthorityDAO) applicationContext.getBean("authorityDAO");
if (!authDao.authorityExists(AuthenticationUtil.getSystemUserName())) {
createPerson(AuthenticationUtil.getSystemUserName(), false);
}
assertTrue("No person object for System available.", authDao.authorityExists(AuthenticationUtil.getSystemUserName()));
storeRef = StoreRef.STORE_REF_WORKSPACE_SPACESSTORE;
rootNodeRef = nodeService.getRootNode(storeRef);
// Create folder
String containerName = "RM2_" + System.currentTimeMillis();
Map<QName, Serializable> containerProps = new HashMap<QName, Serializable>(1);
containerProps.put(ContentModel.PROP_NAME, containerName);
folder = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, containerName), ContentModel.TYPE_FOLDER, containerProps).getChildRef();
assertNotNull("Could not create base folder", folder);
permissionService.setPermission(folder, ADMIN_USER, PermissionService.WRITE, true);
permissionService.setPermission(folder, ADMIN_USER, PermissionService.ADD_CHILDREN, true);
if (isRMSiteTest()) {
siteId = GUID.generate();
siteInfo = siteService.createSite("rm-site-dashboard", siteId, "title", "descrition", SiteVisibility.PUBLIC, RecordsManagementModel.TYPE_RM_SITE);
filePlan = siteService.getContainer(siteId, RmSiteType.COMPONENT_DOCUMENT_LIBRARY);
assertNotNull("Site document library container was not created successfully.", filePlan);
// Create RM container
rmContainer = filePlanService.createRecordCategory(filePlan, "rmContainer");
assertNotNull("Could not create rm container", rmContainer);
// Create disposition schedule
dispositionSchedule = utils.createBasicDispositionSchedule(rmContainer);
// Create RM folder
rmFolder = recordFolderService.createRecordFolder(rmContainer, "rmFolder");
assertNotNull("Could not create rm folder", rmFolder);
}
}
Aggregations