use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcUserRepository method grantProjectToProject.
@Override
@JdbcTransaction
public void grantProjectToProject(String grantToProjectKey, String onProjectKey, SecretKey onProjectSecret) throws RepositoryException {
DbHelper db = new DbHelper();
try {
ProjectEntity grantToProject = db.findProjectByKey(grantToProjectKey);
if (grantToProject == null) {
throw new RepositoryException("Project not found");
}
ProjectEntity onProject = db.findProjectByKey(onProjectKey);
if (onProject == null) {
throw new RepositoryException("Project not found");
}
byte[] projectSecretData = onProjectSecret.getEncoded();
PublicKey grantToProjectPublicKey = grantToProject.getPublicKey();
byte[] newSecretData;
try {
SecretStore store = new SecretStore(onProject.secretData);
Writer writer = store.buildWriter();
writer.writeAsymetricProjectKey(projectSecretData, grantToProject.id, grantToProjectPublicKey);
writer.close();
store.appendContents(writer);
newSecretData = store.getEncoded();
} catch (IOException e) {
throw new RepositoryException("Error writing secrets", e);
}
db.updateProjectSecret(onProject.id, newSecretData);
// db.insertUserProject(user.id, project.id);
} catch (SQLException e) {
throw new RepositoryException("Error reading groups", e);
} finally {
db.close();
}
}
use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method changeTags.
@Override
@JdbcTransaction
public Tags changeTags(ModelClass<?> modelClass, ProjectId project, ManagedItemId itemKey, TagChanges changeTags, Long ifVersion) throws RepositoryException {
DbHelper db = new DbHelper(modelClass, project);
try {
int projectId = db.mapToValue(project);
int modelId = db.mapToValue(modelClass.getItemType());
int serviceId = db.mapToValue(modelClass.getServiceType());
ItemEntity rs = db.queries.findItem(serviceId, modelId, projectId, itemKey.getKey());
if (rs == null) {
// TODO: Better exception??
throw new IllegalStateException("Not found");
}
int itemId = rs.id;
if (ifVersion != null) {
log.warn("CAS version swapping not implemented");
}
Tags tags = new Tags();
mapToTags(db.listTagsForItem(itemId), tags);
if (changeTags.addTags != null) {
for (Tag addTag : changeTags.addTags) {
if (tags.hasTag(addTag)) {
continue;
}
db.insertTag(itemId, addTag);
tags.add(addTag);
}
}
if (changeTags.removeTags != null) {
for (Tag removeTag : changeTags.removeTags) {
boolean removed = tags.remove(removeTag);
if (!removed) {
continue;
}
db.removeTag(itemId, removeTag);
}
}
return tags;
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} finally {
db.close();
}
}
use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method createManagedItem.
@Override
@JdbcTransaction
public <T extends ItemBase> T createManagedItem(ProjectId project, T item) throws RepositoryException {
DbHelper db = new DbHelper(item.getClass(), project);
try {
CryptoKey itemSecret = FathomdbCrypto.generateKey();
byte[] data = serialize(item, itemSecret);
byte[] secretData = itemSecrets.encodeItemSecret(itemSecret);
int itemId = db.insertItem(item, data, secretData);
Tags tags = item.tags;
if (tags != null && !tags.isEmpty()) {
db.insertTags(itemId, tags);
}
return item;
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} finally {
db.close();
}
}
use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method getManagedItem.
@Override
@JdbcTransaction
public ItemBase getManagedItem(PlatformLayerKey key, boolean fetchTags, SecretProvider secretProvider) throws RepositoryException {
DbHelper db = new DbHelper(key);
try {
ServiceProvider serviceProvider = serviceProviderDirectory.getServiceProvider(key.getServiceType());
if (serviceProvider == null) {
throw new IllegalStateException();
}
ModelClass<?> modelClass = serviceProvider.getModelClass(key.getItemType());
ServiceType serviceType = key.getServiceType();
ItemType itemType = key.getItemType();
ProjectId project = key.getProject();
ManagedItemId itemId = key.getItemId();
return fetchItem(db, serviceType, itemType, project, itemId, modelClass.getJavaClass(), secretProvider, fetchTags);
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} finally {
db.close();
}
}
use of com.fathomdb.jdbc.JdbcTransaction in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method findRoots.
@Override
@JdbcTransaction
public List<ItemBase> findRoots(ProjectId project, boolean fetchTags, SecretProvider secretProvider) throws RepositoryException {
DbHelper db = new DbHelper(project);
try {
// TODO: Push-down logic for item selection as well
JoinedQueryResult result = db.listRoots();
List<ItemBase> roots = mapItemsAndTags(project, secretProvider, db, result);
for (ItemBase root : roots) {
// A little bit of paranoia
boolean isRoot = true;
for (Tag tag : root.getTags()) {
boolean tagIsParent = Tag.PARENT.getKey().equals(tag.getKey());
if (tagIsParent) {
isRoot = false;
break;
}
}
assert isRoot;
if (!isRoot) {
throw new IllegalStateException();
}
}
return roots;
} catch (SQLException e) {
throw new RepositoryException("Error fetching items", e);
} finally {
db.close();
}
}
Aggregations