/*
 * Copyright © 2010 Red Hat, Inc.
 *
 * 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 com.redhat.rhevm.api.powershell.resource;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import com.redhat.rhevm.api.common.resource.AbstractActionableResource;
import com.redhat.rhevm.api.common.resource.AttachmentActionValidator;
import com.redhat.rhevm.api.model.Action;
import com.redhat.rhevm.api.model.ActionsBuilder;
import com.redhat.rhevm.api.model.ActionValidator;
import com.redhat.rhevm.api.model.Attachment;
import com.redhat.rhevm.api.model.DataCenter;
import com.redhat.rhevm.api.model.StorageDomain;
import com.redhat.rhevm.api.resource.AttachmentResource;

public class PowerShellAttachmentResource extends AbstractActionableResource<StorageDomain> implements AttachmentResource {

    private String storageDomainId;

    /**
     * Package-protected ctor, never needs to be instantiated by JAX-RS framework.
     *
     * @param attachment  encapsulated Attachment
     */
    PowerShellAttachmentResource(String dataCenterId, String storageDomainId) {
        super(dataCenterId);
        this.storageDomainId = storageDomainId;
    }

    private static void setStorageDomainHref(Attachment attachment, UriBuilder baseUriBuilder) {
        StorageDomain storageDomain = attachment.getStorageDomain();

        String href = PowerShellStorageDomainsResource.getHref(baseUriBuilder, storageDomain.getId());

        storageDomain.setHref(href);
    }

    private static void setDataCenterHref(Attachment attachment, UriBuilder baseUriBuilder) {
        DataCenter dataCenter = attachment.getDataCenter();

        String href = PowerShellDataCentersResource.getHref(baseUriBuilder, dataCenter.getId());

        dataCenter.setHref(href);
    }

    public static Attachment addLinks(Attachment attachment, UriInfo uriInfo, UriBuilder uriBuilder) {
        UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();

        attachment.setHref(uriBuilder.build().toString());

        setStorageDomainHref(attachment, baseUriBuilder);
        setDataCenterHref(attachment, baseUriBuilder);

        ActionValidator actionValidator = new AttachmentActionValidator(attachment);
        ActionsBuilder actionsBuilder = new ActionsBuilder(uriBuilder, AttachmentResource.class, actionValidator);
        attachment.setActions(actionsBuilder.build());

        return attachment;
    }

    @Override
    public Attachment get(UriInfo uriInfo) {
        StringBuilder buf = new StringBuilder();

        buf.append("get-storagedomain");
        buf.append(" -datacenterid " + getId());
        buf.append(" -storagedomainid " + storageDomainId);

        StorageDomain storageDomain = PowerShellStorageDomainResource.runAndParseSingle(buf.toString());

        DataCenter dataCenter = new DataCenter();
        dataCenter.setId(getId());

        Attachment attachment = PowerShellAttachmentsResource.buildAttachment(dataCenter, storageDomain);

        return addLinks(attachment, uriInfo, uriInfo.getRequestUriBuilder());
    }

    @Override
    public Response activate(UriInfo uriInfo, Action action) {
        // FIXME: implement
        return doAction(uriInfo, new DoNothingTask(action));
    }

    @Override
    public Response deactivate(UriInfo uriInfo, Action action) {
        // FIXME: implement
        return doAction(uriInfo, new DoNothingTask(action));
    }
}

@@ -1448,16 +1448,77 @@
 esource;
+%0Aimport com.redhat.rhevm.api.powershell.util.PowerShellUtils;
 %0A%0Apublic
@@ -3960,36 +3960,8 @@
 ) %7B%0A
-        // FIXME: implement%0A
     
@@ -3993,36 +3993,69 @@
 fo, new 
-DoNothingTask(action
+AttachmentActionTask(action, %22activate-storagedomain%22
 ));%0A    
@@ -4148,88 +4148,643 @@
     
-// FIXME: implement%0A        return doAction(uriInfo, new DoNothingTask(action));
+return doAction(uriInfo, new AttachmentActionTask(action, %22deactivate-storagedomain%22));%0A    %7D%0A%0A    private class AttachmentActionTask extends AbstractActionTask %7B%0A        private String command;%0A        public AttachmentActionTask(Action action, String command) %7B%0A            super(action);%0A            this.command = command;%0A        %7D%0A        public void run() %7B%0A            StringBuilder buf = new StringBuilder();%0A%0A            buf.append(command);%0A            buf.append(%22 -datacenterid %22 + getId());%0A            buf.append(%22 -storagedomainid %22 + storageDomainId);%0A%0A            PowerShellUtils.runCommand(buf.toString());%0A        %7D
 %0A   

Implement powershell storage domain activation/deactivation