package com.l7tech.external.assertions.gatewaygraphql.server.resolver.mutation;

import com.l7tech.external.assertions.gatewaygraphql.EntityTypeInfo;
import com.l7tech.external.assertions.gatewaygraphql.server.data.EntityDTO;
import com.l7tech.external.assertions.gatewaygraphql.server.input.EntityInput;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityManager;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.imp.PersistentEntityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.PlatformTransactionManager;

public abstract class EntityManagerAwareGraphQLMutationResolver<ET extends PersistentEntity, HT extends EntityHeader, EM extends EntityManager<ET, HT>, EO extends EntityDTO<ET>, EI extends EntityInput>
   extends EntityAwareGraphQLMutationResolver<ET, EO, EI> {
   protected final EM entityManager;

   protected EntityManagerAwareGraphQLMutationResolver(EntityTypeInfo var1, PlatformTransactionManager var2, EM var3) {
      super(var1, var2);
      this.entityManager = (EM)var3;
   }

   public final ET findByName(String var1) throws FindException {
      return (ET)this.entityManager.findByUniqueName(var1);
   }

   public final ET findByGoid(Goid var1) throws FindException {
      return (ET)this.entityManager.findByPrimaryKey(var1);
   }

   @Override
   protected EO doCreate(EI var1) throws ObjectModelException {
      PersistentEntity var2 = this.toEntity((EI)var1);

      try {
         this.setEntity((EI)var1, (ET)var2);
         if (StringUtils.isNotEmpty(var1.getGoid())) {
            var2.setGoid(Goid.parseGoid(var1.getGoid()));
         }
      } catch (ObjectModelException var3) {
         throw b(var3);
      }

      try {
         if (!Goid.DEFAULT_GOID.equals(var2.getGoid())) {
            PersistentEntityUtil.preserveId(var2);
         }
      } catch (ObjectModelException var4) {
         throw b(var4);
      }

      this.entityManager.save(var2);
      return this.toEntityDTO((ET)var2);
   }

   protected EO doUpdate(EI var1, ET var2) throws ObjectModelException {
      label16: {
         try {
            this.checkEntityProtected(var1.getExecutionContext(), var2);
            this.checkEntityVersion(var1.getExecutionContext(), var1, var2);
            if (var1.isFullInput()) {
               this.resetEntitySelectively((EI)var1, (ET)var2);
               this.setEntity((EI)var1, (ET)var2);
               break label16;
            }
         } catch (ObjectModelException var3) {
            throw b(var3);
         }

         this.resetEntityConditionally((EI)var1, (ET)var2);
         this.setEntityConditionally((EI)var1, (ET)var2);
      }

      this.entityManager.update(var2);
      return this.toEntityDTO((ET)var2);
   }

   protected EO doDelete(EI var1, ET var2) throws ObjectModelException {
      this.checkEntityProtected(var1.getExecutionContext(), var2);
      this.entityManager.delete(var2);
      return this.toEntityDTO((ET)var2);
   }

   private static ObjectModelException b(ObjectModelException var0) {
      return var0;
   }
}
