buildscript {
    ext.cubaVersion = '7.2.13'
    repositories {
        mavenLocal()
        maven {
            url 'https://artifactory.kkt.mcb.ru/artifactory/libs-release'
        }
        maven {
            url 'https://artifactory.kkt.mcb.ru/artifactory/libs-snapshot'
        }
        maven {
            url 'https://artifactory.kkt.mcb.ru/gradle-release'
        }
    }
    dependencies {
        classpath "com.haulmont.gradle:cuba-plugin:$cubaVersion"
        classpath 'io.franzbecker:gradle-lombok:2.0'
        classpath 'ru.mkb.corp.siebel:siebel-service:1.3-SNAPSHOT'
    }
}

def modulePrefix = 'mm'

def globalModule = project(":${modulePrefix}-global")
def coreModule = project(":${modulePrefix}-core")
def webModule = project(":${modulePrefix}-web")
def webToolkitModule = project(":${modulePrefix}-web-toolkit")

def servletApi = 'javax.servlet:javax.servlet-api:3.1.0'

apply(plugin: 'idea')
apply(plugin: 'cuba')
apply plugin: 'io.franzbecker.gradle-lombok'

cuba {
    artifact {
        group = 'ru.mkb.corp.monmod'
        version = '0.2'
        isSnapshot = true
    }
    tomcat {
        dir = "$project.rootDir/deploy/tomcat"
        port = '8082'
    }
}

dependencies {
    appComponent("com.haulmont.cuba:cuba-global:$cubaVersion")
    appComponent('com.haulmont.addon.ldap:ldap-global:1.5.3')
    appComponent('com.haulmont.addon.bproc:bproc-global:1.2.0')
    appComponent("com.haulmont.reports:reports-global:$cubaVersion")
    appComponent('com.haulmont.addon.restapi:restapi-global:7.2.4')
    appComponent('de.diedavids.cuba.entitysoftreference:entity-soft-reference-global:0.7.0')
}

def oracle = 'com.oracle.database.jdbc:ojdbc6:11.2.0.4'

configure([globalModule, coreModule, webModule, webToolkitModule]) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'cuba')
    apply plugin: 'io.franzbecker.gradle-lombok'

    dependencies {
        compile('ru.cbm:working-days-calculator:0.0.7')
        testCompile('org.junit.jupiter:junit-jupiter-api:5.5.2')
        testCompile('org.junit.jupiter:junit-jupiter-engine:5.5.2')
        testCompile('org.junit.vintage:junit-vintage-engine:5.5.2')
        implementation 'pull-parser:pull-parser:2.1.10'
        implementation 'javax.validation:validation-api:2.0.1.Final'
    }

    task sourceJar(type: Jar) {
        from file('src')
        classifier = 'sources'
    }

    artifacts {
        archives sourceJar
    }
    test {
        useJUnitPlatform()
    }
    sourceCompatibility = '11'
    targetCompatibility = '11'
}


configure(globalModule) {
    dependencies {
        if (!JavaVersion.current().isJava8()) {
            runtime('javax.xml.bind:jaxb-api:2.3.1')
            runtime('org.glassfish.jaxb:jaxb-runtime:2.3.1')
        }
        compile group: 'ru.mkb.corp.monmod', name: 'monmod-dto', version: '1.7-SNAPSHOT'
        compile('org.flowable:flowable-engine:6.5.0')
    }

    entitiesEnhancing {
        main {
            enabled = true
        }
    }
}

