package org.carlspring.strongbox.data.domain;

import java.util.Date;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orientechnologies.orient.core.exception.OValidationException;
import com.orientechnologies.orient.core.hook.ORecordHookAbstract;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * @author Sergey Bespalov
 *
 */
public class GenericEntityHook extends ORecordHookAbstract
{

    private static final Logger logger = LoggerFactory.getLogger(GenericEntityHook.class);

    @Override
    public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode()
    {
        return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
    }

    @Override
    public RESULT onRecordBeforeCreate(ORecord iRecord)
    {
        RESULT result = RESULT.RECORD_NOT_CHANGED;
        if (!(iRecord instanceof ODocument))
        {
            return result;
        }

        ODocument doc = (ODocument) iRecord;
        if ("ArtifactEntry".equals(doc.getClassName()))
        {
            doc.field("created", new Date());
            result = RESULT.RECORD_CHANGED;
        }
        for (OClass oClass : doc.getSchemaClass().getAllSuperClasses())
        {
            if ("GenericEntity".equals(oClass.getName()))
            {
                String uuid = doc.field("uuid");
                if (uuid == null || uuid.trim().isEmpty())
                {
                    throw new OValidationException(
                            String.format("Failed to persist document [%s]. UUID can't be empty or null.",
                                          doc.getSchemaClass()));
                }
            }
            else if ("ArtifactEntry".equals(oClass.getName()))
            {
                ODocument artifactCoordinates = doc.field("artifactCoordinates");
                String artifactCoordinatesPath = artifactCoordinates == null ? "" : artifactCoordinates.field("path");

                doc.field("created", new Date());
                result = RESULT.RECORD_CHANGED;

                String artifactEntryPath = doc.field("artifactPath");
                artifactEntryPath = artifactEntryPath == null ? "" : artifactEntryPath.trim();

                if (artifactCoordinatesPath.trim().isEmpty() && artifactEntryPath.trim().isEmpty())
                {
                    throw new OValidationException(
                            String.format("Failed to persist document [%s]. 'artifactPath' can't be empty or null.",
                                          doc.getSchemaClass()));
                }
                else if (artifactCoordinates != null && !artifactEntryPath.equals(artifactCoordinatesPath))
                {
                    throw new OValidationException(
                            String.format("Failed to persist document [%s]. Paths [%s] and [%s] dont match.",
                                          doc.getSchemaClass(), artifactEntryPath, artifactCoordinatesPath));
                }
            }
        }

        return result;
    }

}

@@ -1161,84 +1161,49 @@
     
-doc.field(%22created%22, new Date());%0A            result = RESULT.RECORD_CHANGED
+validateArtifactEntryCreatedProperty(doc)
 ;%0A  
@@ -1202,32 +1202,33 @@
 doc);%0A        %7D%0A
+%0A
         for (OCl
@@ -2031,107 +2031,8 @@
 );%0A%0A
-                doc.field(%22created%22, new Date());%0A                result = RESULT.RECORD_CHANGED;%0A%0A
     
@@ -2984,55 +2984,478 @@
   %7D%0A
-            %7D%0A        %7D%0A%0A        return result;
+%0A                validateArtifactEntryCreatedProperty(doc);%0A            %7D%0A        %7D%0A%0A        return result;%0A    %7D%0A%0A    private void validateArtifactEntryCreatedProperty(ODocument doc) %7B%0A        Date created = doc.field(%22created%22);%0A        if (created == null)%0A        %7B%0A            throw new OValidationException(%0A                    String.format(%22Failed to persist document %5B%25s%5D. 'created' can't be null.%22,%0A                            doc.getSchemaClass()));%0A        %7D
 %0A   

Remove updating 'created' property of ArtifactEntry. Validate if 'created' property is not null while creating ArtifactEntry