Skip to content

Commit

Permalink
check point - working on history tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed May 23, 2024
1 parent d0a94d2 commit 3d8c08c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import jakarta.persistence.*;

import org.mbari.oni.domain.ConceptNameTypes;
import org.mbari.oni.jpa.IPersistentObject;
import org.mbari.oni.jpa.KeyNullifier;
import org.mbari.oni.jpa.TransactionLogger;
Expand Down Expand Up @@ -99,11 +100,17 @@ public class ConceptNameEntity implements Serializable, IPersistentObject {
public ConceptNameEntity() {
}

public ConceptNameEntity(String name) {
this(name, ConceptNameTypes.PRIMARY.getType())
}

public ConceptNameEntity(String name, String nameType) {
this.name = name;
this.nameType = nameType;
}



@Override
public boolean equals(Object obj) {
if (obj == null) {
Expand Down
4 changes: 3 additions & 1 deletion oni/src/main/scala/org/mbari/oni/OniException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package org.mbari.oni

sealed trait OniException extends Throwable

case class ConceptNameNotFound(name: String) extends Exception(s"Concept name $name was not found") with OniException
case class AccessDenied(user: String) extends Exception(s"I'm sorry `$user`, I can not let you do that.") with OniException
case class ConceptNameAlreadyExists(name: String) extends Exception(s"Concept name `$name` already exists") with OniException
case class ConceptNameNotFound(name: String) extends Exception(s"Concept name `$name` was not found") with OniException
case object MissingRootConcept extends Exception("Root concept is missing") with OniException
case object RootAlreadyExists extends Exception("Root concept already exists") with OniException
4 changes: 2 additions & 2 deletions oni/src/main/scala/org/mbari/oni/domain/ConceptCreate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

package org.mbari.oni.domain

case class ConceptCreate(parentName: String,
name: String,
case class ConceptCreate(name: String,
parentName: Option[String],
rankLevel: Option[String] = None,
rankName: Option[String] = None,
aphiaId: Option[Long] = None,
Expand Down
3 changes: 2 additions & 1 deletion oni/src/main/scala/org/mbari/oni/etc/sdk/Eithers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ object Eithers {
def toEither: Either[Throwable, B] =
if opt.isPresent then Right(opt.get)
else Left(emptyOptionalError)



}
48 changes: 45 additions & 3 deletions oni/src/main/scala/org/mbari/oni/services/ConceptService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
package org.mbari.oni.services

import jakarta.persistence.{EntityManager, EntityManagerFactory}
import org.mbari.oni.{ConceptNameNotFound, MissingRootConcept, RootAlreadyExists}
import org.mbari.oni.{AccessDenied, ConceptNameNotFound, MissingRootConcept, RootAlreadyExists}
import org.mbari.oni.domain.{ConceptCreate, ConceptDelete, ConceptMetadata, ConceptUpdate, RawConcept, SimpleConcept}
import org.mbari.oni.jpa.entities.ConceptEntity
import org.mbari.oni.jpa.entities.{ConceptEntity, ConceptNameEntity, HistoryEntity, HistoryEntityFactory, UserAccountEntity}
import org.mbari.oni.jpa.EntityManagerFactories.*
import org.mbari.oni.jpa.repositories.ConceptRepository
import org.mbari.oni.etc.jdk.Loggers.given
Expand All @@ -22,6 +22,7 @@ class ConceptService(entityManagerFactory: EntityManagerFactory):

private val log = System.getLogger(getClass.getName)
private val historyService = HistoryService(entityManagerFactory)
private val userAccountService = UserAccountService(entityManagerFactory)

/**
* Inserts an entire tree of concepts in the database. Requires that the database is empty. This is an ACID
Expand Down Expand Up @@ -185,7 +186,48 @@ class ConceptService(entityManagerFactory: EntityManagerFactory):
handleByConceptName(name, fn2)

// Create
def create(conceptCreate: ConceptCreate): Either[Throwable, ConceptMetadata] = ???
def create(conceptCreate: ConceptCreate): Either[Throwable, ConceptMetadata] =

def buildInTxn(userEntity: UserAccountEntity, parent: ConceptEntity): ConceptEntity =

// build concept
val concept = new ConceptEntity()
conceptCreate.aphiaId.foreach(v => concept.setAphiaId(v.longValue()))
conceptCreate.rankLevel.foreach(v => concept.setRankLevel(v))
conceptCreate.rankName.foreach(v => concept.setRankName(v))
val conceptName = new ConceptNameEntity(conceptCreate.name)
concept.addConceptName(conceptName)
parent.addChildConcept(concept)

// build history
val history = HistoryEntityFactory.add(userEntity, parent)
parent.getConceptMetadata.addHistory(history)
concept

def txn(userEntity: UserAccountEntity): Either[Throwable, ConceptMetadata] =
entityManagerFactory.transaction(entityManager =>
val repo = new ConceptRepository(entityManager)
repo.findByName(conceptCreate.name).toScala match
case Some(_) => throw ConceptNameAlreadyExists(conceptCreate.name)
case None =>
val parent = conceptCreate.parentName match
case Some(parentName) =>
repo.findByName(parentName).toScala match
case Some(p) => p
case None => throw ConceptNameNotFound(parentName)
case None => null

val conceptEntity = buildInTxn(userEntity, parent)
ConceptMetadata.from(conceptEntity)
)

for
user <- userAccountService.findByUserName(conceptCreate.userName)
concept <- txn(user)
yield
concept


// Update
def update(conceptUpdate: ConceptUpdate): Either[Throwable, ConceptMetadata] = ???
// Delete
Expand Down

0 comments on commit 3d8c08c

Please sign in to comment.