Skip to content

Commit

Permalink
add features for delete and api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Enquier committed May 21, 2024
1 parent 5b51661 commit fdc3643
Show file tree
Hide file tree
Showing 29 changed files with 179 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public MountJson getProjectUsages(String projectId, String refId, String commitI
boolean restrictOnPermissions) {
saw.add(Pair.of(projectId, refId));
List<ElementJson> mounts = getNodePersistence().findAllByNodeType(projectId, refId, commitId,
CameoNodeType.PROJECTUSAGE.getValue());
CameoNodeType.PROJECTUSAGE.getValue(), false);

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<MountJson> mountValues = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openmbee.mms.core.objects.ElementsResponse;
import org.openmbee.mms.core.services.NodeChangeInfo;
import org.openmbee.mms.core.services.NodeGetInfo;
import org.openmbee.mms.crud.CrudConstants;
import org.openmbee.mms.crud.domain.JsonDomain;
import org.openmbee.mms.json.ElementJson;
import org.openmbee.mms.view.services.PropertyData;
Expand All @@ -30,9 +31,10 @@ public class CameoViewService extends CameoNodeService implements ViewService {

@Override
public ElementsResponse getDocuments(String projectId, String refId, Map<String, String> params) {
Boolean deleted = Boolean.parseBoolean(params.getOrDefault(CrudConstants.DELETED, "false"));
String commitId = params.getOrDefault(CameoConstants.COMMITID, null);
List<ElementJson> documents = getNodePersistence().findAllByNodeType(projectId, refId,
commitId, CameoNodeType.DOCUMENT.getValue());
commitId, CameoNodeType.DOCUMENT.getValue(), deleted);
ElementsResponse res = this.getViews(projectId, refId, buildRequestFromJsons(documents), params);
for (ElementJson e: res.getElements()) {
Optional<ElementJson> parent = getFirstRelationshipOfType(projectId, refId, commitId, e,
Expand Down Expand Up @@ -83,9 +85,10 @@ public void addChildViews(ElementsResponse res, Map<String, String> params) {
}

public ElementsResponse getGroups(String projectId, String refId, Map<String, String> params) {
Boolean deleted = Boolean.parseBoolean(params.getOrDefault(CrudConstants.DELETED, "false"));
String commitId = params.getOrDefault(CameoConstants.COMMITID, null);
List<ElementJson> groups = getNodePersistence().findAllByNodeType(projectId, refId, commitId,
CameoNodeType.GROUP.getValue());
CameoNodeType.GROUP.getValue(), deleted);

ElementsResponse res = new ElementsResponse().setElements(groups);
for (ElementJson e: groups) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Constants {
public static final String FALSE = "false";
public static final String LIMIT = "limit";

public static final String HOME_SUFFIX = "-home";

public static final String MASTER_BRANCH = "master";

public static final Pattern BRANCH_ID_VALID_PATTERN = Pattern.compile("^[\\w-]+$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public interface NodePersistence {

NodeGetInfo findById(String projectId, String refId, String commitId, String elementId);

List<ElementJson> findAllByNodeType(String projectId, String refId, String commitId, int nodeType);
List<ElementJson> findAllByNodeType(String projectId, String refId, String commitId, int nodeType, Boolean deleted);

NodeGetInfo findAll(String projectId, String refId, String commitId, List<ElementJson> elements);

List<ElementJson> findAll(String projectId, String refId, String commitId);
List<ElementJson> findAll(String projectId, String refId, String commitId, Boolean deleted);

void streamAllAtCommit(String projectId, String refId, String commitId, OutputStream outputStream, String separator);
void streamAllAtCommit(String projectId, String refId, String commitId, OutputStream outputStream, String separator, Boolean deleted);

void branchElements(RefJson parentBranch, CommitJson parentCommit, RefJson targetBranch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class CrudConstants {
public static final String ORG = "Org";
public static final String CREATED = "created";
public static final String CREATING = "creating";
public static final String DELETED = "deleted";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import org.openmbee.mms.core.objects.OrganizationsRequest;
import org.openmbee.mms.core.objects.OrganizationsResponse;
import org.openmbee.mms.core.objects.Rejection;
import org.openmbee.mms.core.services.ProjectService;
import org.openmbee.mms.crud.CrudConstants;
import org.openmbee.mms.crud.controllers.BaseController;
import org.openmbee.mms.crud.services.OrgDeleteService;
import org.openmbee.mms.crud.services.ProjectDeleteService;
import org.openmbee.mms.core.exceptions.BadRequestException;
import org.openmbee.mms.core.exceptions.NotFoundException;
import org.openmbee.mms.json.OrgJson;
Expand All @@ -45,6 +47,8 @@ public class OrgsController extends BaseController {

OrgDeleteService orgDeleteService;

ProjectDeleteService projectDeleteService;

@Autowired
public OrgsController(OrgPersistence organizationRepository) {
this.organizationRepository = organizationRepository;
Expand All @@ -55,14 +59,19 @@ public void setOrgDeleteService(OrgDeleteService orgDeleteService) {
this.orgDeleteService = orgDeleteService;
}

@Autowired
public void setProjectDeleteService(ProjectDeleteService projectDeleteService) {
this.projectDeleteService = projectDeleteService;
}

@GetMapping
public OrganizationsResponse getAllOrgs(@RequestParam(required = false, defaultValue = Constants.FALSE) boolean includeArchived, Authentication auth) {

OrganizationsResponse response = new OrganizationsResponse();
Collection<OrgJson> allOrgs = organizationRepository.findAll();
for (OrgJson org : allOrgs) {
if (mss.hasOrgPrivilege(auth, org.getId(), Privileges.ORG_READ.name(), true)
&& (!Constants.TRUE.equals(org.getIsArchived()) || includeArchived)) {
&& (!org.isArchived() || includeArchived)) {
response.getOrgs().add(org);
}
}
Expand Down Expand Up @@ -119,19 +128,31 @@ public OrganizationsResponse createOrUpdateOrgs(
org.setCreator(auth.getName());
}
}
if (org.getIsArchived() != null && !org.getIsArchived().equals(o.getIsArchived())) {
List<ProjectJson> orgProjs = projectPersistence.findAllByOrgId(org.getId()).stream().collect(Collectors.toList());
//Un/Archive all projects contained by org
for (ProjectJson proj: orgProjs) {
proj.setIsArchived(org.getIsArchived());
projectPersistence.update(proj);
}
}
logger.info("Saving organization: {}", org.getId());
OrgJson saved = organizationRepository.save(org);
if (newOrg) {
// Initialize Org Permissions
permissionService.initOrgPerms(org.getId(), auth.getName());

createOrgHome(org);
} else {
Collection<ProjectJson> orgProjs = projectPersistence.findAllByOrgId(org.getId());
if (!orgProjs.stream().map(ProjectJson::getId).collect(Collectors.toList()).contains(org.getId() + Constants.HOME_SUFFIX)) {
if (org.isArchived() == null) {
org.setIsArchived(o.isArchived());
}
createOrgHome(org);
}
for (ProjectJson proj: orgProjs) {
if (org.isArchived() != null && !org.isArchived().equals(o.isArchived())) {
//Un/Archive all projects contained by org

proj.setIsArchived(org.isArchived());
projectPersistence.update(proj);
}
}
}

response.getOrgs().add(saved);
}
if (orgPost.getOrgs().size() == 1) {
Expand All @@ -151,10 +172,46 @@ public OrganizationsResponse deleteOrg(
if (!orgOption.isPresent()) {
throw new NotFoundException(response.addMessage("Organization not found."));
}
if (!projectPersistence.findAllByOrgId(orgId).isEmpty() && hard) {
throw new BadRequestException(response.addMessage("Cannot Hard Delete Organization that contains Projects"));
if (hard) {
List<ProjectJson> orgProjs = projectPersistence.findAllByOrgId(orgId).stream().collect(Collectors.toList());
if (!orgProjs.isEmpty()) {
if (orgProjs.size() == 1 && orgProjs.get(0).getId().equals(orgId + Constants.HOME_SUFFIX)) {
projectDeleteService.deleteProject(orgId + Constants.HOME_SUFFIX, hard);
} else {
throw new BadRequestException(response.addMessage("Cannot Hard Delete Organization that contains Projects other than its home"));
}
}
}

return orgDeleteService.deleteOrg(orgId, hard);
}

private ProjectJson createOrgHome(OrgJson org) {
// Create New Org "Home" Project
ProjectJson homeProj = new ProjectJson();
homeProj.setCreated(org.getCreated());
homeProj.setType(CrudConstants.PROJECT);
homeProj.setCreator(org.getCreator());
homeProj.setId(org.getId() + Constants.HOME_SUFFIX);
homeProj.setOrgId(org.getId());
homeProj.setIsArchived(org.isArchived());
homeProj.setName((!org.getName().isEmpty() ? org.getName() : org.getId()) + " Home");

ProjectService ps = getProjectService(homeProj);
ProjectJson savedProj = ps.create(homeProj);
permissionService.initProjectPerms(homeProj.getId(), true, org.getCreator());
return savedProj;
}

private ProjectService getProjectService(ProjectJson json) {
String type = json.getProjectType();
if (type == null || type.isEmpty()) {
try {
type = this.getProjectType(json.getProjectId());
} catch (NotFoundException e) {
type = CrudConstants.DEFAULT;
}
json.setProjectType(type);
}
return serviceFactory.getProjectService(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void setProjectSchemas(ProjectSchemas projectSchemas) {
}

@GetMapping
public ProjectsResponse getAllProjects(Authentication auth, @RequestParam(required = false) String orgId, @RequestParam(required = false, defaultValue = Constants.FALSE) boolean includeArchived) {
public ProjectsResponse getAllProjects(Authentication auth, @RequestParam(required = false) String orgId, @RequestParam(required = false, defaultValue = Constants.FALSE) boolean includeArchived, @RequestParam(required = false, defaultValue = Constants.FALSE) boolean includeHomes) {

ProjectsResponse response = new ProjectsResponse();
Collection<ProjectJson> allProjects =
Expand All @@ -54,7 +54,7 @@ public ProjectsResponse getAllProjects(Authentication auth, @RequestParam(requir
try {
if (mss.hasProjectPrivilege(auth, projectJson.getProjectId(), Privileges.PROJECT_READ.name(), true)
&& projectJson.getDocId() != null
&& (!Constants.TRUE.equals(projectJson.getIsArchived()) || includeArchived)) {
&& (!projectJson.isArchived() || includeArchived) && (!projectJson.getId().endsWith(Constants.HOME_SUFFIX) || includeHomes)) {
response.getProjects().add(projectJson);
}
} catch(NotFoundException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DefaultNodeUpdateFilter implements NodeUpdateFilter {
@Override
public boolean filterUpdate(NodeChangeInfo info, ElementJson updated, ElementJson existing) {
if (!info.getOverwrite()) {
if (Constants.TRUE.equals(existing.getIsArchived()) || isUpdated(updated, existing, info)) {
if ((existing.isArchived() != null && existing.isArchived()) || isUpdated(updated, existing, info)) {
return diffUpdateJson(updated, existing, info);
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void processElementAdded(NodeChangeInfo info, ElementJson element) {

element.setCreator(commitJson.getCreator()); //Only set on creation of new element
element.setCreated(commitJson.getCreated());
element.setIsArchived(false);
}

public boolean processElementUpdated(NodeChangeInfo info, ElementJson element, ElementJson existing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void setEventPublisher(Collection<EventService> eventPublisher) {
@Override
public void readAsStream(String projectId, String refId, Map<String, String> params, OutputStream stream,
String accept) throws IOException {

Boolean deleted = Boolean.parseBoolean(params.getOrDefault(CrudConstants.DELETED, "false"));
String commitId = params.getOrDefault(CrudConstants.COMMITID, null);

if (commitId != null && !commitId.isEmpty()) {
Expand All @@ -81,7 +81,7 @@ public void readAsStream(String projectId, String refId, Map<String, String> par
separator = ",";
}

nodePersistence.streamAllAtCommit(projectId, refId, commitId, stream, separator);
nodePersistence.streamAllAtCommit(projectId, refId, commitId, stream, separator, deleted);

if (!"application/x-ndjson".equals(accept)) {
stream.write("]}".getBytes(StandardCharsets.UTF_8));
Expand All @@ -99,6 +99,7 @@ public ElementsResponse read(String projectId, String refId, String id,
ElementsRequest req = buildRequest(id);
return read(projectId, refId, req, params);
}
Boolean deleted = Boolean.parseBoolean(params.getOrDefault(CrudConstants.DELETED, "false"));
String commitId = params.getOrDefault(CrudConstants.COMMITID, null);
if (commitId == null) {
Optional<CommitJson> commitJson = commitPersistence.findLatestByProjectAndRef(projectId, refId);
Expand All @@ -111,7 +112,7 @@ public ElementsResponse read(String projectId, String refId, String id,
ElementsResponse response = new ElementsResponse();
logger.debug("No ElementId given");

List<ElementJson> nodes = nodePersistence.findAll(projectId, refId, commitId);
List<ElementJson> nodes = nodePersistence.findAll(projectId, refId, commitId, deleted);
response.getElements().addAll(nodes);
response.getElements().forEach(v -> v.setRefId(refId));
response.setCommitId(commitId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ProjectJson create(ProjectJson project) {
try {
//TODO Transaction start
ProjectJson savedProjectJson = projectPersistence.save(project);
savedProjectJson.setIsArchived("false");
savedProjectJson.setIsArchived(false);
//create and save master branch. We're combining operations with the branch unifiedDAO.
branchPersistence.save(createMasterRefJson(savedProjectJson));
//TODO Transaction commit
Expand Down Expand Up @@ -112,7 +112,7 @@ public RefJson createMasterRefJson(ProjectJson project) {
branchJson.setCreated(project.getCreated());
branchJson.setProjectId(project.getId());
branchJson.setCreator(project.getCreator());
branchJson.setIsArchived("false");
branchJson.setIsArchived(false);
return branchJson;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public OrganizationsResponse deleteOrg(String orgId, boolean hard) {
projectDeleteService.deleteProject(proj.getId(), false);
}
orgPersistence.archiveById(orgId);
orgJson.setIsArchived(Constants.TRUE);
orgJson.setIsArchived(true);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ProjectsResponse deleteProject(String projectId, boolean hard) {
projectJson.setDeleted(true);
} else {
projectPersistence.archiveById(projectId);
projectJson.setIsArchived(Constants.TRUE);
projectJson.setIsArchived(true);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void create(String projectId) {
ProjectJson projectJson = newInstance();
projectJson.setProjectId(projectId);
projectJson.setType("default");
projectJson.setIsArchived("false");
projectJson.setIsArchived(true);
create(projectJson);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public RefJson save(RefJson refJson) {
scopedBranch.setTimestamp(Formats.FORMATTER.parse(refJson.getCreated(), Instant::from));
scopedBranch.setParentRefId(refJson.getParentRefId());
scopedBranch.setDocId(refJson.getDocId());
scopedBranch.setDeleted(Boolean.parseBoolean(refJson.getIsArchived()));
scopedBranch.setDeleted(refJson.isArchived());

//Setup global Branch object
Optional<Project> project = projectDAO.findByProjectId(refJson.getProjectId());
Expand Down Expand Up @@ -104,8 +104,8 @@ public RefJson update(RefJson refJson) {
ContextHolder.setContext(refJson.getProjectId());
Optional<Branch> optionalExisting = branchDAO.findByBranchId(refJson.getId());
Branch existing = optionalExisting.get();
if (refJson.getIsArchived() != null) {
existing.setDeleted(Boolean.parseBoolean(refJson.getIsArchived()));
if (refJson.isArchived() != null) {
existing.setDeleted(refJson.isArchived());
}

branchDAO.save(existing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ public NodeGetInfo findById(String projectId, String refId, String commitId, Str
}

@Override
public List<ElementJson> findAllByNodeType(String projectId, String refId, String commitId, int nodeType) {
public List<ElementJson> findAllByNodeType(String projectId, String refId, String commitId, int nodeType, Boolean deleted) {
ContextHolder.setContext(projectId, refId);
String commitToPass = checkCommit(refId, commitId);
List<Node> nodes;
if (commitToPass != null) {
nodes = nodeDAO.findAllByNodeType(nodeType);
} else {
nodes = nodeDAO.findAllByDeletedAndNodeType(false, nodeType);
nodes = nodeDAO.findAllByDeletedAndNodeType(deleted, nodeType);
}
return new ArrayList<>(getNodeGetDomain().processGetJsonFromNodes(nodes, commitToPass).getActiveElementMap().values());
}
Expand All @@ -130,28 +130,28 @@ public NodeGetInfo findAll(String projectId, String refId, String commitId, List
}

@Override
public List<ElementJson> findAll(String projectId, String refId, String commitId) {
public List<ElementJson> findAll(String projectId, String refId, String commitId, Boolean deleted) {
ContextHolder.setContext(projectId, refId);
List<Node> nodes;
String commitToPass = checkCommit(refId, commitId);
if (commitToPass != null) {
nodes = nodeDAO.findAll();
} else {
nodes = nodeDAO.findAllByDeleted(false);
nodes = nodeDAO.findAllByDeleted(deleted);
}
return new ArrayList<>(getNodeGetDomain().processGetJsonFromNodes(nodes, commitToPass).getActiveElementMap().values());
}


@Override
public void streamAllAtCommit(String projectId, String refId, String commitId, OutputStream stream, String separator) {
public void streamAllAtCommit(String projectId, String refId, String commitId, OutputStream stream, String separator, Boolean deleted) {
ContextHolder.setContext(projectId, refId);
List<Node> nodes;
final String commitToPass = checkCommit(refId, commitId);
if (commitToPass != null) {
nodes = nodeDAO.findAll();
} else {
nodes = nodeDAO.findAllByDeleted(false);
nodes = nodeDAO.findAllByDeleted(deleted);
}
AtomicInteger counter = new AtomicInteger();
batches(nodes, streamLimit).forEach(ns -> {
Expand Down
Loading

0 comments on commit fdc3643

Please sign in to comment.