Skip to content

Commit

Permalink
Merge pull request #11 from Open-MBEE/feature/elasticRefactor
Browse files Browse the repository at this point in the history
refactor elastic impl into module, use optional for single returns, w…
  • Loading branch information
HuiJun authored Jan 6, 2019
2 parents 6f7f20b + 867b383 commit 12533c9
Show file tree
Hide file tree
Showing 48 changed files with 774 additions and 691 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.openmbee.sdvc.authenticator.security.UserDetailsImpl;
import org.openmbee.sdvc.core.domains.Privilege;
import org.openmbee.sdvc.core.domains.Role;
Expand Down Expand Up @@ -35,13 +36,13 @@ public void setPasswordEncoder(PasswordEncoder passwordEncoder) {

@Override
public UserDetailsImpl loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
Optional<User> user = userRepository.findByUsername(username);

if (user == null) {
if (!user.isPresent()) {
throw new UsernameNotFoundException(
String.format("No user found with username '%s'.", username));
}
return new UserDetailsImpl(user);
return new UserDetailsImpl(user.get());
}

private Collection<? extends GrantedAuthority> getAuthorities(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openmbee.sdvc.authenticator.services;

import java.util.Optional;
import org.openmbee.sdvc.core.domains.User;

public interface UserService {
Expand All @@ -8,9 +9,9 @@ public interface UserService {

void delete(User user);

User findByEmail(String email);
Optional<User> findByEmail(String email);

User findByUsername(String username);
Optional<User> findByUsername(String username);

Iterable<User> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openmbee.sdvc.authenticator.services;

import java.util.Optional;
import org.openmbee.sdvc.core.domains.User;
import org.openmbee.sdvc.core.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -27,11 +28,11 @@ public Iterable<User> findAll() {
return userRepository.findAll();
}

public User findByEmail(String email) {
public Optional<User> findByEmail(String email) {
return userRepository.findByEmail(email);
}

public User findByUsername(String username) {
public Optional<User> findByUsername(String username) {
return userRepository.findByUsername(username);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
Expand All @@ -15,6 +16,7 @@ public class Organization extends Base {
private String organizationName;

@JsonProperty("id")
@Column(unique = true)
private String organizationId;

@OneToMany(mappedBy = "organization")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.openmbee.sdvc.core.repositories;

import java.util.Optional;
import org.openmbee.sdvc.core.domains.Organization;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OrganizationRepository extends JpaRepository<Organization, String> {

Organization findByOrganizationId(String id);
Optional<Organization> findByOrganizationId(String id);

Organization findByOrganizationName(String name);
Optional<Organization> findByOrganizationName(String name);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.openmbee.sdvc.core.repositories;

import java.util.Optional;
import org.openmbee.sdvc.core.domains.Project;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProjectRepository extends JpaRepository<Project, String> {

Project findByProjectId(String id);
Optional<Project> findByProjectId(String id);

Project findByProjectName(String name);
Optional<Project> findByProjectName(String name);

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.openmbee.sdvc.core.repositories;

import java.util.Optional;
import org.openmbee.sdvc.core.domains.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RoleRepository extends JpaRepository<Role, String> {

Role findByName(String name);
Optional<Role> findByName(String name);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.openmbee.sdvc.core.repositories;

import java.util.Optional;
import org.openmbee.sdvc.core.domains.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, String> {

User findByEmail(String email);
Optional<User> findByEmail(String email);

User findByUsername(String username);
Optional<User> findByUsername(String username);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openmbee.sdvc.crud.controllers.branches;

import java.time.Instant;
import java.util.Optional;
import org.openmbee.sdvc.crud.config.DbContextHolder;
import org.openmbee.sdvc.crud.controllers.BaseController;
import org.openmbee.sdvc.crud.controllers.BaseResponse;
Expand Down Expand Up @@ -62,11 +63,16 @@ public ResponseEntity<? extends BaseResponse> handleRequest(
}

if (branch.getParentCommitId() != null) {
Commit parentCommit = commitRepository
Optional<Commit> parentCommit = commitRepository
.findByCommitId(branch.getParentCommitId());
b.setParentCommit(parentCommit.getId());
if (parentCommit.isPresent()) {
b.setParentCommit(parentCommit.get().getId());
}
} else {
b.setParentCommit(commitRepository.findLatestByRef(b.getParentRefId()).getId());
Optional<Commit> parentCommit = commitRepository.findLatestByRef(b.getParentRefId());
if (parentCommit.isPresent()) {
b.setParentCommit(parentCommit.get().getId());
}
}

b.setTimestamp(now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ResponseEntity<? extends BaseResponse> handlePost(

if (!req.getElements().isEmpty()) {
NodeService nodeService = getNodeService(projectId);
ElementsResponse response = nodeService.create(projectId, refId, req, params);
ElementsResponse response = nodeService.createOrUpdate(projectId, refId, req, params);
return ResponseEntity.ok(response);
}
ElementsResponse err = new ElementsResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import java.util.Map;
import org.openmbee.sdvc.crud.controllers.BaseResponse;
import org.openmbee.sdvc.crud.controllers.Constants;
import org.openmbee.sdvc.json.ElementJson;

public class ElementsResponse extends BaseResponse {

public ElementsResponse() {
this.put(Constants.ELEMENT_KEY, new ArrayList<Map>());
this.put(Constants.ELEMENT_KEY, new ArrayList<ElementJson>());
}

public List<Map> getElements() {
return (List<Map>) this.get(Constants.ELEMENT_KEY);
public List<ElementJson> getElements() {
return (List<ElementJson>) this.get(Constants.ELEMENT_KEY);
}

public void setElements(List<Map> elements) {
public void setElements(List<ElementJson> elements) {
this.put(Constants.ELEMENT_KEY, elements);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openmbee.sdvc.crud.controllers.orgs;

import java.util.List;
import java.util.Optional;
import javax.transaction.Transactional;
import org.openmbee.sdvc.core.domains.Organization;
import org.openmbee.sdvc.core.repositories.OrganizationRepository;
Expand Down Expand Up @@ -34,11 +35,13 @@ public ResponseEntity<?> handleGet(@PathVariable(required = false) String orgId)

if (orgId != null) {
logger.debug("OrgId given: ", orgId);
Organization org = organizationRepository.findByOrganizationId(orgId);
Optional<Organization> orgOption = organizationRepository.findByOrganizationId(orgId);
if (!orgOption.isPresent()) {
return ResponseEntity.notFound().build();
}
OrgJson orgJson = new OrgJson();
orgJson.merge(convertToMap(org));
orgJson.merge(convertToMap(orgOption.get()));
response.getOrgs().add(orgJson);
return ResponseEntity.ok(response);
} else {
logger.debug("No OrgId given");
List<Organization> allOrgs = organizationRepository.findAll();
Expand All @@ -47,8 +50,8 @@ public ResponseEntity<?> handleGet(@PathVariable(required = false) String orgId)
orgJson.merge(convertToMap(org));
response.getOrgs().add(orgJson);
}
return ResponseEntity.ok(response);
}
return ResponseEntity.ok(response);
}

@PostMapping
Expand All @@ -61,18 +64,17 @@ public ResponseEntity<? extends BaseResponse> handlePost(
OrganizationsResponse response = new OrganizationsResponse();

for (OrgJson org : orgPost.getOrgs()) {
Organization o = new Organization();
//TODO reject if orgId isn't there
Organization o = organizationRepository.findByOrganizationId(org.getId())
.orElse(new Organization());
o.setOrganizationId(org.getId());
o.setOrganizationName(org.getName());
logger.info("Saving organization: {}", o.getOrganizationId());
Organization saved = organizationRepository.save(o);
org.merge(convertToMap(saved));
response.getOrgs().add(org);
}

return ResponseEntity.ok(response);
} else {

}
logger.debug("Bad Request");
OrganizationsResponse err = new OrganizationsResponse();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openmbee.sdvc.crud.controllers.projects;

import java.util.List;
import java.util.Optional;
import javax.transaction.Transactional;
import org.openmbee.sdvc.core.domains.Project;
import org.openmbee.sdvc.core.repositories.ProjectRepository;
Expand Down Expand Up @@ -31,14 +32,18 @@ public ProjectsController(ProjectRepository projectRepository) {

@GetMapping(value = {"", "/{projectId}"})
@Transactional
public ResponseEntity<? extends BaseResponse> handleGet(@PathVariable(required = false) String projectId) {
public ResponseEntity<? extends BaseResponse> handleGet(
@PathVariable(required = false) String projectId) {
ProjectsResponse response = new ProjectsResponse();

if (projectId != null) {
logger.debug("ProjectId given: ", projectId);
Project org = projectRepository.findByProjectId(projectId);
Optional<Project> orgOption = projectRepository.findByProjectId(projectId);
if (!orgOption.isPresent()) {
return ResponseEntity.notFound().build();
}
ProjectJson projectJson = new ProjectJson();
projectJson.merge(convertToMap(org));
projectJson.merge(convertToMap(orgOption.get()));
response.getProjects().add(projectJson);
} else {
logger.debug("No ProjectId given");
Expand All @@ -49,7 +54,6 @@ public ResponseEntity<? extends BaseResponse> handleGet(@PathVariable(required =
response.getProjects().add(projectJson);
}
}

return ResponseEntity.ok(response);
}

Expand All @@ -65,6 +69,7 @@ public ResponseEntity<? extends BaseResponse> handlePost(
}
ProjectsResponse response = new ProjectsResponse();
for (ProjectJson json: projectsPost.getProjects()) {
//TODO reject if projectId isn't there
ProjectService ps = getProjectService(json.getProjectId());
if (!ps.exists(json.getProjectId())) {
response.getProjects().add(ps.create(json));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Node {
@Column(name = "id", updatable = false, nullable = false)
Long id;

@Column(unique = true)
private String nodeId;
private String indexId;
private String lastCommit;
Expand Down
Loading

0 comments on commit 12533c9

Please sign in to comment.