configure(coreModule) {

    configurations {
        jdbc
        dbscripts
    }

    dependencies {
        compile('com.sun.xml.ws:jaxws-rt:2.3.0')
        compile('com.sun.xml.ws:rt:2.3.0')
        compile(globalModule)
        compile('ru.mkb.corp.siebel:siebel-service:1.3-SNAPSHOT')
        compile('ru.mkb.corp.wso:get-kpk-rko-curator:1.1-SNAPSHOT')
        compile('ru.mkb.corp.wso:elar-all-in-one:1.3-SNAPSHOT')
        compile('org.springframework.amqp:spring-rabbit:2.3.6')
        compile('org.springframework.amqp:spring-amqp:2.3.6')
        compile("org.apache.poi:poi-ooxml:4.1.1")
        compileOnly(servletApi)
        jdbc(oracle)
        testRuntime(oracle)
    }

    task cleanConf(description: 'Cleans up conf directory', type: Delete) {
        delete "$cuba.appHome/${modulePrefix}-core/conf"
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = "${modulePrefix}-core"
        appJars(modulePrefix + '-global', modulePrefix + '-core')
    }

    task createDb(dependsOn: assembleDbScripts, description: 'Creates local database', type: CubaDbCreation) {
        oracleSystemUser = 'system'
        oracleSystemPassword = 'oracle'
    }

    task updateDb(dependsOn: assembleDbScripts, description: 'Updates local database', type: CubaDbUpdate) {
    }
}

configure(webModule) {
    configurations {
        webcontent
    }

    dependencies {
        compileOnly(servletApi)
        compile(globalModule)
    }

    jar {
        with copySpec {
            from sourceSets.main.allJava
            include "ru/mkb/corp/monmod/web/toolkit/ui/client/**"
        }
    }

    task webArchive(type: Zip) {
        from file("$buildDir/web")
        from file('web')
        classifier = 'web'
    }

    artifacts {
        archives webArchive
    }

    task deployConf(type: Copy) {
        from file('src')
        include "ru/mkb/corp/monmod/**"
        into "$cuba.appHome/${modulePrefix}/conf"
    }

    task clearMessagesCache(type: CubaClearMessagesCache) {
        appName = "${modulePrefix}"
    }
    deployConf.dependsOn clearMessagesCache

    task cleanConf(description: 'Cleans up conf directory', type: Delete) {
        delete "$cuba.appHome/${modulePrefix}/conf"
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = "${modulePrefix}"
        appJars(modulePrefix + '-global', modulePrefix + '-web')
    }

    task buildScssThemes(type: CubaWebScssThemeCreation)

    task deployThemes(type: CubaDeployThemeTask, dependsOn: buildScssThemes)

    assemble.dependsOn buildScssThemes

    task themesJar(type: Jar) {
        from file('themes')
        classifier = 'themes'
    }

    artifacts {
        archives themesJar
    }
}

configure(webToolkitModule) {
    dependencies {
        compile(webModule)
    }

    jar {
        from sourceSets.main.allJava
    }

    task buildWidgetSet(type: CubaWidgetSetBuilding) {
        widgetSetClass = 'ru.mkb.corp.monmod.web.toolkit.ui.AppWidgetSet'
    }

    task debugWidgetSet(type: CubaWidgetSetDebug) {
        widgetSetClass = 'ru.mkb.corp.monmod.web.toolkit.ui.AppWidgetSet'
    }

    task webArchive(dependsOn: buildWidgetSet, type: Jar) {
        from file("$buildDir/web")
        classifier = 'client'
    }

    artifacts {
        archives webArchive
    }

    task deploy(dependsOn: webArchive, type: Copy) {
        from webArchive
        into "$cuba.tomcat.dir/webapps/${modulePrefix}/WEB-INF/lib"
    }
}

task undeploy(type: Delete, dependsOn: ":${modulePrefix}-web:cleanConf") {
    delete("$cuba.tomcat.dir/shared")
    delete("$cuba.tomcat.dir/webapps/${modulePrefix}-core")
    delete("$cuba.tomcat.dir/webapps/${modulePrefix}")
}

task restart(dependsOn: ['stop', ":${modulePrefix}-core:deploy", ":${modulePrefix}-web:deploy", ":${modulePrefix}-web-toolkit:deploy"], description: 'Redeploys applications and restarts local Tomcat') {
    doLast {
        ant.waitfor(maxwait: 6, maxwaitunit: 'second', checkevery: 2, checkeveryunit: 'second') {
            not {
                socket(server: 'localhost', port: '8787')
            }
        }
    }
}
restart.finalizedBy start

task buildWar(type: CubaWarBuilding) {
    includeJdbcDriver = true
    appProperties = ['cuba.automaticDatabaseUpdate': true]
    webXmlPath = 'modules/web/web/WEB-INF/single-war-web.xml'
    logbackConfigurationFile = 'etc/war-logback.xml'
}