/*******************************************************************************
 * Copyright (c) 2012-2015 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.extension.maven.server.projecttype.handler;

import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.ForbiddenException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.project.server.FolderEntry;
import org.eclipse.che.api.project.server.VirtualFileEntry;
import org.eclipse.che.api.project.server.handlers.CreateModuleHandler;
import org.eclipse.che.ide.extension.maven.shared.MavenAttributes;
import org.eclipse.che.ide.maven.tools.Model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * @author Vitaly Parfonov
 * @author Dmitry Shnurenko
 */
public class AddMavenModuleHandler implements CreateModuleHandler {

    private static final Logger LOG = LoggerFactory.getLogger(AddMavenModuleHandler.class);

    @Override
    public void onCreateModule(FolderEntry parentFolder, String modulePath, String moduleType, Map<String, String> options)
            throws ForbiddenException, ConflictException, ServerException {
        if (!moduleType.equals(MavenAttributes.MAVEN_ID)) {
            LOG.warn("Module must be Maven project to able be added to Maven project");
            throw new IllegalArgumentException("Module must be Maven project to able be added to Maven project");
        }
        VirtualFileEntry pom = parentFolder.getChild("pom.xml");
        if (pom == null) {
            return;
        }
        try {
            Model model = Model.readFrom(pom.getVirtualFile());
            if ("pom".equals(model.getPackaging())) {
                final String relativePath = modulePath.substring(parentFolder.getPath().length() + 1);
                if (!model.getModules().contains(relativePath)) {
                    model.addModule(relativePath);
                    model.writeTo(pom.getVirtualFile());
                }
            } else {
                throw new IllegalArgumentException("Project must have packaging 'pom' in order to adding modules.");
            }
        } catch (IOException e) {
            throw new ServerException(e);
        }
    }

    @Override
    public String getProjectType() {
        return MavenAttributes.MAVEN_ID;
    }
}

@@ -923,24 +923,114 @@
 uleHandler;%0A
+import org.eclipse.che.ide.extension.maven.server.projecttype.MavenClassPathConfigurator;%0A
 import org.e
@@ -1084,16 +1084,16 @@
 ibutes;%0A
-
 import o
@@ -1130,17 +1130,16 @@
 .Model;%0A
-%0A
 import o
@@ -1538,46 +1538,139 @@
 der,
- String modulePath, String moduleType,
+%0A                               String modulePath,%0A                               String moduleType,%0A                              
  Map
@@ -1694,28 +1694,16 @@
 options)
-%0A           
  throws 
@@ -1759,16 +1759,71 @@
 ption %7B%0A
+        configureClassPath(parentFolder, modulePath);%0A%0A
         
@@ -2863,16 +2863,16 @@
 ion(e);%0A
-
         
@@ -2868,24 +2868,668 @@
 );%0A        %7D
+%0A%0A    %7D%0A%0A    private static void configureClassPath(FolderEntry parentFolder, String path) throws ServerException,%0A                                                                                         ForbiddenException,%0A                                                                                         ConflictException %7B%0A        String pathToModule = path.contains(%22/%22) ? path.substring(path.lastIndexOf(%22/%22)) : path;%0A        VirtualFileEntry addedModule = parentFolder.getChild(pathToModule);%0A%0A        if (addedModule == null) %7B%0A            return;%0A        %7D%0A%0A        MavenClassPathConfigurator.configure((FolderEntry)addedModule);
 %0A    %7D%0A%0A    

Add code assistant in created modules