Skip to content

Commit

Permalink
Merge pull request #235 from TAMULib/sprint12-staging
Browse files Browse the repository at this point in the history
Sprint12 Staging
  • Loading branch information
jmicah committed Jan 7, 2020
2 parents 07d7840 + ae3fc74 commit 6432f34
Show file tree
Hide file tree
Showing 20 changed files with 540 additions and 59 deletions.
16 changes: 14 additions & 2 deletions src/main/java/edu/tamu/app/controller/DocumentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static edu.tamu.weaver.response.ApiStatus.ERROR;
import static edu.tamu.weaver.response.ApiStatus.SUCCESS;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -169,16 +168,29 @@ public ApiResponse save(@RequestBody Document document) {
public ApiResponse push(@PathVariable String projectName, @PathVariable String documentName) {
Document document = documentRepo.findByProjectNameAndName(projectName, documentName);

if (document.isPublishing()) {
return new ApiResponse(ERROR, "Cannot publish because document is already pending publication");
}

document.isPublishing(true);
documentRepo.update(document);

for (ProjectRepository repository : document.getProject().getRepositories()) {
try {
document = ((Destination) projectServiceRegistry.getService(repository.getName())).push(document);
} catch (IOException e) {
} catch (Exception e) {
document.isPublishing(false);
documentRepo.update(document);

logger.error("Exception thrown attempting to push to " + repository.getName() + "!", e);
e.printStackTrace();
return new ApiResponse(ERROR, "There was an error publishing this item");
}
}

document.isPublishing(false);
documentRepo.update(document);

return new ApiResponse(SUCCESS, "Your item has been successfully published", document);
}

Expand Down
39 changes: 38 additions & 1 deletion src/main/java/edu/tamu/app/controller/ProjectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
import edu.tamu.app.model.repo.ProjectRepo;
import edu.tamu.app.model.repo.ProjectRepositoryRepo;
import edu.tamu.app.model.repo.ProjectSuggestorRepo;
import edu.tamu.app.model.repo.ResourceRepo;
import edu.tamu.app.service.ProjectFactory;
import edu.tamu.app.service.PropertyProtectionService;
import edu.tamu.app.service.SyncService;
import edu.tamu.app.model.repo.ResourceRepo;
import edu.tamu.app.service.registry.MagpieServiceRegistry;
import edu.tamu.app.service.repository.Destination;
import edu.tamu.app.utilities.FileSystemUtility;
Expand Down Expand Up @@ -86,6 +87,9 @@ public class ProjectController {
@Autowired
private SyncService syncService;

@Autowired
private PropertyProtectionService propertyProtectionService;

/**
* Endpoint to return list of projects.
*
Expand Down Expand Up @@ -116,8 +120,41 @@ public ApiResponse create(@WeaverValidatedModel Project project) throws IOExcept
@RequestMapping("/update")
@PreAuthorize("hasRole('MANAGER')")
public ApiResponse update(@WeaverValidatedModel Project project) {

Project currentProject = projectRepo.findOne(project.getId());
boolean refreshProjectListener = (currentProject.isHeadless() != project.isHeadless());

//we need to populate the values of any protected ProjectService properties by getting the full entities from the repo
List<Long> projectRepositoryIds = new ArrayList<Long>();
project.getRepositories().forEach(pr -> {
projectRepositoryIds.add(pr.getId());
});
project.setRepositories(projectRepositoryRepo.findAll(projectRepositoryIds));

project.getRepositories().forEach(r -> {
r.setPropertyProtectionService(propertyProtectionService);
});

List<Long> projectAuthorityIds = new ArrayList<Long>();
project.getAuthorities().forEach(pa -> {
projectAuthorityIds.add(pa.getId());
});
project.setAuthorities(projectAuthorityRepo.findAll(projectAuthorityIds));

project.getAuthorities().forEach(a -> {
a.setPropertyProtectionService(propertyProtectionService);
});

List<Long> projectSuggestorIds = new ArrayList<Long>();
project.getSuggestors().forEach(ps -> {
projectSuggestorIds.add(ps.getId());
});
project.setSuggestors(projectSuggestorRepo.findAll(projectSuggestorIds));

project.getSuggestors().forEach(s -> {
s.setPropertyProtectionService(propertyProtectionService);
});

BeanUtils.copyProperties(project, currentProject, "documents","profiles");
currentProject = projectRepo.update(currentProject);
projectFactory.registerServiceListeners(currentProject);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/edu/tamu/app/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class Document extends BaseEntity {
@Column(nullable = true)
private String path;

@Column(nullable = false)
private Boolean publishing;

@ManyToOne(optional = false)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, scope = Project.class, resolver = ProjectByNameResolver.class, property = "name")
@JsonIdentityReference(alwaysAsId = true)
Expand All @@ -57,6 +60,7 @@ public class Document extends BaseEntity {
public Document() {
fields = new ArrayList<MetadataFieldGroup>();
publishedLocations = new ArrayList<PublishedLocation>();
publishing = false;
}

public Document(Project project, String name, String path, String status) {
Expand Down Expand Up @@ -170,4 +174,12 @@ public MetadataFieldGroup getFieldByLabel(String labelName) {
return targetField;
}

public Boolean isPublishing() {
return publishing;
}

public void isPublishing(Boolean publishing) {
this.publishing = publishing;
}

}
30 changes: 27 additions & 3 deletions src/main/java/edu/tamu/app/model/ProjectService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.tamu.app.model;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -9,10 +11,12 @@
import javax.persistence.FetchType;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import edu.tamu.app.service.PropertyProtectionService;
import edu.tamu.weaver.validation.model.ValidatingBaseEntity;

@MappedSuperclass
Expand All @@ -28,6 +32,9 @@ public abstract class ProjectService extends ValidatingBaseEntity {
@Fetch(FetchMode.SELECT)
private List<ProjectSetting> settings;

@Transient
private PropertyProtectionService propertyProtectionService = null;

public ProjectService() {
settings = new ArrayList<ProjectSetting>();
}
Expand Down Expand Up @@ -57,14 +64,31 @@ public void setSettings(List<ProjectSetting> settings) {
}

public List<String> getSettingValues(String key) {
List<String> targetSetting = null;
List<String> targetSettingValues = null;
boolean isProtect = false;
for (ProjectSetting setting : settings) {
if (setting.getKey().equals(key)) {
targetSetting = setting.getValues();
isProtect = setting.isProtect();
targetSettingValues = setting.getValues();
break;
}
}
return targetSetting;
if (propertyProtectionService != null && isProtect) {
try {
targetSettingValues = propertyProtectionService.decryptPropertyValues(targetSettingValues);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
return targetSettingValues;
}

public PropertyProtectionService getPropertyProtectionService() {
return propertyProtectionService;
}

public void setPropertyProtectionService(PropertyProtectionService propertyProtectionService) {
this.propertyProtectionService = propertyProtectionService;
}

}
32 changes: 32 additions & 0 deletions src/main/java/edu/tamu/app/model/ProjectSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import javax.persistence.Entity;
import javax.persistence.FetchType;

import com.fasterxml.jackson.annotation.JsonGetter;

import edu.tamu.weaver.data.model.BaseEntity;

@Entity
Expand All @@ -19,6 +21,9 @@ public class ProjectSetting extends BaseEntity {
@ElementCollection(fetch = FetchType.EAGER)
private List<String> values;

@Column
private Boolean protect = false;

public ProjectSetting() {
setValues(new ArrayList<String>());
}
Expand All @@ -28,6 +33,12 @@ public ProjectSetting(String key, List<String> values) {
setValues(values);
}

public ProjectSetting(String key, List<String> values, Boolean protect) {
setKey(key);
setValues(values);
setProtect(protect);
}

public String getKey() {
return key;
}
Expand All @@ -40,6 +51,19 @@ public List<String> getValues() {
return values;
}

@JsonGetter("values")
protected List<String> getValuesForSerializer() {
if (this.isProtect()) {
ArrayList<String> protectedValues = new ArrayList<String>();
this.getValues().forEach(k -> {
protectedValues.add("");
});
return protectedValues;
} else {
return getValues();
}
}

public void setValues(List<String> values) {
this.values = values;
}
Expand All @@ -54,4 +78,12 @@ public void removeValue(String value) {
this.values.remove(value);
}

public Boolean isProtect() {
return protect;
}

public void setProtect(Boolean protect) {
this.protect = protect;
}

}
31 changes: 31 additions & 0 deletions src/main/java/edu/tamu/app/model/PublishingEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package edu.tamu.app.model;

import java.util.Date;

public class PublishingEvent {

private final PublishingType type;

private final String message;

private final Date timestamp;

public PublishingEvent(PublishingType type, String message) {
this.type = type;
this.message = message;
timestamp = new Date(System.currentTimeMillis());
}

public PublishingType getType() {
return type;
}

public String getMessage() {
return message;
}

public Date getTimestamp() {
return timestamp;
}

}
5 changes: 5 additions & 0 deletions src/main/java/edu/tamu/app/model/PublishingType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package edu.tamu.app.model;

public enum PublishingType {
ALERT, ATTACHMENT, CONNECTION, ITEM, MESSAGE, WARNING
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.tamu.app.model.repo.impl;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -12,6 +14,7 @@
import edu.tamu.app.model.repo.ProjectAuthorityRepo;
import edu.tamu.app.model.repo.ProjectRepo;
import edu.tamu.app.model.repo.custom.ProjectAuthorityRepoCustom;
import edu.tamu.app.service.PropertyProtectionService;
import edu.tamu.weaver.data.model.repo.impl.AbstractWeaverRepoImpl;

public class ProjectAuthorityRepoImpl extends AbstractWeaverRepoImpl<ProjectAuthority, ProjectAuthorityRepo> implements ProjectAuthorityRepoCustom {
Expand All @@ -22,6 +25,14 @@ public class ProjectAuthorityRepoImpl extends AbstractWeaverRepoImpl<ProjectAuth
@Autowired
private ProjectRepo projectRepo;

@Autowired
PropertyProtectionService propertyProtectionService;

@Override
public ProjectAuthority create(ProjectAuthority projectAuthority) {
return super.create(processProjectAuthority(projectAuthority));
}

@Override
public synchronized ProjectAuthority create(String name, ServiceType serviceType) {
return create(name, serviceType, null);
Expand All @@ -33,7 +44,24 @@ public synchronized ProjectAuthority create(String name, ServiceType serviceType
projectAuthority.setName(name);
projectAuthority.setType(serviceType);
projectAuthority.setSettings(settings);
return projectAuthorityRepo.create(projectAuthority);
return this.create(projectAuthority);
}

@Override
public ProjectAuthority update(ProjectAuthority projectAuthority) {
ProjectAuthority currentProjectAuthority = projectAuthorityRepo.findOne(projectAuthority.getId());
for (int i=0;i<projectAuthority.getSettings().size();i++) {
ProjectSetting setting = projectAuthority.getSettings().get(i);
if (setting.isProtect() && setting.getValues().stream().allMatch(v -> v.equals(""))) {
try {
setting.setValues(propertyProtectionService.decryptPropertyValues(currentProjectAuthority.getSettingValues(setting.getKey())));
projectAuthority.getSettings().set(i, setting);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
}
return super.update(processProjectAuthority(projectAuthority));
}

@Override
Expand All @@ -52,4 +80,18 @@ public void delete(ProjectAuthority projectAuthority) {
protected String getChannel() {
return "/channel/project-authority";
}

private ProjectAuthority processProjectAuthority(ProjectAuthority projectAuthority) {
projectAuthority.setPropertyProtectionService(propertyProtectionService);
projectAuthority.getSettings().forEach(s -> {
if (s.isProtect()) {
try {
s.setValues(propertyProtectionService.encryptPropertyValues(s.getValues()));
} catch (GeneralSecurityException | IOException e1) {
e1.printStackTrace();
}
}
});
return projectAuthority;
}
}

0 comments on commit 6432f34

Please sign in to comment.