      p_fileType varchar(45)
 	)
     MODIFIES SQL DATA
-    COMMENT 'Inserts or updates info about a file attachmet for a data collec'
+    COMMENT 'Inserts or updates info about a file attachmet for a data collection. Returns: The PK value in p_id.'
 BEGIN
 	IF p_id IS NOT NULL OR p_dataCollectionId IS NOT NULL THEN
 
-      INSERT INTO DataCollectionFileAttachment (dataCollectionFileAttachmentId, dataCollectionId, fileFullPath, fileType)
+      INSERT INTO DataCollectionFileAttachment (dataCollectionFileAttachmentId, dataCollectionId, fileFullPath, fileType) 
         VALUES (p_id, p_dataCollectionId, p_fileFullPath, p_fileType)
 	    ON DUPLICATE KEY UPDATE
 		  dataCollectionId = IFNULL(p_dataCollectionId, dataCollectionId),
           fileFullPath = IFNULL(p_fileFullPath, fileFullPath),
           fileType = IFNULL(p_fileType, fileType);
 
-	  IF p_id IS NULL THEN
+	  IF p_id IS NULL THEN 
 		  SET p_id = LAST_INSERT_ID();
       END IF;
     ELSE
       SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=1644, MESSAGE_TEXT='Mandatory argument is NULL: p_id OR p_dataCollectionId must be non-NULL.';
-    END IF;
+    END IF;      
 END ;;
 DELIMITER ;
 /*!50003 SET sql_mode              = @saved_sql_mode */ ;

  p_driftFactor float
)
    MODIFIES SQL DATA
    COMMENT 'Inserts into or updates a row in the image quality indicators ta'
BEGIN
  DECLARE iqiId int(11) unsigned DEFAULT NULL;
  IF (p_dataCollectionId IS NOT NULL AND p_imageNumber IS NOT NULL) THEN
    SELECT MAX(imageQualityIndicatorsId) INTO iqiId FROM ImageQualityIndicators WHERE dataCollectionId = p_dataCollectionId AND imageNumber = p_imageNumber;
    IF iqiId IS NULL THEN
        INSERT INTO ImageQualityIndicators (
          dataCollectionId, imageNumber, spotTotal, goodBraggCandidates,  
	      method1Res, method2Res, totalIntegratedSignal, dozor_score, driftFactor) 

/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `upsert_proposal` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = '' */ ;
DELIMITER ;;
CREATE PROCEDURE `upsert_proposal`(
	 INOUT p_id int(11) unsigned,
	 p_personId int(11) unsigned,
	 p_title varchar(200),
	 p_proposalCode varchar(45),
	 p_proposalNumber int(11) unsigned,
	 p_proposalType varchar(2),
   p_externalPkUUID varchar(32)
 )
    MODIFIES SQL DATA
    COMMENT 'Inserts or updates info about a proposal (p_id).\nMandatory columns:\nFor insert: p_personId AND p_proposalCode AND p_proposalNumber\nFor update: p_id \nReturns: Record ID in p_id.'
BEGIN

IF p_id IS NOT NULL OR (p_personId IS NOT NULL AND p_proposalCode IS NOT NULL AND p_proposalNumber IS NOT NULL)  THEN

  INSERT INTO Proposal (proposalId, personId, title, proposalCode, proposalNumber, proposalType, externalId)
    VALUES (p_id, p_personId, p_title, p_proposalCode, p_proposalNumber, p_proposalType, unhex(p_externalPkUUID))
    ON DUPLICATE KEY UPDATE
      personId = IFNULL(p_personId, personId),
      title = IFNULL(p_title, title),
      proposalCode = IFNULL(p_proposalCode, proposalCode),
      proposalNumber = IFNULL(p_proposalNumber, proposalNumber),
      proposalType = IFNULL(p_proposalType, proposalType),
      externalId = IFNULL(unhex(p_externalPkUUID), externalId);

  IF p_id IS NULL THEN
    SET p_id = LAST_INSERT_ID();
  END IF;
ELSE
  SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=1644, MESSAGE_TEXT='Mandatory argument(s) are NULL: p_id OR (p_personId AND p_proposalCode AND p_proposalNumber) must be non-NULL.';
END IF;

END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `upsert_proposal_has_person` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
