package org.drools.compiler.kproject;

import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;

import org.drools.core.util.StringUtils;
import org.kie.api.builder.ReleaseId;

public class ReleaseIdImpl implements ReleaseId {
    private final String groupId;
    private final String artifactId;
    private final String version;

    private String snapshotVersion;

    public ReleaseIdImpl(String releaseId) {
        String[] split = releaseId.split(":");
        this.groupId = split[0];
        this.artifactId = split[1];
        this.version = split[2];
    }

    public ReleaseIdImpl(String groupId,
                         String artifactId,
                         String version) {
        this.groupId = groupId;
        this.artifactId = artifactId;
        this.version = version;
    }
    
    public String getGroupId() {
        return groupId;
    }
    
    public String getArtifactId() {
        return artifactId;
    }
    
    public String getVersion() {
        return version;
    }

    @Override
    public String toString() {
        return groupId + ":" + artifactId + ":" + version;
    }
    
    public String toExternalForm() {
        return toString();
    }
    
    public String getPomXmlPath() {
        return "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml";
    }

    public String getPomPropertiesPath() {
        return "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties";
    }   
    
    public String getCompilationCachePathPrefix() { 
        //return "META-INF/maven/" + groupId + "/" + artifactId + "/";
        return "META-INF/";
    }

    public boolean isSnapshot() {
        return version.endsWith("-SNAPSHOT");
    }
    
    public static ReleaseId fromPropertiesString(String string) {
        Properties props = new Properties();
        ReleaseId releaseId = null;
        try {
            props.load( new StringReader( string ) );
            String groupId = props.getProperty( "groupId" );
            String artifactId = props.getProperty( "artifactId" );
            String version = props.getProperty( "version" );            
            if ( StringUtils.isEmpty( groupId  ) || StringUtils.isEmpty( artifactId ) || StringUtils.isEmpty( version ) ) {
                throw new RuntimeException("pom.properties exists but ReleaseId content is malformed\n" + string);
            }
            releaseId =  new ReleaseIdImpl( groupId, artifactId, version );
        } catch ( IOException e ) {
            throw new RuntimeException( "pom.properties was malformed\n" + string, e );
        }
        
        return releaseId;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ReleaseIdImpl that = (ReleaseIdImpl) o;

        if (artifactId != null ? !artifactId.equals(that.artifactId) : that.artifactId != null) return false;
        if (groupId != null ? !groupId.equals(that.groupId) : that.groupId != null) return false;
        if (version != null ? !version.equals(that.version) : that.version != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = groupId != null ? groupId.hashCode() : 0;
        result = 31 * result + (artifactId != null ? artifactId.hashCode() : 0);
        result = 31 * result + (version != null ? version.hashCode() : 0);
        return result;
    }

    public String getSnapshotVersion() {
        return snapshotVersion;
    }

    public void setSnapshotVersion(String snapshotVersion) {
        this.snapshotVersion = snapshotVersion;
    }
}

@@ -47,27 +47,115 @@
 java.io.
-IOException
+Externalizable;%0Aimport java.io.IOException;%0Aimport java.io.ObjectInput;%0Aimport java.io.ObjectOutput
 ;%0Aimport
@@ -334,16 +334,32 @@
 eleaseId
+, Externalizable
  %7B%0A    p
@@ -361,30 +361,24 @@
     private 
-final 
 String group
@@ -389,30 +389,24 @@
     private 
-final 
 String artif
@@ -428,14 +428,8 @@
 ate 
-final 
 Stri
@@ -474,24 +474,56 @@
 otVersion;%0A%0A
+    public ReleaseIdImpl() %7B %7D%0A%0A
     public R
@@ -3807,11 +3807,449 @@
 n;%0A    %7D
+%0A%0A    @Override%0A    public void writeExternal(ObjectOutput out) throws IOException %7B%0A        out.writeObject(groupId);%0A        out.writeObject(artifactId);%0A        out.writeObject(version);%0A    %7D%0A%0A    @Override%0A    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException %7B%0A        groupId = (String)in.readObject();%0A        artifactId = (String)in.readObject();%0A        version = (String)in.readObject();%0A    %7D
 %0A%7D%0A

make ReleaseIdImpl Externalizable