001/*
002 * Copyright (c) 2016-2017 Chris K Wensel. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.nested.core;
022
023import cascading.flow.FlowProcess;
024import cascading.operation.Function;
025import cascading.operation.FunctionCall;
026import cascading.operation.OperationCall;
027import cascading.tuple.Fields;
028import cascading.tuple.Tuple;
029import cascading.tuple.TupleEntry;
030import heretical.pointer.operation.Builder;
031
032/**
033 * Class NestedBaseBuildFunction is the base class for {@link Function} implementations that rely on the
034 * {@link BuildSpec} class when declaring transformations on nested object types.
035 * <p>
036 * Specifically, {@code *BuildAsFunction} and {@code *BuildIntoFunction} classes create or update (respectively)
037 * nested object types from Function argument {@link Fields} where the field values can be primitive types, objects
038 * (with a corresponding {@link cascading.tuple.type.CoercibleType}), or themselves nest object types.
039 * <p>
040 * In the case of a {@code *BuildIntoFunction} the last argument in the {@code arguments} {@link TupleEntry} will be
041 * the object the BuildSpec copies values into.
042 * <p>
043 * In the case of a {@code *BuildAsFunction} a new root object will be created for the BuildSpec to copy values into.
044 * <p>
045 * In the case of JSON objects, multiple JSON objects selected as arguments can be combined into a new JSON object
046 * by mapping the field names into locations on the new object.
047 * <p>
048 * For selecting the values from an existing nested object in order to create a new object or update an existing one
049 * see {@link NestedBaseCopyFunction} sub-classes.
050 *
051 * @see BuildSpec
052 */
053public abstract class NestedBaseBuildFunction<Node, Result> extends NestedSpecBaseOperation<Node, Result, NestedBaseBuildFunction.Context> implements Function<NestedBaseBuildFunction.Context>
054  {
055  protected static class Context
056    {
057    public Tuple result;
058
059    public Context( Tuple result )
060      {
061      this.result = result;
062      }
063    }
064
065  protected Builder<Node, Result> builder;
066
067  public NestedBaseBuildFunction( NestedCoercibleType<Node, Result> nestedCoercibleType, Fields fieldDeclaration, BuildSpec... buildSpecs )
068    {
069    super( nestedCoercibleType, fieldDeclaration );
070    this.builder = new Builder( nestedCoercibleType.getNestedPointerCompiler(), buildSpecs );
071    }
072
073  @Override
074  public void prepare( FlowProcess flowProcess, OperationCall<Context> operationCall )
075    {
076    operationCall.setContext( new Context( Tuple.size( 1 ) ) );
077    }
078
079  @Override
080  public void operate( FlowProcess flowProcess, FunctionCall<Context> functionCall )
081    {
082    Node rootNode = getResultNode( functionCall );
083
084    TupleEntry arguments = functionCall.getArguments();
085
086    builder.build( arguments::getObject, rootNode );
087
088    Context context = functionCall.getContext();
089
090    context.result.set( 0, rootNode );
091
092    functionCall.getOutputCollector().add( context.result );
093    }
094  }