package org.carlspring.strongbox.data.domain;

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;
        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");

                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;
    }

}

@@ -40,16 +40,39 @@
 omain;%0A%0A
+import java.util.Date;%0A
 import j
@@ -86,16 +86,16 @@
 l.UUID;%0A
-
 %0Aimport 
@@ -1079,16 +1079,182 @@
 Record;%0A
+        if (%22ArtifactEntry%22.equals(doc.getClassName()))%0A        %7B%0A            doc.field(%22created%22, new Date());%0A            result = RESULT.RECORD_CHANGED;%0A        %7D%0A
         
@@ -1937,24 +1937,24 @@
 rdinates%22);%0A
-
             
@@ -2057,24 +2057,123 @@
 d(%22path%22);%0A%0A
+                doc.field(%22created%22, new Date());%0A                result = RESULT.RECORD_CHANGED;%0A%0A
             

Initialize created field with current date when creating ArtifactEntry or it's derivatives