package com.collabnet.ctf.events.custom.events;

import com.collabnet.ce.soap60.webservices.tracker.ArtifactSoapDO;
import com.vasoftware.sf.events.EventHandler60;

public class ClosedArtifactValidator extends EventHandler60 {

    private static final String ERROR_CLOSED_ARTIFACT_UPDATE = "You can not update a closed artifact. " +
            "Please re-open the artifact to proceed with update";

    @Override
    public void processEvent() throws Exception {
        ArtifactSoapDO originalArtf = (ArtifactSoapDO) getOriginalData();
        ArtifactSoapDO updatedArtf = (ArtifactSoapDO) getUpdatedData();

        // If the artifact is in `Closed` meta-status then dont allow
        // the update.

        if (originalArtf.getStatusClass().equals("Close") &&
                updatedArtf.getStatusClass().equals("Close")) {

            // Pass an error message for UI. This will be shown on UI
            addErrorMessage(ERROR_CLOSED_ARTIFACT_UPDATE);

            // Fail the artifact update
            throw new Exception(ERROR_CLOSED_ARTIFACT_UPDATE);
        }
    }
}
