NIHVIVO-2279 add more methods to the UserAccountsDao, with tests for the Jena implementation.

This commit is contained in:
j2blake 2011-05-09 18:17:42 +00:00
parent 9682b91f94
commit 9f91be45e1
7 changed files with 576 additions and 10 deletions

View file

@ -20,6 +20,45 @@ public interface UserAccountsDao {
*/
UserAccount getUserAccountByUri(String uri);
/**
* Create a new UserAccount in the model.
*
* On entry, the URI of the UserAccount should be empty. On exit, the URI
* which was created for this UserAccount will be stored in the UserAccount,
* as well as being returned by the method.
*
* Does not confirm that PermissionSet objects already exist for the
* PermissionSet URIs referenced by the UserAcocunt.
*
* @throws NullPointerException
* if the UserAccount is null.
* @throws IllegalArgumentException
* if the URI of the UserAccount is not empty.
*/
String insertUserAccount(UserAccount userAccount);
/**
* Update the values on a UserAccount that already exists in the model.
*
* Does not confirm that PermissionSet objects already exist for the
* PermissionSet URIs referenced by the UserAcocunt.
*
* @throws NullPointerException
* if the UserAccount is null.
* @throws IllegalArgumentException
* if a UserAccount with this URI does not already exist in the
* model.
*/
void updateUserAccount(UserAccount userAccount);
/**
* Remove the UserAccount with this URI from the model.
*
* If the URI is null, or if no UserAccount with this URI is found in the
* model, no action is taken.
*/
void deleteUserAccount(String userAccountUri);
/**
* Get the PermissionSet for this URI.
*

View file

@ -32,6 +32,21 @@ public class UserAccountsDaoFiltering extends BaseFiltering implements
return innerDao.getUserAccountByUri(uri);
}
@Override
public String insertUserAccount(UserAccount userAccount) {
return innerDao.insertUserAccount(userAccount);
}
@Override
public void updateUserAccount(UserAccount userAccount) {
innerDao.updateUserAccount(userAccount);
}
@Override
public void deleteUserAccount(String userAccountUri) {
innerDao.deleteUserAccount(userAccountUri);
}
@Override
public PermissionSet getPermissionSetByUri(String uri) {
return innerDao.getPermissionSetByUri(uri);

View file

@ -258,6 +258,16 @@ public class JenaBaseDao extends JenaBaseDaoCon {
}
}
/**
* convenience method
*/
protected void addPropertyLongValue(Resource res, Property dataprop,
long value, Model model) {
if (dataprop != null) {
model.add(res, dataprop, Long.toString(value), XSDDatatype.XSDlong);
}
}
/**
* convenience method for use with functional datatype properties
*/
@ -318,6 +328,33 @@ public class JenaBaseDao extends JenaBaseDaoCon {
}
/**
* convenience method for use with functional datatype properties
*/
protected void updatePropertyLongValue(Resource res, Property dataprop,
Long value, Model model) {
if (dataprop != null) {
Long existingValue = null;
Statement stmt = res.getProperty(dataprop);
if (stmt != null) {
RDFNode object = stmt.getObject();
if (object != null && object.isLiteral()) {
existingValue = ((Literal) object).getLong();
}
}
if (existingValue == null) {
model.add(res, dataprop, value.toString(),
XSDDatatype.XSDlong);
} else if (existingValue.longValue() != value) {
model.removeAll(res, dataprop, null);
model.add(res, dataprop, value.toString(),
XSDDatatype.XSDlong);
}
}
}
/**
* convenience method
*/
@ -622,6 +659,59 @@ public class JenaBaseDao extends JenaBaseDaoCon {
}
}
/**
* convenience method to update the value(s) of a one-to-many object
* property
*
* NOTE: this should be run from within a CriticalSection(WRITE)
*/
protected void updatePropertyResourceURIValues(Resource res, Property prop,
Collection<String> uris, Model model) {
log.debug("updatePropertyResourceURIValues(), resource="
+ (res == null ? "null" : res.getURI()) + ", property="
+ (prop == null ? "null" : prop.getURI()) + ", uris=" + uris);
if ((res == null) || (prop == null)) {
return;
}
// figure existing URIs
Set<String> existingUris = new HashSet<String>();
StmtIterator stmts = model.listStatements(res, prop, (RDFNode) null);
while (stmts.hasNext()) {
Statement stmt = stmts.next();
RDFNode o = stmt.getObject();
if (o instanceof Resource) {
existingUris.add(((Resource) o).getURI());
}
}
// figure which to add and which to remove
Set<String> addingUris = new HashSet<String>(uris);
addingUris.removeAll(existingUris);
Set<String> removingUris = new HashSet<String>(existingUris);
removingUris.removeAll(uris);
// for each to remove, remove it.
for (String removeUri : removingUris) {
Resource o = model.getResource(removeUri);
model.remove(res, prop, o);
}
// for each to add, add it, unless it is null, empty, or invalid.
for (String addUri : addingUris) {
if ((addUri != null) && (!addUri.isEmpty())) {
String badUriErrorStr = checkURI(addUri);
if (badUriErrorStr == null) {
Resource o = model.getResource(addUri);
model.add(res, prop, o);
} else {
log.warn(badUriErrorStr);
}
}
}
}
/**
* convenience method for updating the RDFS label
*/

View file

