/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.ode.bpel.compiler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.ode.bpel.compiler.api.CompilationException;
import org.apache.ode.bpel.compiler.bom.Activity;
import org.apache.ode.bpel.compiler.bom.Correlation;
import org.apache.ode.bpel.compiler.bom.InvokeActivity;
import org.apache.ode.bpel.o.OActivity;
import org.apache.ode.bpel.o.OInvoke;
import org.apache.ode.bpel.o.OProcess;
import org.apache.ode.bpel.o.OScope;
import org.apache.ode.utils.msg.MessageBundle;
import org.apache.ode.utils.stl.CollectionsX;
import org.apache.ode.utils.stl.MemberOfFunction;

/**
 * Generates code for <code>&lt;invoke&gt;</code> activities.
 */
class InvokeGenerator extends DefaultActivityGenerator {

    private static final InvokeGeneratorMessages __imsgs = MessageBundle.getMessages(InvokeGeneratorMessages.class);

    public OActivity newInstance(Activity src) {
        return new OInvoke(_context.getOProcess(), _context.getCurrent());
    }

    public void compile(OActivity output, Activity srcx) {
        InvokeActivity src = (InvokeActivity) srcx;
        OInvoke oinvoke = (OInvoke) output;

        oinvoke.partnerLink = _context.resolvePartnerLink(src.getPartnerLink());
        oinvoke.operation = _context.resolvePartnerRoleOperation(oinvoke.partnerLink, src.getOperation());
        assert oinvoke.operation.getInput() != null; // ensured by
        // resolvePartnerRoleOperation
        assert oinvoke.operation.getInput().getMessage() != null; // ensured
        // by
        // resolvePartnerRoleOperation
        // TODO: Add portType checking if specified by user
        // if (portType != null &&
        // !portType.equals(onMessage.partnerLink.myRolePortType.getQName()))
        // throw new CompilationException(CMSGSG.errPortTypeMismatch(portType,
        // onMessage.partnerLink.myRolePortType.getQName()));
        if (oinvoke.operation.getInput() != null && oinvoke.operation.getInput().getMessage() != null) {
            if (src.getInputVar() == null)
                throw new CompilationException(__imsgs.errInvokeNoInputMessageForInputOp(oinvoke.operation.getName()));
            oinvoke.inputVar = _context.resolveMessageVariable(src.getInputVar(), oinvoke.operation.getInput()
                    .getMessage().getQName());
        }
        if (oinvoke.operation.getOutput() != null && oinvoke.operation.getOutput().getMessage() != null) {
            if (src.getOutputVar() == null)
                throw new CompilationException(__imsgs.errInvokeNoOutputMessageForOutputOp(oinvoke.operation.getName()));
            oinvoke.outputVar = _context.resolveMessageVariable(src.getOutputVar(), oinvoke.operation.getOutput()
                    .getMessage().getQName());
        }
        List<Correlation> correlations = src.getCorrelations();
        List<Correlation> incorrelations = CollectionsX.filter(new ArrayList<Correlation>(), correlations,
                new MemberOfFunction<Correlation>() {
                    @Override
                    public boolean isMember(Correlation o) {
                        return o.getPattern() == Correlation.CorrelationPattern.IN;
                    }
                });
        List<Correlation> outcorrelations = CollectionsX.filter(new ArrayList<Correlation>(), correlations,
                new MemberOfFunction<Correlation>() {
                    @Override
                    public boolean isMember(Correlation o) {
                        return o.getPattern() == Correlation.CorrelationPattern.OUT;
                    }
                });

        List<Correlation> inoutcorrelations = CollectionsX.filter(new ArrayList<Correlation>(), correlations,
                new MemberOfFunction<Correlation>() {
                    @Override
                    public boolean isMember(Correlation o) {
                        return o.getPattern() == Correlation.CorrelationPattern.INOUT;
                    }
                });

        if (oinvoke.inputVar != null) {
            doCorrelations(outcorrelations, oinvoke.inputVar, oinvoke.assertCorrelationsInput,
                    oinvoke.initCorrelationsInput);
            doCorrelations(inoutcorrelations, oinvoke.inputVar, oinvoke.assertCorrelationsInput,
                    oinvoke.initCorrelationsInput);
        }
        if (oinvoke.outputVar != null) {
            doCorrelations(incorrelations, oinvoke.outputVar,
                    oinvoke.assertCorrelationsOutput, oinvoke.initCorrelationsOutput);
            doCorrelations(inoutcorrelations, oinvoke.outputVar,
                    oinvoke.assertCorrelationsOutput, oinvoke.initCorrelationsOutput);
        }
        

//        Partner link could be initialized with magic session in a previous receive.
//        if (!oinvoke.getOwner().version.equals(Constants.NS_BPEL4WS_2003_03)) {
//            if (!oinvoke.partnerLink.initializePartnerRole && !_context.isPartnerLinkAssigned(oinvoke.partnerLink.getName())) {
//                throw new CompilationException(__cmsgs.errUninitializedPartnerLinkInInvoke(oinvoke.partnerLink.getName()));
//            }
//        }
    }

    private void doCorrelations(List<Correlation> correlations, OScope.Variable var,
            Collection<OScope.CorrelationSet> assertCorrelations, Collection<OScope.CorrelationSet> initCorrelations) {
        for (Correlation correlation : correlations) {
            OScope.CorrelationSet cset = _context.resolveCorrelationSet(correlation.getCorrelationSet());
            switch (correlation.getInitiate()) {
            case NO:
                assertCorrelations.add(cset);
                break;
            case YES:
                initCorrelations.add(cset);
                break;
            case JOIN:
                // TODO: fixe errror
                throw new UnsupportedOperationException();
            }
            for (OProcess.OProperty property : cset.properties) {
                // Force resolution of alias, to make sure that we have one for
                // this variable-property pair.
                try {
                    _context.resolvePropertyAlias(var, property.name);
                } catch (CompilationException ce) {
                    if (ce.getCompilationMessage().source == null) {
                        ce.getCompilationMessage().source = correlation;
                    }
                    throw ce;
                }
                // onMessage.
            }
        }
    }
}

@@ -846,89 +846,8 @@
 r;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0A%0A
 impo
@@ -1361,16 +1361,97 @@
 ction;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0A%0A
 /**%0A * G
@@ -2810,32 +2810,189 @@
 ge() != null) %7B%0A
+            // Input var can be omitted if input message has no part %0A            if (oinvoke.operation.getInput().getMessage().getParts().size() %3E 0) %7B%0A    
             if (
@@ -3010,32 +3010,36 @@
 tVar() == null)%0A
+    
                 
@@ -3146,32 +3146,36 @@
 ));%0A            
+    
 oinvoke.inputVar
@@ -3269,32 +3269,36 @@
                 
+    
 .getMessage().ge
@@ -3300,32 +3300,46 @@
 ().getQName());%0A
+            %7D%0A
         %7D%0A      

The compiler should allow no input var if the operation input has no part.