Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor code refactoring changes #2778

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void testGetCustomAttributes() {
}

@Test(expected = NullPointerException.class)
public void testGetForm1() {
public void testGetFormWithNullUserName() {
PersonForm personForm = userAccountHelper.getForm(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
*/
public class EntityIdentifier implements Serializable {
private static final long serialVersionUID = 1L;
// Named constants for HashCodeBuilder
private static final int INITIAL_NON_ZERO_ODD_NUMBER = -646446001;
private static final int MULTIPLIER_NON_ZERO_ODD_NUMBER = 994968607;

protected final String key;
protected final Class<? extends IBasicEntity> type;
Expand Down Expand Up @@ -66,7 +69,7 @@ public boolean equals(Object object) {
/** @see java.lang.Object#hashCode() */
@Override
public int hashCode() {
return new HashCodeBuilder(-646446001, 994968607)
return new HashCodeBuilder(INITIAL_NON_ZERO_ODD_NUMBER, MULTIPLIER_NON_ZERO_ODD_NUMBER)
.append(this.type)
.append(this.key)
.toHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,69 +38,54 @@ public class PersonDirectorySearcher implements ITypedEntitySearcher {
private static final Log log = LogFactory.getLog(PersonDirectorySearcher.class);

private final Class<? extends IBasicEntity> personEntityType = IPerson.class;
private IPersonAttributeDao personAttributeDao;
private IUsernameAttributeProvider usernameAttributeProvider;
private final IPersonAttributeDao personAttributeDao;
private final IUsernameAttributeProvider usernameAttributeProvider;

@Autowired
public void setUsernameAttributeProvider(IUsernameAttributeProvider usernameAttributeProvider) {
this.usernameAttributeProvider = usernameAttributeProvider;
}

@Autowired
public void setPersonAttributeDao(IPersonAttributeDao personAttributeDao) {
public PersonDirectorySearcher(
IPersonAttributeDao personAttributeDao,
IUsernameAttributeProvider usernameAttributeProvider) {
this.personAttributeDao = personAttributeDao;
this.usernameAttributeProvider = usernameAttributeProvider;
}

@Override
public EntityIdentifier[] searchForEntities(String query, SearchMethod method)
throws GroupsException {
query = transformQuery(query, method);
log.debug("Searching for a person directory account matching query string " + query);

final String usernameAttribute = this.usernameAttributeProvider.getUsernameAttribute();
final Map<String, Object> queryMap = Collections.singletonMap(usernameAttribute, query);
final Set<IPersonAttributes> results = this.personAttributeDao.getPeople(queryMap);
// create an array of EntityIdentifiers from the search results
final List<EntityIdentifier> entityIdentifiers = new ArrayList<>(results.size());
for (final IPersonAttributes personAttributes : results) {
entityIdentifiers.add(
new EntityIdentifier(personAttributes.getName(), this.personEntityType));
}

return entityIdentifiers.toArray(new EntityIdentifier[entityIdentifiers.size()]);
}

private String transformQuery(String query, SearchMethod method) throws GroupsException {
// Ignores CS / CI
switch (method) {
case DISCRETE:
case DISCRETE_CI:
{
break;
}
return query;
case STARTS_WITH:
case STARTS_WITH_CI:
{
query = query + IPersonAttributeDao.WILDCARD;
break;
}
return query + IPersonAttributeDao.WILDCARD;
case ENDS_WITH:
case ENDS_WITH_CI:
{
query = IPersonAttributeDao.WILDCARD + query;
break;
}
return IPersonAttributeDao.WILDCARD + query;
case CONTAINS:
case CONTAINS_CI:
{
query = IPersonAttributeDao.WILDCARD + query + IPersonAttributeDao.WILDCARD;
break;
}
return IPersonAttributeDao.WILDCARD + query + IPersonAttributeDao.WILDCARD;
default:
{
throw new GroupsException("Unknown search type");
}
throw new GroupsException("Unknown search type");
}

log.debug("Searching for a person directory account matching query string " + query);

final String usernameAttribute = this.usernameAttributeProvider.getUsernameAttribute();
final Map<String, Object> queryMap =
Collections.<String, Object>singletonMap(usernameAttribute, query);
final Set<IPersonAttributes> results = this.personAttributeDao.getPeople(queryMap);

// create an array of EntityIdentifiers from the search results
final List<EntityIdentifier> entityIdentifiers =
new ArrayList<EntityIdentifier>(results.size());
for (final IPersonAttributes personAttributes : results) {
entityIdentifiers.add(
new EntityIdentifier(personAttributes.getName(), this.personEntityType));
}

return entityIdentifiers.toArray(new EntityIdentifier[entityIdentifiers.size()]);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1093,18 +1093,14 @@ private IPermission[] primGetPermissionsForPrincipal(
for (int i = 0; i < perms.length; i++) {
String permissionTarget = perms[i].getTarget();

if (
// owner matches
(owner == null || owner.equals(perms[i].getOwner()))
&&
// activity matches
(activity == null || activity.equals(perms[i].getActivity()))
&&
// target matches or is a member of the current permission target
(target == null
boolean isOwnerMatch = owner == null || owner.equals(perms[i].getOwner());
boolean isActivityMatch = activity == null || activity.equals(perms[i].getActivity());
boolean isTargetMatch =
target == null
|| target.equals(permissionTarget)
|| containingGroups.contains(permissionTarget))) {
|| containingGroups.contains(permissionTarget);

if (isOwnerMatch && isActivityMatch && isTargetMatch) {
al.add(perms[i]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@Service("portalInfoProvider")
public class PortalInfoProviderImpl implements IPortalInfoProvider, ReadWriteCallback<String> {
protected final Logger logger = LoggerFactory.getLogger(getClass());

private static final int TOKEN_LENGTH = 4;
private String serverName;
private String networkInterfaceName;

Expand Down Expand Up @@ -89,7 +89,7 @@ public String doInWriteLock(ReadResult<String> readResult) {
this.resolvedUniqueServerName =
this.resolvedServerName
+ "_"
+ RandomTokenGenerator.INSTANCE.generateRandomToken(4);
+ RandomTokenGenerator.INSTANCE.generateRandomToken(TOKEN_LENGTH);
return this.resolvedServerName;
}

Expand Down