/*******************************************************************************
 * Copyright (c) 2012-2016 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.ext.java.client.project.interceptor;

import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.promises.client.js.Promises;
import org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.ide.api.project.node.Node;
import org.eclipse.che.ide.api.project.node.interceptor.NodeInterceptor;
import org.eclipse.che.ide.ext.java.client.project.node.JavaNodeManager;
import org.eclipse.che.ide.ext.java.client.project.settings.JavaNodeSettings;
import org.eclipse.che.ide.ext.java.shared.ContentRoot;
import org.eclipse.che.ide.project.node.FolderReferenceNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Vlad Zhukovskiy
 */
public abstract class AbstractJavaContentRootInterceptor implements NodeInterceptor {

    private JavaNodeManager nodeManager;

    public AbstractJavaContentRootInterceptor(JavaNodeManager nodeManager) {
        this.nodeManager = nodeManager;
    }

    @Override
    public Promise<List<Node>> intercept(Node parent, List<Node> children) {
        List<Node> nodes = new ArrayList<>();

        for (Node child : children) {
            ContentRoot contentRoot = getSourceType(child);

            if (contentRoot == null) {
                nodes.add(child);
                continue;
            }

            FolderReferenceNode oldNode = (FolderReferenceNode)child;

            JavaNodeSettings settings = (JavaNodeSettings)nodeManager.getJavaSettingsProvider().getSettings();

            nodes.add(nodeManager.getJavaNodeFactory().newSourceFolderNode(oldNode.getData(),
                                                                           oldNode.getProjectConfig(),
                                                                           settings,
                                                                           contentRoot));
        }

        return Promises.resolve(nodes);
    }

    @Nullable
    private ContentRoot getSourceType(Node node) {
        if (!JavaNodeManager.isJavaProject(node)) {
            return null;
        }

        if (!(node instanceof FolderReferenceNode)) {
            return null;
        }

        final FolderReferenceNode folderNode = (FolderReferenceNode)node;
        final ProjectConfigDto projectConfig = folderNode.getProjectConfig();

        String srcFolder = _getSourceFolder(projectConfig, getSrcFolderAttribute());
        if (folderNode.getStorablePath().endsWith(srcFolder)) {
            return ContentRoot.SOURCE;
        }

        String testSrcFolder = _getSourceFolder(projectConfig, getTestSrcFolderAttribute());
        if (folderNode.getStorablePath().endsWith(testSrcFolder)) {
            return ContentRoot.TEST_SOURCE;
        }

        return null;
    }

    private String _getSourceFolder(ProjectConfigDto projectConfig, String srcAttribute) {
        Map<String, List<String>> attributes = projectConfig.getAttributes();
        if (!attributes.containsKey(srcAttribute)) {
            return "";
        }

        List<String> values = attributes.get(srcAttribute);

        if (values.isEmpty()) {
            return "";
        }

        String srcFolder = "";

        if ("maven.resource.folder".equals(srcAttribute)) {
            for (String srcFolderValue : values) {
                if (srcFolderValue.endsWith("/resources")) {
                    srcFolder = srcFolderValue;

                    break;
                }
            }
        } else {
            srcFolder = values.get(0);
        }

        return projectConfig.getPath() + (srcFolder.startsWith("/") ? srcFolder : "/" + srcFolder);
    }

    public abstract String getSrcFolderAttribute();

    public abstract String getTestSrcFolderAttribute();

    @Override
    public int getPriority() {
        return MAX_PRIORITY;
    }
}

@@ -3010,32 +3010,53 @@
 ));%0A        if (
+srcFolder != null && 
 folderNode.getSt
@@ -3246,16 +3246,41 @@
     if (
+testSrcFolder != null && 
 folderNo
@@ -3509,16 +3509,22 @@
         
+final 
 Map%3CStri
@@ -3649,34 +3649,36 @@
          return 
-%22%22
+null
 ;%0A        %7D%0A%0A   
@@ -3678,24 +3678,30 @@
  %7D%0A%0A        
+final 
 List%3CString%3E
@@ -3740,17 +3740,16 @@
 ibute);%0A
-%0A
         
@@ -3818,340 +3818,20 @@
     
+final 
 String
- srcFolder = %22%22;%0A%0A        if (%22maven.resource.folder%22.equals(srcAttribute)) %7B%0A            for (String srcFolderValue : values) %7B%0A                if (srcFolderValue.endsWith(%22/resources%22)) %7B%0A                    srcFolder = srcFolderValue;%0A%0A                    break;%0A                %7D%0A            %7D%0A        %7D else %7B%0A           
  src
@@ -3853,26 +3853,16 @@
 .get(0);
-%0A        %7D
 %0A%0A      

Exclude non-source directories from java project