/* ****************************************************************************
*
* putIndividualContextEntityAttributes - 
*/
std::string putIndividualContextEntityAttributes(ConnectionInfo* ciP, int components, std::vector<std::string>& compV, ParseData* parseDataP)
{
  std::string                   answer;
  std::string                   entityId = compV[2];
  UpdateContextElementResponse  response;

  LM_T(LmtConvenience, ("CONVENIENCE: got a 'PUT' request for entityId '%s'", entityId.c_str()));
  ciP->httpStatusCode = mapPutIndividualContextEntityAttributes(entityId, &parseDataP->ucer.res, &response, ciP);
  answer = response.render(IndividualContextEntityAttributes, ciP->outFormat, "");
  response.release();

  return answer;
}
/* ****************************************************************************
*
* putIndividualContextEntity -
*
* Corresponding Standard Operation: UpdateContext/UPDATE
*
* PUT /v1/contextEntities/{entityId::id}
* PUT /ngsi10/contextEntities/{entityId::id}
*
* Payload In:  UpdateContextElementRequest
* Payload Out: UpdateContextElementResponse
*
* URI parameters:
*   - attributesFormat=object
*   - entity::type=TYPE
*   - note that '!exist=entity::type' and 'exist=entity::type' are not supported by convenience operations
*     that use the standard operation UpdateContext as there is no restriction within UpdateContext.
*
* 01. Take care of URI params
* 02. Fill in UpdateContextRequest from UpdateContextElementRequest
* 03. Call postUpdateContext standard service routine
* 04. Translate UpdateContextResponse to UpdateContextElementResponse
* 05. Cleanup and return result
*/
std::string putIndividualContextEntity
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  std::string                   answer;
  std::string                   entityId = compV[2];
  UpdateContextElementResponse  response;
  std::string                   entityType;

  bool asJsonObject = (ciP->uriParam[URI_PARAM_ATTRIBUTE_FORMAT] == "object" && ciP->outMimeType == JSON);

  // 01. Take care of URI params
  entityType = ciP->uriParam[URI_PARAM_ENTITY_TYPE];


  // 02. Fill in UpdateContextRequest from UpdateContextElementRequest and entityId
  parseDataP->upcr.res.fill(&parseDataP->ucer.res, entityId, entityType);

  // And, set the UpdateActionType to UPDATE
  parseDataP->upcr.res.updateActionType = ActionTypeUpdate;


  // 03. Call postUpdateContext standard service routine
  postUpdateContext(ciP, components, compV, parseDataP);


  // 04. Translate UpdateContextResponse to UpdateContextElementResponse
  response.fill(&parseDataP->upcrs.res);


  // 05. Cleanup and return result
  TIMED_RENDER(answer = response.toJsonV1(asJsonObject, IndividualContextEntity));


  response.release();
  parseDataP->upcr.res.release();

  return answer;
}
/* ****************************************************************************
*
* putAllEntitiesWithTypeAndId - 
*/
extern std::string putAllEntitiesWithTypeAndId
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  std::string                   enType = compV[3];
  std::string                   enId   = compV[5];
  std::string                   answer;
  UpdateContextElementResponse  response;

  LM_T(LmtConvenience, ("CONVENIENCE: got a 'PUT' request for entityId '%s', type '%s'", enId.c_str(), enType.c_str()));
  ciP->httpStatusCode = mapPutIndividualContextEntity(enId, enType, &parseDataP->ucer.res, &response, ciP);
  answer = response.render(ciP, IndividualContextEntity, "");
  response.release();

  return answer;
}
/* ****************************************************************************
*
* check - 
*
*
* FIXME P3: once (if ever) AttributeDomainName::check stops to always return "OK", put back this piece of code 
*           in its place:
-
*   else if ((res = attributeDomainName.check(AppendContextElement, format, indent, predetectedError, counter)) != "OK")
*   {
*     response.errorCode.code         = SccBadRequest;
*     response.errorCode.reasonPhrase = res;
*   }
*
*/
std::string UpdateContextElementRequest::check(RequestType requestType, Format format, std::string indent, std::string predetectedError, int counter)
{
   UpdateContextElementResponse  response;
   std::string                   res;

   if (predetectedError != "")
   {
     response.errorCode.code         = SccBadRequest;
     response.errorCode.reasonPhrase = predetectedError;
   }
   else if ((res = contextAttributeVector.check(UpdateContextElement, format, indent, predetectedError, counter)) != "OK")
   {
     response.errorCode.code         = SccBadRequest;
     response.errorCode.reasonPhrase = res;
   }
   else
     return "OK";
   
   return response.render(requestType, format, indent);
}
/* ****************************************************************************
*
* putAllEntitiesWithTypeAndId - 
*
* PUT /v1/contextEntities/type/{entity::type}/id/{entity::id}
*
* Payload In:  UpdateContextElementRequest
* Payload Out: UpdateContextElementResponse
*
* URI parameters:
*   - attributesFormat=object
*   - entity::type=TYPE (must coincide with type in URL-path)
*   - !exist=entity::type  (if set - error -- entity::type cannot be empty)
*   - exist=entity::type   (not supported - ok if present, ok if not present ...)
*
* 01. Get values from URL (entityId::type, exist, !exist)
* 02. Check validity of URI params
* 03. Fill in UpdateContextRequest
* 04. Call Standard Operation
* 05. Fill in response from UpdateContextResponse
* 06. Cleanup and return result
*/
extern std::string putAllEntitiesWithTypeAndId
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  std::string                   entityType            = compV[3];
  std::string                   entityId              = compV[5];
  EntityTypeInfo                typeInfo              = EntityTypeEmptyOrNotEmpty;
  std::string                   typeNameFromUriParam  = ciP->uriParam[URI_PARAM_ENTITY_TYPE];
  std::string                   answer;
  UpdateContextElementResponse  response;

  // FIXME P1: AttributeDomainName skipped
  // FIXME P1: domainMetadataVector skipped


  // 01. Get values from URL (entityId::type, esist, !exist)
  if (ciP->uriParam[URI_PARAM_NOT_EXIST] == URI_PARAM_ENTITY_TYPE)
  {
    typeInfo = EntityTypeEmpty;
  }
  else if (ciP->uriParam[URI_PARAM_EXIST] == URI_PARAM_ENTITY_TYPE)
  {
    typeInfo = EntityTypeNotEmpty;
  }


  // 02. Check validity of URI params
  if (typeInfo == EntityTypeEmpty)
  {
    alarmMgr.badInput(clientIp, "entity::type cannot be empty for this request");
    response.errorCode.fill(SccBadRequest, "entity::type cannot be empty for this request");
    TIMED_RENDER(answer = response.render(ciP, AllEntitiesWithTypeAndId, ""));
    return answer;
  }
  else if ((typeNameFromUriParam != entityType) && (typeNameFromUriParam != ""))
  {
    alarmMgr.badInput(clientIp, "non-matching entity::types in URL");
    response.errorCode.fill(SccBadRequest, "non-matching entity::types in URL");
    TIMED_RENDER(answer = response.render(ciP, AllEntitiesWithTypeAndId, ""));
    return answer;
  }


  // 03. Fill in UpdateContextRequest
  parseDataP->upcr.res.fill(&parseDataP->ucer.res, entityId, entityType);


  // 04. Call Standard Operation
 postUpdateContext(ciP, components, compV, parseDataP);


  // 05. Fill in response from UpdateContextResponse
  response.fill(&parseDataP->upcrs.res);


  // 06. Cleanup and return result
  TIMED_RENDER(answer = response.render(ciP, IndividualContextEntity, ""));

  parseDataP->upcr.res.release();
  response.release();

  return answer;
}