/*
 * 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 com.google.dataflow.sample.timeseriesflow.verifier;

import com.google.dataflow.sample.timeseriesflow.TimeSeriesData.TSDataPoint;
import com.google.dataflow.sample.timeseriesflow.common.CommonUtils;
import org.apache.beam.sdk.annotations.Experimental;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.values.PCollection;

/** Ensure all TSDataPoint is valid. */
@Experimental
public class TSDataPointVerifier
    extends PTransform<PCollection<TSDataPoint>, PCollection<TSDataPoint>> {

  @Override
  public PCollection<TSDataPoint> expand(PCollection<TSDataPoint> input) {
    return input.apply(ParDo.of(new VerifyTSDataPoint()));
  }

  private static class VerifyTSDataPoint extends DoFn<TSDataPoint, TSDataPoint> {

    @ProcessElement
    public void process(@Element TSDataPoint input, OutputReceiver<TSDataPoint> o) {

      if (!(input.hasData() && CommonUtils.hasData(input.getData()))) {
        throw new IllegalStateException(
            String.format("TSDataPoint has no data. %s", input.toString()));
      }

      // Categorical values are not yet supported.
      if (!(input.getData().getCategoricalVal().isEmpty())) {
        throw new IllegalStateException(
            String.format("Categorical Values are not yet supported. %s", input.toString()));
      }

      if (!(input.hasKey())) {
        throw new IllegalStateException(
            String.format("TSDataPoint has no key. %s", input.toString()));
      }

      if (!(input.hasTimestamp())) {
        throw new IllegalStateException(
            String.format("TSDataPoint has no timestamp. %s", input.toString()));
      }

      o.output(input);
    }
  }
}

@@ -1229,26 +1229,118 @@
 sdk.
-values.PCollection
+transforms.windowing.GlobalWindow;%0Aimport org.apache.beam.sdk.values.PCollection;%0Aimport org.joda.time.Instant
 ;%0A%0A/
@@ -1808,16 +1808,41 @@
 t input,
+ @Timestamp Instant time,
  OutputR
@@ -2551,32 +2551,32 @@
 StateException(%0A
-
             Stri
@@ -2646,24 +2646,343 @@
 );%0A      %7D%0A%0A
+      if (!(time.isAfter(GlobalWindow.TIMESTAMP_MIN_VALUE) && time.isBefore(GlobalWindow.TIMESTAMP_MAX_VALUE))) %7B%0A        throw new IllegalStateException(%0A                String.format(%22If in Batch mode you must use outputWithTimestamp(), TSDataPoints cannot start in the global window %25s%22, time.toString()));%0A      %7D%0A%0A
       o.outp

Verify in batch mode that outputWithTimestamp() is used