package edu.psu.swe.scim.server.rest;

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

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import edu.psu.swe.scim.server.provider.Provider;
import edu.psu.swe.scim.server.provider.ProviderRegistry;
import edu.psu.swe.scim.server.schema.Registry;
import edu.psu.swe.scim.spec.protocol.BulkResource;
import edu.psu.swe.scim.spec.protocol.data.BulkOperation;
import edu.psu.swe.scim.spec.protocol.data.BulkOperation.Status;
import edu.psu.swe.scim.spec.protocol.data.BulkRequest;
import edu.psu.swe.scim.spec.protocol.data.BulkResponse;
import edu.psu.swe.scim.spec.resources.ScimResource;
import edu.psu.swe.scim.spec.schema.ErrorResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Stateless
public class BulkResourceImpl implements BulkResource {
  private static final Status METHOD_NOT_ALLOWED_STATUS = new Status();
  private static final Status OKAY_STATUS = new Status();
  private static final String METHOD_NOT_ALLOWED = "405";
  private static final String OKAY = "200";

  static {
    METHOD_NOT_ALLOWED_STATUS.setCode(METHOD_NOT_ALLOWED);
    OKAY_STATUS.setCode(OKAY);
  }

  @Inject
  Registry registry;

  @Inject
  ProviderRegistry providerRegistry;

  @Override
  public Response doBulk(BulkRequest request, UriInfo uriInfo) {
    BulkResponse response;
//    int errorCount = 0;
    List<BulkOperation> completedOperations = new ArrayList<>();

    for (BulkOperation bulkOperation : request.getOperations()) {
      BulkOperation operationResult = new BulkOperation();
      ScimResource scimResource = bulkOperation.getData();
      @SuppressWarnings("unchecked")
      Class<ScimResource> scimResourceClass = (Class<ScimResource>) scimResource.getClass();
      Provider<ScimResource> provider = providerRegistry.getProvider(scimResourceClass);

      operationResult.setStatus(OKAY_STATUS);

      switch (bulkOperation.getMethod()) {
      case POST: {
        log.debug("POST: {}", scimResource);

        ScimResource newResource = provider.create(scimResource);

        operationResult.setLocation(uriInfo.getBaseUriBuilder().path(bulkOperation.getPath()).path(newResource.getId()).toString());
      } break;

      case DELETE: {
        log.debug("DELETE: {}", scimResource);

        String scimResourceId = scimResource.getId();

        provider.delete(scimResourceId);
      } break;

      case PATCH:
      case PUT: {
        log.debug("PUT/PATCH: {}", scimResource);

        ScimResource newResource = provider.update(scimResource);

        operationResult.setLocation(uriInfo.getBaseUriBuilder().path(bulkOperation.getPath()).path(newResource.getId()).toString());
      } break;

      default: {
        ErrorResponse error = new ErrorResponse();

        error.setStatus(METHOD_NOT_ALLOWED);
        error.setDetail("Method not allowed: " + bulkOperation.getMethod());
        operationResult.setResponse(error);
        operationResult.setStatus(METHOD_NOT_ALLOWED_STATUS);
      } break;
      }
      completedOperations.add(operationResult);
    }
    response = new BulkResponse();
    response.setOperations(completedOperations);
    response.setStatus(OKAY);

    return Response.ok(response).build();
  }
}

@@ -2101,33 +2101,32 @@
 mResource);%0A
-%0A
         
 operationRes
@@ -2117,36 +2117,144 @@
     
-operationResult.setLocation(
+String bulkOperationPath = bulkOperation.getPath();%0A        String newResourceId = newResource.getId();%0A        String newResourceUri = 
 uriI
@@ -2291,34 +2291,28 @@
 ulkOperation
-.get
 Path
-()
 ).path(newRe
@@ -2313,32 +2313,34 @@
 (newResource
-.getId()
+Id).build(
 ).toString()
@@ -2331,32 +2331,85 @@
 ild().toString()
+;%0A%0A        operationResult.setLocation(newResourceUri
 );%0A      %7D break

Fix URI building