// ConcatSequenceSetImpl.java
//
// Created: February 2008
//
// Copyright 2008 Martin Senger
//
// Licensed 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.jmoby.tutorial.service;

import net.jmoby.samples.ConcatSequenceSetSkel;
import org.biomoby.shared.MobyException;
import org.biomoby.shared.parser.MobyPackage;
import org.biomoby.shared.parser.MobyJob;
import org.biomoby.shared.datatypes.*;

import org.apache.commons.lang.BooleanUtils;

/**
 * A simple service that concatenates sequences contained in an input
 * collection. It is a service from the tutorial series: It can be
 * used to illustrate that a MoSeS-based service can accept more
 * specialized collection elements and still correctly assign them
 * their fallback type (a type the service was registered for). <p>
 *
 * The same functionality can be found in the "ConcatSequences"
 * service that operates on individual primary inputs. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: ConcatSequenceSetImpl.java,v 1.2 2008/03/03 11:34:17 senger Exp $
 */
public class ConcatSequenceSetImpl
    extends ConcatSequenceSetSkel {

    /**************************************************************************
     * This is a mandatory method to be implemented.
     *************************************************************************/
    public void processIt (MobyJob request,
			   MobyJob response,
			   MobyPackage outputContext)
	throws MobyException {

	// primary input (may be empty)
	GenericSequence[] seqs = get_seqsSet (request);

	// secondary input (an empty input means 'false')
	boolean onlyLength =
	    BooleanUtils.toBoolean (getParameter_return_only_length (request));

	// build the result
	int totalLength = 0;
	StringBuilder buf = new StringBuilder();
	if (seqs != null) {
	    for (GenericSequence seq: seqs) {
		totalLength += ConcatSequencesImpl.addOneInput (seq, buf);
	    }
	}

	GenericSequence result = new GenericSequence();
	result.set_SequenceString (onlyLength ? "" : buf.toString());
	result.set_Length (new MobyInteger (totalLength));

	set_result (response, result);
    }

}

