/**
   * Update catalog service
   *
   * @param param Catalog Service update parameters
   * @param id the URN the catalog service
   * @prereq none
   * @brief Update Catalog Service
   * @return No data returned in response body
   */
  @PUT
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Path("/{id}")
  @CheckPermission(
      roles = {Role.TENANT_ADMIN},
      acls = {ACL.OWN})
  public CatalogServiceRestRep updateCatalogService(
      @PathParam("id") URI id, CatalogServiceUpdateParam param) {
    CatalogService catalogService = getCatalogServiceById(id, true);
    List<CatalogServiceField> catalogServiceFields =
        catalogServiceManager.getCatalogServiceFields(id);

    StorageOSUser user = getUserFromContext();
    CatalogCategory parentCatalogCategory =
        catalogCategoryManager.getCatalogCategoryById(param.getCatalogCategory());
    verifyAuthorizedInTenantOrg(uri(parentCatalogCategory.getTenant()), user);

    validateParam(param, catalogService);

    updateObject(catalogService, param, parentCatalogCategory);
    List<CatalogServiceField> updatedCatalogServiceFields =
        updateObjectList(catalogService, catalogServiceFields, param.getCatalogServiceFields());

    catalogServiceManager.updateCatalogService(catalogService, updatedCatalogServiceFields);
    auditOpSuccess(OperationTypeEnum.UPDATE_CATALOG_SERVICE, catalogService.auditParameters());

    // Refresh Objects
    catalogService = catalogServiceManager.getCatalogServiceById(catalogService.getId());
    catalogServiceFields = catalogServiceManager.getCatalogServiceFields(catalogService.getId());
    ServiceDescriptor serviceDescriptor = getServiceDescriptor(catalogService);

    return map(catalogService, serviceDescriptor, catalogServiceFields);
  }
  @PUT
  @Path("/{id}/acl")
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @CheckPermission(
      roles = {Role.SECURITY_ADMIN, Role.TENANT_ADMIN},
      acls = {ACL.OWN},
      block_proxies = true)
  public ACLAssignments updateRoleAssignments(
      @PathParam("id") URI id, ACLAssignmentChanges changes) {
    CatalogService catalogService = catalogServiceManager.getCatalogServiceById(id);
    CatalogCategory parentCatalogCategory =
        catalogCategoryManager.getCatalogCategoryById(
            catalogService.getCatalogCategoryId().getURI());
    URI tenantId = uri(parentCatalogCategory.getTenant());

    _permissionsHelper.updateACLs(catalogService, changes, new CatalogACLInputFilter(tenantId));

    catalogServiceManager.updateCatalogService(catalogService, null);
    ;

    auditOpSuccess(
        OperationTypeEnum.MODIFY_CATALOG_SERVICE_ACL,
        catalogService.getId().toString(),
        catalogService.getLabel(),
        changes);

    catalogConfigUtils.notifyCatalogAclChange();

    return getRoleAssignmentsResponse(id);
  }
  /**
   * Creates a new catalog service
   *
   * @param createParam the parameter to create a new catalog service
   * @prereq none
   * @brief Create Catalog Service
   * @return none
   */
  @POST
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @CheckPermission(
      roles = {Role.TENANT_ADMIN},
      acls = {ACL.OWN})
  @Path("")
  public CatalogServiceRestRep createCatalogService(CatalogServiceCreateParam createParam) {

    StorageOSUser user = getUserFromContext();
    CatalogCategory parentCatalogCategory =
        catalogCategoryManager.getCatalogCategoryById(createParam.getCatalogCategory());
    verifyAuthorizedInTenantOrg(uri(parentCatalogCategory.getTenant()), user);

    validateParam(createParam, null);

    CatalogService catalogService = createNewObject(createParam, parentCatalogCategory);
    List<CatalogServiceField> catalogServiceFields =
        createNewObjectList(catalogService, createParam.getCatalogServiceFields());
    catalogServiceManager.createCatalogService(catalogService, catalogServiceFields);

    auditOpSuccess(OperationTypeEnum.CREATE_CATALOG_SERVICE, catalogService.auditParameters());

    // Refresh Objects
    catalogService = catalogServiceManager.getCatalogServiceById(catalogService.getId());
    catalogServiceFields = catalogServiceManager.getCatalogServiceFields(catalogService.getId());
    ServiceDescriptor serviceDescriptor = getServiceDescriptor(catalogService);

    return map(catalogService, serviceDescriptor, catalogServiceFields);
  }
 @Override
 protected URI getTenantOwner(URI id) {
   CatalogService catalogService = queryResource(id);
   CatalogCategory parentCatalogCategory =
       catalogCategoryManager.getCatalogCategoryById(
           catalogService.getCatalogCategoryId().getURI());
   return uri(parentCatalogCategory.getTenant());
 }
 private CatalogCategory createCategoryWithLabel(String label) {
   CatalogCategory model = new CatalogCategory();
   model.setLabel(label);
   model.setDescription("my desc");
   model.setImage("my image");
   model.setTitle("my title");
   return model;
 }
 public CatalogService createCatalogService(
     ServiceDef serviceDef, CatalogCategory parentCategory) {
   CatalogBuilder builder = new CatalogBuilder(client, serviceDescriptors);
   NamedURI namedUri = new NamedURI(parentCategory.getId(), parentCategory.getLabel());
   CatalogService newService = builder.createService(serviceDef, namedUri);
   newService.setSortedIndex(null);
   client.save(newService);
   return newService;
 }
  public void deleteCatalogService(CatalogService catalogService) {
    CatalogCategory parentCatalogCategory =
        catalogCategoryManager.getCatalogCategoryById(
            catalogService.getCatalogCategoryId().getURI());
    URI tenantId = uri(parentCatalogCategory.getTenant());

    if (isServiceUsedForOrders(tenantId, catalogService)) {
      URI deletedCategoryURI = URI.create(CatalogCategory.DELETED_CATEGORY);
      String deletedCategoryLabel = CatalogCategory.DELETED_CATEGORY;
      catalogService.setCatalogCategoryId(new NamedURI(deletedCategoryURI, deletedCategoryLabel));
      client.save(catalogService);
    } else {
      List<CatalogServiceField> serviceFields = getCatalogServiceFields(catalogService.getId());
      log.debug(String.format("Deleting Service Fields: %s", catalogService.getTitle()));
      client.delete(serviceFields);

      log.info(String.format("Deleting Service: %s", catalogService.getTitle()));
      client.delete(catalogService);
    }
  }
  @Test
  public void testReparent() throws Exception {
    _logger.info("Starting reparent CatalogService test");

    ModelClient modelClient = getModelClient();

    CatalogCategory parent1 = createCategoryWithLabel("Parent 1");
    modelClient.save(parent1);

    CatalogCategory parent2 = createCategoryWithLabel("Parent 2");
    modelClient.save(parent2);

    CatalogService service = createWithLabel("Service");
    setParent(parent1, service);
    modelClient.save(service);

    List<CatalogService> parent1Children =
        modelClient.catalogServices().findByCatalogCategory(parent1.getId());
    Assert.assertEquals(1, parent1Children.size());
    Assert.assertEquals(service.getId(), parent1Children.get(0).getId());

    List<CatalogService> parent2Children =
        modelClient.catalogServices().findByCatalogCategory(parent2.getId());
    Assert.assertEquals(0, parent2Children.size());

    setParent(parent2, service);
    modelClient.save(service);

    parent1Children = modelClient.catalogServices().findByCatalogCategory(parent1.getId());
    Assert.assertEquals(0, parent1Children.size());

    parent2Children = modelClient.catalogServices().findByCatalogCategory(parent2.getId());
    Assert.assertEquals(1, parent2Children.size());
    Assert.assertEquals(service.getId(), parent2Children.get(0).getId());
  }
  @Test
  public void testFindByCatalogCategory() {

    _logger.info("Starting FindByCatalogCategory test");

    ModelClient modelClient = getModelClient();

    CatalogCategory root = createCategoryWithLabel("rooty");
    modelClient.save(root);

    CatalogService s1 = createWithLabel("s1");
    s1.setCatalogCategoryId(new NamedURI(root.getId(), root.getLabel()));
    modelClient.save(s1);

    CatalogCategory c1 = createCategoryWithLabel("asdf");
    c1.setCatalogCategoryId(new NamedURI(root.getId(), root.getLabel()));
    modelClient.save(c1);

    CatalogService s2 = createWithLabel("s2");
    s2.setCatalogCategoryId(new NamedURI(c1.getId(), c1.getLabel()));
    modelClient.save(s2);

    CatalogService s3 = createWithLabel("s3");
    s3.setCatalogCategoryId(new NamedURI(c1.getId(), c1.getLabel()));
    modelClient.save(s3);

    CatalogCategory c2 = createCategoryWithLabel("asdf2");
    c2.setCatalogCategoryId(new NamedURI(root.getId(), root.getLabel()));
    modelClient.save(c2);

    CatalogService s4 = createWithLabel("s4");
    s4.setCatalogCategoryId(new NamedURI(c2.getId(), c2.getLabel()));
    modelClient.save(s4);

    CatalogService s5 = createWithLabel("s5");
    s5.setCatalogCategoryId(new NamedURI(c2.getId(), c2.getLabel()));
    modelClient.save(s5);

    CatalogService s6 = createWithLabel("s6");
    s6.setCatalogCategoryId(new NamedURI(c2.getId(), c2.getLabel()));
    modelClient.save(s6);

    List<CatalogService> results =
        modelClient.catalogServices().findByCatalogCategory(root.getId());
    Assert.assertNotNull(results);
    Assert.assertEquals(1, results.size());

    results = modelClient.catalogServices().findByCatalogCategory(c1.getId());
    Assert.assertNotNull(results);
    Assert.assertEquals(2, results.size());

    results = modelClient.catalogServices().findByCatalogCategory(c2.getId());
    Assert.assertNotNull(results);
    Assert.assertEquals(3, results.size());
  }
 private void setParent(CatalogCategory parent, CatalogService child) {
   child.setCatalogCategoryId(new NamedURI(parent.getId(), parent.getLabel()));
 }