@ -5,9 +5,11 @@ package edu.cornell.mannlib.vitro.webapp.dao.jena;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.util.iterator.ClosableIterator;
@ -16,6 +18,7 @@ import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vitro.webapp.beans.PermissionSet;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.dao.InsertException;
import edu.cornell.mannlib.vitro.webapp.dao.UserAccountsDao;
/**
@ -66,6 +69,120 @@ public class UserAccountsDaoJena extends JenaBaseDao implements UserAccountsDao
}
}
@Override
public String insertUserAccount(UserAccount userAccount) {
if (userAccount == null) {
throw new NullPointerException("userAccount may not be null.");
}
if (!userAccount.getUri().isEmpty()) {
throw new IllegalArgumentException(
"URI of new userAccount must be empty.");
}
OntModel model = getOntModel();
model.enterCriticalSection(Lock.WRITE);
try {
String userUri = getUnusedURI();
Resource res = model.createIndividual(userUri, USERACCOUNT);
addPropertyStringValue(res, USERACCOUNT_EMAIL_ADDRESS,
userAccount.getEmailAddress(), model);
addPropertyStringValue(res, USERACCOUNT_FIRST_NAME,
userAccount.getFirstName(), model);
addPropertyStringValue(res, USERACCOUNT_LAST_NAME,
userAccount.getLastName(), model);
addPropertyStringValue(res, USERACCOUNT_MD5_PASSWORD,
userAccount.getMd5Password(), model);
addPropertyStringValue(res, USERACCOUNT_OLD_PASSWORD,
userAccount.getOldPassword(), model);
addPropertyLongValue(res, USERACCOUNT_PASSWORD_LINK_EXPIRES,
userAccount.getPasswordLinkExpires(), model);
addPropertyBooleanValue(res, USERACCOUNT_PASSWORD_CHANGE_REQUIRED,
userAccount.isPasswordChangeRequired(), model);
addPropertyIntValue(res, USERACCOUNT_LOGIN_COUNT,
userAccount.getLoginCount(), model);
if (userAccount.getStatus() != null) {
addPropertyStringValue(res, USERACCOUNT_STATUS, userAccount
.getStatus().toString(), model);
}
updatePropertyResourceURIValues(res,
USERACCOUNT_HAS_PERMISSION_SET,
userAccount.getPermissionSetUris(), model);
userAccount.setUri(userUri);
return userUri;
} catch (InsertException e) {
log.error(e, e);
return null;
} finally {
model.leaveCriticalSection();
}
}
@Override
public void updateUserAccount(UserAccount userAccount) {
if (userAccount == null) {
throw new NullPointerException("userAccount may not be null.");
}
OntModel model = getOntModel();
model.enterCriticalSection(Lock.WRITE);
try {
OntResource res = model.getOntResource(userAccount.getUri());
if (res == null) {
throw new IllegalArgumentException("userAccount '"
+ userAccount.getUri() + "' does not exist.");
}
updatePropertyStringValue(res, USERACCOUNT_EMAIL_ADDRESS,
userAccount.getEmailAddress(), model);
updatePropertyStringValue(res, USERACCOUNT_FIRST_NAME,
userAccount.getFirstName(), model);
updatePropertyStringValue(res, USERACCOUNT_LAST_NAME,
userAccount.getLastName(), model);
updatePropertyStringValue(res, USERACCOUNT_MD5_PASSWORD,
userAccount.getMd5Password(), model);
updatePropertyStringValue(res, USERACCOUNT_OLD_PASSWORD,
userAccount.getOldPassword(), model);
updatePropertyLongValue(res, USERACCOUNT_PASSWORD_LINK_EXPIRES,
userAccount.getPasswordLinkExpires(), model);
updatePropertyBooleanValue(res,
USERACCOUNT_PASSWORD_CHANGE_REQUIRED,
userAccount.isPasswordChangeRequired(), model, true);
updatePropertyIntValue(res, USERACCOUNT_LOGIN_COUNT,
userAccount.getLoginCount(), model);
if (userAccount.getStatus() == null) {
updatePropertyStringValue(res, USERACCOUNT_STATUS, null, model);
} else {
updatePropertyStringValue(res, USERACCOUNT_STATUS, userAccount
.getStatus().toString(), model);
}
updatePropertyResourceURIValues(res,
USERACCOUNT_HAS_PERMISSION_SET,
userAccount.getPermissionSetUris(), model);
} finally {
model.leaveCriticalSection();
}
}
@Override
public void deleteUserAccount(String userAccountUri) {
if (userAccountUri == null) {
return;
}
OntModel model = getOntModel();
model.enterCriticalSection(Lock.WRITE);
try {
Resource res = model.createResource(userAccountUri);
model.removeAll(res, null, null);
} finally {
model.leaveCriticalSection();
}
}
@Override
public PermissionSet getPermissionSetByUri(String uri) {
if (uri == null) {
@ -120,4 +237,24 @@ public class UserAccountsDaoJena extends JenaBaseDao implements UserAccountsDao
return list;
}
private String getUnusedURI() throws InsertException {
String errMsg = null;
String namespace = DEFAULT_NAMESPACE;
String uri = null;
Random random = new Random(System.currentTimeMillis());
for (int attempts = 0; attempts < 30; attempts++) {
int upperBound = (int) Math.pow(2, attempts + 13);
uri = namespace + ("n" + random.nextInt(upperBound));
errMsg = getWebappDaoFactory().checkURI(uri);
if (errMsg == null) {
return uri;
}
}
throw new InsertException("Could not create URI for individual: "
+ errMsg);
}
}