in app/services/migrationcomponents/LinkVStoPL.scala [59:131]
override def onPush(): Unit = {
val vsProject = grab(in)
val maybeProjectStatus = getVSProjectStatus(vsProject)
val maybeProductionOffice = getVSProductionOffice(vsProject)
//see if we have an existing project with that id
val projectLookupFut = vsProject.getMetaOptional("collectionId").flatMap(_.headOption) match {
case Some(vsid)=>
ProjectEntry.lookupByVidispineId(vsid).map({
case Failure(err)=>throw err
case Success(resultList)=>resultList.headOption //should only be 1 or 0 results on this query
})
case None=>
Future.failed(new RuntimeException("Incoming VS project had no collection ID!"))
}
//build the new project out of the old one. if we have an existing project, then only update the "new" fields;
//otherwise, create a whole new record
val updatedFut = projectLookupFut.flatMap({
case None=>
logger.debug(s"No pre-existing project for ${vsProject.getSingle("collectionId")}, creating new")
Future.sequence(Seq(
ProjectHelper.findWorkingGroup(vsProject),
ProjectHelper.findCommission(vsProject)
)).map(results=> {
val maybeWg = results.head.asInstanceOf[Option[PlutoWorkingGroup]]
val maybeCommission = results(1).asInstanceOf[Option[PlutoCommission]]
ProjectEntry(
None,
createProjectTypeId,
vsProject.getSingle("collectionId"),
projectTitle = vsProject.title.getOrElse("(no title)"),
created = vsProject.created.getOrElse(Timestamp.from(Instant.now)),
updated = vsProject.updated.getOrElse(Timestamp.from(Instant.now)),
user = vsProject.owner_id_list.map(vsUserCache.lookup).collect({case Some(user)=>user}).mkString("|"),
workingGroupId = maybeWg.flatMap(_.id),
commissionId = maybeCommission.flatMap(_.id),
deletable = Some(vsProject.isDeletable),
deep_archive = Some(vsProject.isDeepArchive),
sensitive = Some(vsProject.isSensitive),
status = maybeProjectStatus.getOrElse(EntryStatus.Completed),
productionOffice = maybeProductionOffice.getOrElse(ProductionOffice.UK),
None,
Some(false)
)
})
case Some(existingProject)=>
logger.debug(s"Updating existing project ${existingProject.id} (${existingProject.vidispineProjectId}")
/*
if we have an entry already in the database, that's from Projectlocker.
in that case, we must apply the fields that did not exist in Projectlocker, and update the title
because an updated title in Pluto would not reflect in PL
*/
Future(existingProject.copy(
projectTitle = vsProject.title.getOrElse(existingProject.projectTitle),
updated = vsProject.updated.getOrElse(Timestamp.from(Instant.now())),
status = maybeProjectStatus.getOrElse(EntryStatus.Completed),
productionOffice = maybeProductionOffice.getOrElse(ProductionOffice.UK)
))
})
updatedFut.onComplete({
case Success(newProjectEntry)=>
logger.info(s"Successfully update project ${vsProject.getSingle("collectionId")} to ${newProjectEntry}")
successCb.invoke(newProjectEntry)
case Failure(err)=>
logger.error(s"Could not perform project update on ${vsProject.getSingle("collectionId")}: ", err)
errCb.invoke(err)
})
}