/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.domain.controller.operations;

import java.util.Locale;

import org.jboss.as.controller.BasicOperationResult;
import org.jboss.as.controller.ModelAddOperationHandler;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationResult;
import org.jboss.as.controller.ResultHandler;
import org.jboss.as.controller.descriptions.DescriptionProvider;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.domain.controller.descriptions.ServerGroupDescription;
import org.jboss.dmr.ModelNode;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.*;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SOCKET_BINDING_GROUP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SOCKET_BINDING_PORT_OFFSET;

/**
 * @author Emanuel Muckenhuber
 */
public class ServerGroupAddHandler implements ModelAddOperationHandler, DescriptionProvider {

    public static final ServerGroupAddHandler INSTANCE = new ServerGroupAddHandler();

    /** {@inheritDoc} */
    @Override
    public OperationResult execute(OperationContext context, ModelNode operation, ResultHandler resultHandler) {
        final ModelNode subModel = context.getSubModel();
        subModel.get(PROFILE).set(operation.require(PROFILE));
        subModel.get(JVM).setEmptyObject();
        subModel.get(DEPLOYMENT).setEmptyObject();

        if(operation.hasDefined(SOCKET_BINDING_GROUP)) {
            subModel.get(SOCKET_BINDING_GROUP).set(operation.get(SOCKET_BINDING_GROUP));
        }
        if(operation.hasDefined(SOCKET_BINDING_PORT_OFFSET)) {
            subModel.get(SOCKET_BINDING_PORT_OFFSET).set(operation.get(SOCKET_BINDING_PORT_OFFSET));
        }

        final ModelNode compensatingOperation = Util.getResourceRemoveOperation(operation.get(OP_ADDR));

        resultHandler.handleResultComplete();
        return new BasicOperationResult(compensatingOperation);
    }

    @Override
    public ModelNode getModelDescription(Locale locale) {
        return ServerGroupDescription.getServerGroupAdd(locale);
    }

}

@@ -1659,212 +1659,8 @@
 s.*;
-%0Aimport static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SOCKET_BINDING_GROUP;%0Aimport static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SOCKET_BINDING_PORT_OFFSET;
 %0A%0A/*
@@ -2154,103 +2154,8 @@
 E));
-%0A        subModel.get(JVM).setEmptyObject();%0A        subModel.get(DEPLOYMENT).setEmptyObject();
 %0A%0A  
@@ -2308,16 +2308,17 @@
       %7D%0A
+%0A
         
@@ -2476,32 +2476,291 @@
 T));%0A        %7D%0A%0A
+        if(operation.hasDefined(JVM)) %7B%0A            subModel.get(JVM).set(operation.get(JVM).asString(), new ModelNode());%0A        %7D%0A        else %7B%0A            subModel.get(JVM).setEmptyObject();%0A        %7D%0A%0A        subModel.get(DEPLOYMENT).setEmptyObject();%0A%0A
         final Mo
@@ -3110,10 +3110,9 @@
 %0A    %7D%0A%0A
+
 %7D
-%0A

create jvm value as property type