Skip to content

Commit

Permalink
Merged in nojira/attribute_bundles (pull request #675)
Browse files Browse the repository at this point in the history
NOJIRA - expanding the values that can be used for att bundles

Approved-by: Dmitriy Kopylenko
  • Loading branch information
chasegawa authored and sporth committed Oct 7, 2024
2 parents 6f1b105 + 09afabb commit bfc6d4a
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class JsonSchemaBuilderService {
List<Object> result = new ArrayList<>()
List<String> resultNames = new ArrayList<>()
attributeBundleService.findAll().forEach({ bundle ->
result.add(bundle.getAttributes())
result.add(bundle.getAttributeNames())
resultNames.add(bundle.getName())
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.internet2.tier.shibboleth.admin.ui.configuration;

import edu.internet2.tier.shibboleth.admin.ui.domain.BundleableAttributeType;
import edu.internet2.tier.shibboleth.admin.ui.repository.AttributeBundleRepository;
import edu.internet2.tier.shibboleth.admin.ui.repository.EntityDescriptorRepository;
import edu.internet2.tier.shibboleth.admin.ui.security.model.Group;
import edu.internet2.tier.shibboleth.admin.ui.security.model.Ownership;
Expand All @@ -13,11 +15,19 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Set;

/**
* After the context loads, do any needed migration tasks
*/
@Component
public class MigrationTasksContextLoadedListener implements ApplicationListener<ContextRefreshedEvent> {
private static boolean attrBundleMigrated = false;

@Autowired
private AttributeBundleRepository attributeBundleRepository;

@Autowired
private EntityDescriptorRepository entityDescriptorRepository;

Expand All @@ -36,8 +46,14 @@ public class MigrationTasksContextLoadedListener implements ApplicationListener<
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
doshibui_1740_migration(); // do first
if (!attrBundleMigrated) {
doAttributeBundleMigration();
}
}

/**
* This was a long time ago - don't think we need this anymore...
*/
@Transactional
void doshibui_1740_migration() {
groupService.ensureAdminGroupExists(); // do first
Expand All @@ -62,4 +78,23 @@ void doshibui_1740_migration() {
}
});
}

@Transactional
void doAttributeBundleMigration() {
attributeBundleRepository.migrateData();
attributeBundleRepository.findAll().forEach(attrBundleEntry -> {
try {
Set<String> names = new HashSet<String>();
attrBundleEntry.getAttributeNames().forEach(value -> {
names.add(BundleableAttributeType.values()[Integer.parseInt(value)].label());
});
attrBundleEntry.setAttributeNames(names);
attributeBundleRepository.save(attrBundleEntry);
}
catch (NumberFormatException ignore) {
}
});
attrBundleMigrated = true;
attributeBundleRepository.clearMigratedValues();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.internet2.tier.shibboleth.admin.ui.controller;

import edu.internet2.tier.shibboleth.admin.ui.configuration.CustomPropertiesConfiguration;
import edu.internet2.tier.shibboleth.admin.ui.domain.AttributeBundle;
import edu.internet2.tier.shibboleth.admin.ui.exception.ObjectIdExistsException;
import edu.internet2.tier.shibboleth.admin.ui.exception.PersistentEntityNotFound;
Expand All @@ -21,13 +22,19 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/custom/entity/bundles")
@Slf4j
@Tags(value = {@Tag(name = "bundles")})
@Tags(value = { @Tag(name = "bundles") })
public class AttributeBundleController {
@Autowired AttributeBundleService attributeBundleService;

@Autowired CustomPropertiesConfiguration customPropertiesConfiguration;

@Secured("ROLE_ADMIN")
@PostMapping
@Transactional
Expand Down Expand Up @@ -63,4 +70,37 @@ public ResponseEntity<?> update(@RequestBody AttributeBundle bundle) throws Pers
AttributeBundle result = attributeBundleService.updateBundle(bundle);
return ResponseEntity.ok(result);
}

@GetMapping("/bundle_schema")
public ResponseEntity<?> getBundleSchema() {
HashMap<String, Object> result = new HashMap<>();
result.put("type", "object");
result.put("required", new String[] { "name" });
HashMap<String, Object> props = new HashMap<>();
HashMap<String, Object> nameMap = new HashMap<>();
nameMap.put("type", "string");
nameMap.put("title", "label.bundle-name");
nameMap.put("description", "tooltip.bundle-name");
nameMap.put("minLength", 1);
nameMap.put("maxLength", 255);
props.put("name", nameMap);
HashMap<String, Object> attributesMap = new HashMap<>();
attributesMap.put("type", "array");
attributesMap.put("title", "label.attributes");
attributesMap.put("description", "Attribute table - select the attributes you want to bundle (default unchecked)");
attributesMap.put("uniqueItems", true);
HashMap<String, Object> itemsMap = new HashMap<>();
itemsMap.put("type", "string");
itemsMap.put("enum", getAttributeNames());
attributesMap.put("items", itemsMap);
props.put("attributeNames", attributesMap);
result.put("properties", props);
return ResponseEntity.ok(result);
}

private List<String> getAttributeNames() {
List<String> resultNames = new ArrayList<>();
customPropertiesConfiguration.getAttributes().forEach(map -> resultNames.add(map.get("name")));
return resultNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
@Entity(name = "attribute_bundle_definition")
@Data
public class AttributeBundle {
@Column(nullable = false)
@Column(name="attribute_names", nullable = false)
@ElementCollection
Set<BundleableAttributeType> attributes = new HashSet<>();
Set<String> attributeNames = new HashSet<>();

@Column(name = "name", nullable = true)
String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import edu.internet2.tier.shibboleth.admin.ui.domain.AttributeBundle;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
Expand All @@ -15,4 +18,14 @@ public interface AttributeBundleRepository extends JpaRepository<AttributeBundle
Optional<AttributeBundle> findByResourceId(String resourceId);

AttributeBundle save(AttributeBundle target);

@Query(value = "INSERT INTO attribute_bundle_definition_attribute_names SELECT * FROM attribute_bundle_definition_attributes", nativeQuery = true)
@Modifying
@Transactional
void migrateData();

@Query(value = "DELETE FROM attribute_bundle_definition_attributes", nativeQuery = true)
@Modifying
@Transactional
void clearMigratedValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public AttributeBundle updateBundle(AttributeBundle bundle) throws PersistentEnt
}
AttributeBundle bundleToUpdate = dbBundle.get();
bundleToUpdate.setName(bundle.getName());
bundleToUpdate.setAttributes(bundle.getAttributes());
bundleToUpdate.setAttributeNames(bundle.getAttributeNames());
return attributeBundleRepository.save(bundleToUpdate);
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=shibui
group=edu.internet2.tier.shibboleth.admin.ui
version=2.0.2
version=2.1.0-SNAPSHOT

### library versions ###
## As of 2-23-23
Expand Down
Loading

0 comments on commit bfc6d4a

Please sign in to comment.