Skip to content

Commit

Permalink
Merge pull request #106 from jacomago/fix-property-update
Browse files Browse the repository at this point in the history
Makes sure to update the value of the property on the channels
  • Loading branch information
shroffk authored Sep 21, 2023
2 parents aeecbf2 + ab8ab95 commit da69ebf
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 71 deletions.
145 changes: 74 additions & 71 deletions src/main/java/org/phoebus/channelfinder/PropertyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -296,91 +297,93 @@ public Property addSingle(@PathVariable("propertyName") String propertyName, @Pa
@PostMapping("/{propertyName}")
public Property update(@PathVariable("propertyName") String propertyName, @RequestBody Property property) {
// check if authorized role
if(authorizationService.isAuthorizedRole(SecurityContextHolder.getContext().getAuthentication(), ROLES.CF_PROPERTY)) {
long start = System.currentTimeMillis();
propertyManagerAudit.log(Level.INFO, () -> MessageFormat.format(TextUtil.CLIENT_INITIALIZATION, (System.currentTimeMillis() - start)));
// Validate request parameters
validatePropertyRequest(property);
if(!authorizationService.isAuthorizedRole(SecurityContextHolder.getContext().getAuthentication(), ROLES.CF_PROPERTY)) {
String message = MessageFormat.format(TextUtil.USER_NOT_AUTHORIZED_ON_PROPERTY, propertyName);
logger.log(Level.SEVERE, message, new ResponseStatusException(HttpStatus.UNAUTHORIZED));
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, message, null);
}

// check if authorized owner
if(!authorizationService.isAuthorizedOwner(SecurityContextHolder.getContext().getAuthentication(), property)) {
String message = MessageFormat.format(TextUtil.USER_NOT_AUTHORIZED_ON_PROPERTY, property.toLog());
long start = System.currentTimeMillis();
propertyManagerAudit.log(Level.INFO, () -> MessageFormat.format(TextUtil.CLIENT_INITIALIZATION, (System.currentTimeMillis() - start)));
// Validate request parameters
validatePropertyRequest(property);

// check if authorized owner
if(!authorizationService.isAuthorizedOwner(SecurityContextHolder.getContext().getAuthentication(), property)) {
String message = MessageFormat.format(TextUtil.USER_NOT_AUTHORIZED_ON_PROPERTY, property.toLog());
logger.log(Level.SEVERE, message, new ResponseStatusException(HttpStatus.UNAUTHORIZED));
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, message, null);
}

List<Channel> chans = new ArrayList<>();
Optional<Property> existingProperty = propertyRepository.findById(propertyName,true);
Property newProperty;
if(existingProperty.isPresent()) {
if(!authorizationService.isAuthorizedOwner(SecurityContextHolder.getContext().getAuthentication(), existingProperty.get())) {
String message = MessageFormat.format(TextUtil.USER_NOT_AUTHORIZED_ON_PROPERTY, existingProperty.get().toLog());
logger.log(Level.SEVERE, message, new ResponseStatusException(HttpStatus.UNAUTHORIZED));
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, message, null);
}
List<Channel> chans = new ArrayList<>();
Optional<Property> existingProperty = propertyRepository.findById(propertyName,true);
Property newProperty;
if(existingProperty.isPresent()) {
if(!authorizationService.isAuthorizedOwner(SecurityContextHolder.getContext().getAuthentication(), existingProperty.get())) {
String message = MessageFormat.format(TextUtil.USER_NOT_AUTHORIZED_ON_PROPERTY, existingProperty.get().toLog());
logger.log(Level.SEVERE, message, new ResponseStatusException(HttpStatus.UNAUTHORIZED));
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, message, null);
}
chans = existingProperty.get().getChannels();
newProperty = existingProperty.get();
newProperty.setOwner(property.getOwner());
// Is an existing channel being renamed
if (!property.getName().equalsIgnoreCase(existingProperty.get().getName())) {
// Since this is a rename operation we will need to remove the old channel.
propertyRepository.deleteById(existingProperty.get().getName());
newProperty.setName(property.getName());
}
} else {
newProperty = property;
chans = existingProperty.get().getChannels();
newProperty = existingProperty.get();
newProperty.setOwner(property.getOwner());
// Is an existing channel being renamed
if (!property.getName().equalsIgnoreCase(existingProperty.get().getName())) {
// Since this is a rename operation we will need to remove the old channel.
propertyRepository.deleteById(existingProperty.get().getName());
newProperty.setName(property.getName());
}
} else {
newProperty = property;
}

// update property
Property updatedProperty = propertyRepository.save(newProperty);

// update channels of existing property
if(!chans.isEmpty()) {
List<Channel> chanList = new ArrayList<>();
boolean updated;
for(Channel chan: chans) {
updated = false;
for(Channel updatedChan: updatedProperty.getChannels()) {
if(chan.getName().equals(updatedChan.getName()))
{
updated = true;
break;
}
}
if(!updated) {
Property prop = new Property(property.getName(),property.getOwner(),chan.getProperties().get(0).getValue());
chan.setProperties(Arrays.asList(prop));
// update property
Property updatedProperty = propertyRepository.save(newProperty);

// update channels of existing property
if(!chans.isEmpty()) {
List<Channel> chanList = new ArrayList<>();
for(Channel chan: chans) {
boolean alreadyUpdated = updatedProperty.getChannels().stream()
.anyMatch(c -> c.getName().equals(chan.getName()));

if(!alreadyUpdated) {
Optional<String> value = chan.getProperties().stream()
.filter(p -> p.getName().equals(propertyName))
.findFirst()
.map(Property::getValue);
if (value.isPresent()) {
Property prop = new Property(property.getName(),property.getOwner(),value.get());
chan.setProperties(List.of(prop));
chanList.add(chan);
}
}
if(!chanList.isEmpty())
channelRepository.saveAll(chanList);
}
if(!chanList.isEmpty())
channelRepository.saveAll(chanList);
}

if(!property.getChannels().isEmpty()) {
// update the listed channels in the property's payloads with the new property
Iterable<Channel> channels = channelRepository.saveAll(property.getChannels());
List<Channel> chanList = new ArrayList<>();
Property p = null;
for(Channel chan: channels) {
chan.setTags(new ArrayList<>());
for(Property prop: chan.getProperties())
{
if(prop.getName().equals(propertyName))
p = prop;
}
chan.setProperties(Arrays.asList(p));
chanList.add(chan);
if(!property.getChannels().isEmpty()) {
// update the listed channels in the property's payloads with the new property
Iterable<Channel> channels = channelRepository.saveAll(property.getChannels());
List<Channel> chanList = new ArrayList<>();
Property p = null;
for(Channel chan: channels) {
chan.setTags(new ArrayList<>());
for(Property prop: chan.getProperties())
{
if(prop.getName().equals(propertyName))
p = prop;
}
if(!chanList.isEmpty())
updatedProperty.setChannels(chanList);
chan.setProperties(Collections.singletonList(p));
chanList.add(chan);
}

return updatedProperty;
} else {
String message = MessageFormat.format(TextUtil.USER_NOT_AUTHORIZED_ON_PROPERTY, propertyName);
logger.log(Level.SEVERE, message, new ResponseStatusException(HttpStatus.UNAUTHORIZED));
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, message, null);
if(!chanList.isEmpty())
updatedProperty.setChannels(chanList);
}

return updatedProperty;

}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/phoebus/channelfinder/PropertyManagerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,36 @@ public void updatePropertyTest5() {
Assertions.assertEquals(testProperty0WithChannels, propertyRepository.findById(testProperty0WithChannels.getName(), true).get(), "Failed to update property " + testProperty0WithChannels);
}

/**
* Update the value of a property when channels have multiple properties
*/
@Test
public void updatePropertyWithChannelsTest() {
// A test property with testChannel0,testChannel1
Property testProperty0WithChannels = new Property("testProperty0WithChannels", "testOwner");
testProperty0WithChannels.setChannels(Arrays.asList(
new Channel(testChannel0.getName(), testChannel0.getOwner(), List.of(new Property(testProperty0WithChannels.getName(), testProperty0WithChannels.getOwner(), "property0channel0")), new ArrayList<Tag>()),
new Channel(testChannel1.getName(), testChannel1.getOwner(), List.of(new Property(testProperty0WithChannels.getName(), testProperty0WithChannels.getOwner(), "property0channel1")), new ArrayList<Tag>())));

Property testProperty1WithChannels = new Property("testProperty1WithChannels", "testOwner");
testProperty1WithChannels.setChannels(Arrays.asList(
new Channel(testChannel0.getName(), testChannel0.getOwner(), List.of(new Property(testProperty1WithChannels.getName(), testProperty1WithChannels.getOwner(), "property1channel0")), new ArrayList<Tag>()),
new Channel(testChannel1.getName(), testChannel1.getOwner(), List.of(new Property(testProperty1WithChannels.getName(), testProperty1WithChannels.getOwner(), "property1channel1")), new ArrayList<Tag>())));

propertyManager.create(testProperty0WithChannels.getName(), testProperty0WithChannels);
propertyManager.create(testProperty1WithChannels.getName(), testProperty1WithChannels);

Property newValueProperty = new Property(testProperty1WithChannels.getName(), testProperty1WithChannels.getOwner(), "newValueProperty");
newValueProperty.setChannels(List.of(
new Channel(testChannel1.getName(), testChannel1.getOwner(), List.of(new Property(newValueProperty.getName(), newValueProperty.getOwner(), "newValueProperty")), new ArrayList<Tag>())));
propertyManager.update(newValueProperty.getName(), newValueProperty);

List<Property> expected0Properties = List.of(new Property(testProperty0WithChannels.getName(), testProperty0WithChannels.getOwner(), "property0channel0"), new Property(testProperty1WithChannels.getName(), testProperty1WithChannels.getOwner(), "property1channel0"));
Assertions.assertEquals(expected0Properties, channelRepository.findById(testChannel0.getName()).get().getProperties());
List<Property> expected1Properties = List.of(new Property(testProperty0WithChannels.getName(), testProperty0WithChannels.getOwner(), "property0channel1"), new Property(newValueProperty.getName(), newValueProperty.getOwner(), "newValueProperty"));
Assertions.assertEquals(expected1Properties, channelRepository.findById(testChannel1.getName()).get().getProperties());
}

/**
* Update multiple properties
* Update on non-existing properties should result in the creation of the properties
Expand Down

0 comments on commit da69ebf

Please sign in to comment.