text:
package org.intellij.plugins.xsltDebugger.rt.engine.remote;

import org.intellij.plugins.xsltDebugger.rt.engine.*;

import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

public class RemoteDebuggerClient implements Debugger {
  private final RemoteDebugger myRemote;
  private final BreakpointManager myBreakpointManager;
  private final OutputEventQueue myEventQueue;

  public RemoteDebuggerClient(int port) throws IOException, NotBoundException {
    myRemote = (RemoteDebugger)Naming.lookup("rmi://127.0.0.1:" + port + "/XsltDebugger");

    final RemoteBreakpointManager manager = myRemote.getBreakpointManager();
    myBreakpointManager = new MyBreakpointManager(manager);

    final RemoteDebugger.EventQueue eventQueue = myRemote.getEventQueue();
    myEventQueue = new MyOutputEventQueue(eventQueue);
  }

  public boolean ping() {
    try {
      return myRemote.ping();
    } catch (RemoteException e) {
      return false;
    }
  }

  public State getState() {
    try {
      return myRemote.getState();
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public void stop(boolean force) {
    try {
      myRemote.stop(force);
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  static RuntimeException handleRemoteException(RemoteException e) {
    Throwable t = e.getCause();
    while (t != null) {
      if (t instanceof SocketException || t instanceof EOFException) {
        throw new DebuggerStoppedException();
      }
      t = t.getCause();
    }
    return e.getCause() instanceof RuntimeException ? (RuntimeException)e.getCause() : new RuntimeException(e);
  }

  public Debugger.State waitForStateChange(State state) {
    try {
      return myRemote.waitForStateChange(state);
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public boolean waitForDebuggee() {
    try {
      return myRemote.waitForDebuggee();
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public boolean start() {
    try {
      return myRemote.start();
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public void step() {
    try {
      myRemote.step();
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public void stepInto() {
    try {
      myRemote.stepInto();
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public void resume() {
    try {
      myRemote.resume();
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public void pause() {
    try {
      myRemote.pause();
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public boolean isStopped() {
    try {
      return myRemote.isStopped();
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public StyleFrame getCurrentFrame() {
    try {
      return MyFrame.create(myRemote.getCurrentFrame());
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public SourceFrame getSourceFrame() {
    try {
      return MySourceFrame.create(myRemote.getSourceFrame());
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public Value eval(String expr) throws EvaluationException {
    try {
      return myRemote.eval(expr);
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public List<Variable> getGlobalVariables() {
    try {
      return MyVariable.convert(myRemote.getGlobalVariables());
    } catch (RemoteException e) {
      throw handleRemoteException(e);
    }
  }

  public BreakpointManager getBreakpointManager() {
    return myBreakpointManager;
  }

  public OutputEventQueue getEventQueue() {
    return myEventQueue;
  }

  private static class MyBreakpointManager implements BreakpointManager {
    private final RemoteBreakpointManager myManager;

    public MyBreakpointManager(RemoteBreakpointManager manager) {
      myManager = manager;
    }

    public Breakpoint setBreakpoint(File file, int line) {
      try {
        return new MyBreakpoint(myManager.setBreakpoint(file, line));
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public Breakpoint setBreakpoint(String uri, int line) {
      try {
        return new MyBreakpoint(myManager.setBreakpoint(uri, line));
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public void removeBreakpoint(Breakpoint bp) {
      try {
        myManager.removeBreakpoint(bp.getUri(), bp.getLine());
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public void removeBreakpoint(String uri, int line) {
      try {
        myManager.removeBreakpoint(uri, line);
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public List<Breakpoint> getBreakpoints() {
      try {
        final List<RemoteBreakpoint> list = myManager.getBreakpoints();
        final ArrayList<Breakpoint> breakpoints = new ArrayList<Breakpoint>(list.size());
        for (RemoteBreakpoint breakpoint : list) {
          breakpoints.add(new MyBreakpoint(breakpoint));
        }
        return breakpoints;
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public Breakpoint getBreakpoint(String uri, int lineNumber) {
      try {
        final RemoteBreakpoint breakpoint = myManager.getBreakpoint(uri, lineNumber);
        return breakpoint != null ? new MyBreakpoint(breakpoint) : null;
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    private static class MyBreakpoint implements Breakpoint {
      private final RemoteBreakpoint myBreakpoint;

      public MyBreakpoint(RemoteBreakpoint breakpoint) {
        myBreakpoint = breakpoint;
      }

      public String getUri() {
        try {
          return myBreakpoint.getUri();
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public int getLine() {
        try {
          return myBreakpoint.getLine();
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public boolean isEnabled() {
        try {
          return myBreakpoint.isEnabled();
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public String getCondition() {
        try {
          return myBreakpoint.getCondition();
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public String getLogMessage() {
        try {
          return myBreakpoint.getLogMessage();
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public void setCondition(String expr) {
        try {
          myBreakpoint.setCondition(expr);
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public void setEnabled(boolean enabled) {
        try {
          myBreakpoint.setEnabled(enabled);
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public void setLogMessage(String expr) {
        try {
          myBreakpoint.setLogMessage(expr);
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public String getTraceMessage() {
        try {
          return myBreakpoint.getTraceMessage();
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public void setTraceMessage(String expr) {
        try {
          myBreakpoint.setTraceMessage(expr);
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public boolean isSuspend() {
        try {
          return myBreakpoint.isSuspend();
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }

      public void setSuspend(boolean suspend) {
        try {
          myBreakpoint.setSuspend(suspend);
        } catch (RemoteException e) {
          throw handleRemoteException(e);
        }
      }
    }
  }

  private static abstract class MyAbstractFrame<F extends Frame> implements Frame<F> {
    protected final RemoteDebugger.Frame myFrame;

    protected MyAbstractFrame(RemoteDebugger.Frame frame) {
      myFrame = frame;
    }

    public int getLineNumber() {
      try {
        return myFrame.getLineNumber();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public String getURI() {
      try {
        return myFrame.getURI();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public F getNext() {
      try {
        return createImpl(myFrame.getNext());
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public F getPrevious() {
      try {
        return createImpl(myFrame.getPrevious());
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    protected abstract F createImpl(RemoteDebugger.Frame frame);

    public String getXPath() {
      try {
        return myFrame.getXPath();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public Value eval(String expr) throws EvaluationException {
      try {
        return myFrame.eval(expr);
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public List<Variable> getVariables() {
      try {
        return MyVariable.convert(myFrame.getVariables());
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }
  }

  private static class MyFrame extends MyAbstractFrame<StyleFrame> implements StyleFrame {
    protected MyFrame(RemoteDebugger.Frame frame) {
      super(frame);
    }

    public String getInstruction() {
      try {
        return myFrame.getInstruction();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    protected StyleFrame createImpl(RemoteDebugger.Frame frame) {
      return create(frame);
    }

    public static StyleFrame create(RemoteDebugger.Frame currentFrame) {
      return currentFrame != null ? new MyFrame(currentFrame) : null;
    }
  }

  private static class MySourceFrame extends MyAbstractFrame<SourceFrame> implements SourceFrame {
    protected MySourceFrame(RemoteDebugger.Frame frame) {
      super(frame);
    }

    public SourceFrame createImpl(RemoteDebugger.Frame frame) {
      return create(frame);
    }

    public static SourceFrame create(RemoteDebugger.Frame currentFrame) {
      return currentFrame != null ? new MySourceFrame(currentFrame) : null;
    }
  }

  private static class MyVariable implements Variable {
    private final RemoteDebugger.Variable myVariable;

    public MyVariable(RemoteDebugger.Variable variable) {
      myVariable = variable;
    }

    public String getURI() {
      try {
        return myVariable.getURI();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public int getLineNumber() {
      try {
        return myVariable.getLineNumber();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public boolean isGlobal() {
      try {
        return myVariable.isGlobal();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public Kind getKind() {
      try {
        return myVariable.getKind();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public String getName() {
      try {
        return myVariable.getName();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public Value getValue() {
      try {
        return myVariable.getValue();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    static List<Variable> convert(List<RemoteDebugger.Variable> list) {
      final ArrayList<Variable> variables = new ArrayList<Variable>(list.size());
      for (final RemoteDebugger.Variable variable : list) {
        variables.add(new MyVariable(variable));
      }
      return variables;
    }
  }

  private static class MyOutputEventQueue implements OutputEventQueue {
    private final RemoteDebugger.EventQueue myEventQueue;

    public MyOutputEventQueue(RemoteDebugger.EventQueue eventQueue) {
      myEventQueue = eventQueue;
    }

    public void setEnabled(boolean b) {
      try {
        myEventQueue.setEnabled(b);
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }

    public List<NodeEvent> getEvents() {
      try {
        return myEventQueue.getEvents();
      } catch (RemoteException e) {
        throw handleRemoteException(e);
      }
    }
  }
}


text:
"""Tests for Keras subclassed layers utilizing desired user syntax."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python import keras
from tensorflow.python.eager import context
from tensorflow.python.framework import ops
from tensorflow.python.keras import keras_parameterized
from tensorflow.python.keras import testing_utils
from tensorflow.python.keras.utils import tf_utils
from tensorflow.python.ops import variables
from tensorflow.python.platform import test

@keras_parameterized.run_all_keras_modes
@keras_parameterized.run_with_all_model_types
class SubclassedLayersTest(keras_parameterized.TestCase):

  def test_simple_build_with_constant(self):

    class BuildConstantLayer(keras.layers.Layer):

      def build(self, input_shape):
        self.b = ops.convert_to_tensor_v2_with_dispatch(2.0)

      def call(self, inputs):
        return self.b * inputs

    layer = BuildConstantLayer()
    model = testing_utils.get_model_from_layers(
        [layer, keras.layers.Dense(1)], input_shape=(1,))

    x = ops.convert_to_tensor_v2_with_dispatch([[3.0]])
    self.assertEqual(
        tf_utils.is_symbolic_tensor(model(x)), not context.executing_eagerly())
    self.assertEqual(
        tf_utils.is_symbolic_tensor(layer(x)), not context.executing_eagerly())
    self.assertAllClose(keras.backend.get_value(layer(x)), [[6.0]])

  def test_build_with_derived_constant(self):

    class BuildDerivedConstantLayer(keras.layers.Layer):

      def build(self, input_shape):
        a = ops.convert_to_tensor_v2_with_dispatch(1.0)
        b = 2.0 * a
        self.variable = variables.Variable(b)
        self.constant = ops.convert_to_tensor_v2_with_dispatch(self.variable)

      def call(self, inputs):
        return self.variable * self.constant * inputs

    layer = BuildDerivedConstantLayer()
    model = testing_utils.get_model_from_layers(
        [layer, keras.layers.Dense(1)], input_shape=(1,))

    x = ops.convert_to_tensor_v2_with_dispatch([[3.0]])
    self.assertEqual(
        tf_utils.is_symbolic_tensor(model(x)), not context.executing_eagerly())
    self.assertEqual(
        tf_utils.is_symbolic_tensor(layer(x)), not context.executing_eagerly())
    self.assertAllClose(keras.backend.get_value(layer(x)), [[12.0]])

if __name__ == '__main__':
  test.main()

redpajama_set_name: RedPajamaGithub


text:
import { Component } from '@angular/core';

@Component({
  selector: 'ocio-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent {

  constructor() { }

}


text:
package com.linkedin.pinot.controller.helix.core.util;

import com.google.common.io.Files;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.helix.HelixAdmin;
import org.apache.helix.ZNRecord;
import org.apache.helix.model.ExternalView;
import org.apache.helix.model.IdealState;
import org.apache.helix.store.zk.ZkHelixPropertyStore;
import org.joda.time.DateTime;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.testng.annotations.Test;
import com.linkedin.pinot.controller.helix.core.SegmentDeletionManager;
import junit.framework.Assert;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class SegmentDeletionManagerTest {
  final static String tableName = "table";
  final static String clusterName = "mock";

  HelixAdmin makeHelixAdmin() {
    HelixAdmin admin = mock(HelixAdmin.class);
    ExternalView ev = mock(ExternalView.class);
    IdealState is = mock(IdealState.class);
    when(admin.getResourceExternalView(clusterName, tableName)).thenReturn(ev);
    when(admin.getResourceIdealState(clusterName, tableName)).thenReturn(is);

    List<String> segmentsInIs = segmentsInIdealStateOrExtView();
    Map<String, String> dummy = new HashMap<>(1);
    dummy.put("someHost", "ONLINE");
    for (String segment : segmentsInIs) {
      when(is.getInstanceStateMap(segment)).thenReturn(dummy);
    }
    when(ev.getStateMap(anyString())).thenReturn(null);

    return admin;
  }

  ZkHelixPropertyStore<ZNRecord> makePropertyStore() {
    ZkHelixPropertyStore store = mock(ZkHelixPropertyStore.class);
    final List<String> failedSegs = segmentsFailingPropStore();
    /*
    for (String segment : failedSegs) {
      String propStorePath = ZKMetadataProvider.constructPropertyStorePathForSegment(tableName, segment);
      when(store.remove(propStorePath, AccessOption.PERSISTENT)).thenReturn(false);
    }
    List<String> successfulSegs = segmentsThatShouldBeDeleted();
    for (String segment : successfulSegs) {
      String propStorePath = ZKMetadataProvider.constructPropertyStorePathForSegment(tableName, segment);
      when(store.remove(propStorePath, AccessOption.PERSISTENT)).thenReturn(true);
    }
    */
    when(store.remove(anyList(), anyInt())).thenAnswer(new Answer<boolean[]>() {
      @Override
      public boolean[] answer(InvocationOnMock invocationOnMock)
          throws Throwable {
        List<String> propStoreList = (List<String>) (invocationOnMock.getArguments()[0]);
        boolean[] result = new boolean[propStoreList.size()];
        for (int i = 0; i < result.length; i++) {
          final String path = propStoreList.get(i);
          final String segmentId = path.substring((path.lastIndexOf('/') + 1));
          if (failedSegs.indexOf(segmentId) < 0) {
            result[i] = true;
          } else {
            result[i] = false;
          }
        }
        return result;
      }
    });

        when(store.exists(anyString(), anyInt())).thenReturn(true);
    return store;
  }

  List<String> segmentsThatShouldBeDeleted() {
    List<String> result = new ArrayList<>(3);
    result.add("seg1");
    result.add("seg2");
    result.add("seg3");
    return result;
  }

  List<String> segmentsInIdealStateOrExtView() {
    List<String> result = new ArrayList<>(3);
    result.add("seg11");
    result.add("seg12");
    result.add("seg13");
    return result;
  }

  List<String> segmentsFailingPropStore() {
    List<String> result = new ArrayList<>(3);
    result.add("seg21");
    result.add("seg22");
    result.add("seg23");
    return result;
  }

  @Test
  public void testBulkDeleteWithFailures() throws Exception {
    testBulkDeleteWithFailures(true);
    testBulkDeleteWithFailures(false);
  }

  public void testBulkDeleteWithFailures(boolean useSet) throws Exception {
    HelixAdmin helixAdmin =  makeHelixAdmin();
    ZkHelixPropertyStore<ZNRecord> propertyStore = makePropertyStore();
    FakeDeletionManager deletionManager = new FakeDeletionManager(helixAdmin, propertyStore);
    Collection<String> segments;
    if (useSet) {
      segments = new HashSet<String>();
    } else {
      segments = new ArrayList<String>();
    }

    segments.addAll(segmentsThatShouldBeDeleted());
    segments.addAll(segmentsInIdealStateOrExtView());
    segments.addAll(segmentsFailingPropStore());
    deletionManager.deleteSegmentsFromPropertyStoreAndLocal(tableName, segments);

    Assert.assertTrue(deletionManager.segmentsToRetry.containsAll(segmentsFailingPropStore()));
    Assert.assertTrue(deletionManager.segmentsToRetry.containsAll(segmentsInIdealStateOrExtView()));
    Assert.assertTrue(deletionManager.segmentsRemovedFromStore.containsAll(segmentsThatShouldBeDeleted()));
  }

  @Test
  public void testAllFailed() throws Exception {
    testAllFailed(segmentsFailingPropStore());
    testAllFailed(segmentsInIdealStateOrExtView());
    List<String> segments = segmentsFailingPropStore();
    segments.addAll(segmentsInIdealStateOrExtView());
    testAllFailed(segments);
  }

  @Test
  public void allPassed() throws Exception {
    HelixAdmin helixAdmin = makeHelixAdmin();
    ZkHelixPropertyStore<ZNRecord> propertyStore = makePropertyStore();
    FakeDeletionManager deletionManager = new FakeDeletionManager(helixAdmin, propertyStore);
    Set<String> segments = new HashSet<>();
    segments.addAll(segmentsThatShouldBeDeleted());
    deletionManager.deleteSegmentsFromPropertyStoreAndLocal(tableName, segments);

    Assert.assertEquals(deletionManager.segmentsToRetry.size(), 0);
    Assert.assertEquals(deletionManager.segmentsRemovedFromStore.size(), segments.size());
    Assert.assertTrue(deletionManager.segmentsRemovedFromStore.containsAll(segments));
  }

  private void testAllFailed(List<String> segments) throws Exception {
    HelixAdmin helixAdmin =  makeHelixAdmin();
    ZkHelixPropertyStore<ZNRecord> propertyStore = makePropertyStore();
    FakeDeletionManager deletionManager = new FakeDeletionManager(helixAdmin, propertyStore);
    deletionManager.deleteSegmentsFromPropertyStoreAndLocal(tableName, segments);

    Assert.assertTrue(deletionManager.segmentsToRetry.containsAll(segments));
    Assert.assertEquals(deletionManager.segmentsToRetry.size(), segments.size());
    Assert.assertEquals(deletionManager.segmentsRemovedFromStore.size(), 0);
  }

  @Test
  public void testRemoveDeletedSegments() throws Exception {
    HelixAdmin helixAdmin = makeHelixAdmin();
    ZkHelixPropertyStore<ZNRecord> propertyStore = makePropertyStore();
    File tempDir = Files.createTempDir();
    tempDir.deleteOnExit();
    FakeDeletionManager deletionManager = new FakeDeletionManager(tempDir.getAbsolutePath(), helixAdmin, propertyStore);

    // Test delete when deleted segments directory does not exists
    deletionManager.removeAgedDeletedSegments(1);

    // Create deleted directory
    String deletedDirectoryPath = tempDir + File.separator + "Deleted_Segments";
    File deletedDirectory = new File(deletedDirectoryPath);
    deletedDirectory.mkdir();

    // Test delete when deleted segments directory is empty
    deletionManager.removeAgedDeletedSegments(1);

    // Create dummy directories and files
    File dummyDir1 = new File(deletedDirectoryPath + File.separator + "dummy1");
    dummyDir1.mkdir();
    File dummyDir2 = new File(deletedDirectoryPath + File.separator + "dummy2");
    dummyDir2.mkdir();

    // Test delete when there is no files but some directories exist
    deletionManager.removeAgedDeletedSegments(1);
    Assert.assertEquals(dummyDir1.exists(), false);
    Assert.assertEquals(dummyDir2.exists(), false);

    // Create dummy directories and files
    dummyDir1.mkdir();
    dummyDir2.mkdir();

    // Create dummy files
    for (int i = 0; i < 3; i++) {
      createTestFileWithAge(dummyDir1.getAbsolutePath() + File.separator + "file" + i, i);
    }
    for (int i = 2; i < 5; i++) {
      createTestFileWithAge(dummyDir2.getAbsolutePath() + File.separator +"file" + i, i);
    }

    // Check that dummy directories and files are successfully created.
    Assert.assertEquals(dummyDir1.list().length, 3);
    Assert.assertEquals(dummyDir2.list().length, 3);

    // Try to remove files with the retention of 3 days.
    deletionManager.removeAgedDeletedSegments(3);
    Assert.assertEquals(dummyDir1.list().length, 3);
    Assert.assertEquals(dummyDir2.list().length, 1);

    // Try to further remove files with the retention of 1 days.
    deletionManager.removeAgedDeletedSegments(1);
    Assert.assertEquals(dummyDir1.list().length, 1);

    // Check that empty directory has successfully been removed.
    Assert.assertEquals(dummyDir2.exists(), false);
  }

  public void createTestFileWithAge(String path, int age) throws Exception {
    File testFile = new File(path);
    testFile.createNewFile();
    testFile.setLastModified(DateTime.now().minusDays(age).getMillis());
  }

  public static class FakeDeletionManager extends SegmentDeletionManager {

    public Set<String> segmentsRemovedFromStore = new HashSet<>();
    public Set<String> segmentsToRetry = new HashSet<>();

    FakeDeletionManager(HelixAdmin helixAdmin, ZkHelixPropertyStore<ZNRecord> propertyStore) {
      super(null, helixAdmin, clusterName, propertyStore);
    }

    FakeDeletionManager(String localDiskDir, HelixAdmin helixAdmin, ZkHelixPropertyStore<ZNRecord> propertyStore) {
      super(localDiskDir, helixAdmin, clusterName, propertyStore);
    }

    public void deleteSegmentsFromPropertyStoreAndLocal(String tableName, Collection<String> segments) {
      super.deleteSegmentFromPropertyStoreAndLocal(tableName, segments, 0L);
    }

    @Override
    protected void removeSegmentFromStore(String tableName, String segmentId) {
      segmentsRemovedFromStore.add(segmentId);
    }
    @Override
    protected void deleteSegmentsWithDelay(final String tableName, final Collection<String> segmentIds,
        final long deletionDelaySeconds) {
      segmentsToRetry.addAll(segmentIds);
    }
  }
}


text:
package org.tuckey.web.filters.urlrewrite.functions;

import org.tuckey.web.filters.urlrewrite.substitution.SubstitutionContext;
import org.tuckey.web.filters.urlrewrite.substitution.SubstitutionFilterChain;
import org.tuckey.web.filters.urlrewrite.utils.Log;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.regex.Pattern;

public class StringFunctions {

    private static Log log = Log.getLog(StringFunctions.class);
    private static final Pattern FIND_COLON_PATTERN = Pattern.compile("(?<!\\\\):");
    private static final Pattern FIND_ENCODING_PATTERN = Pattern.compile("^[0-9a-zA-Z-]+:");

    public static String toLower(final String s, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        return s == null ? null : nextFilter.substitute(s, ctx).toLowerCase();
    }

    public static String toUpper(final String s, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        return s == null ? null : nextFilter.substitute(s, ctx).toUpperCase();
    }

    public static String trim(final String str, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        if (str == null) {
            return null;
        }
        return nextFilter.substitute(str, ctx).trim();
    }

    public static String length(final String str, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        if (str == null) {
            return "0";
        }
        return String.valueOf(nextFilter.substitute(str, ctx).length());
    }

    /**
     * escape query string "as as" will return "as+as"
     * note, encoding can be specified after colon eg, "as:UTF-16"
     *
     * @param ctx
     * @param nextFilter
     */
    public static String escape(String subject, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        String encoding = "UTF-8";
        if (FIND_ENCODING_PATTERN.matcher(subject).find()) {
            encoding = subject.substring(0, subject.indexOf(':'));
            subject = subject.substring(subject.indexOf(':') + 1);
            if (!Charset.isSupported(encoding)) encoding = "UTF-8";
        }
        subject = nextFilter.substitute(subject, ctx);
        try {
            return URLEncoder.encode(subject, encoding);
        } catch (UnsupportedEncodingException e) {
            log.error(e, e);
        }
        return "";
    }

    /**
     * escape string as a URI path segment "as as" will return "as%20as"
     * note, encoding can be specified after colon eg, "as:UTF-16"
     *
     * @param nextFilter
     * @param ctx
     */
    public static String escapePath(String subject, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        String encoding = "UTF-8";
        if (FIND_ENCODING_PATTERN.matcher(subject).find()) {
            encoding = subject.substring(0, subject.indexOf(':'));
            subject = subject.substring(subject.indexOf(':') + 1);
            if (!Charset.isSupported(encoding)) encoding = "UTF-8";
        }
        subject = nextFilter.substitute(subject, ctx);
        try {
            return org.tuckey.web.filters.urlrewrite.utils.URLEncoder.encodePathSegment(subject, encoding);
        } catch (UnsupportedEncodingException e) {
            log.error(e, e);
        }
        return "";
    }

    /**
     * unescape query string "as+as" will return "as as"
     * note, encoding can be specified after colon eg, "as:UTF-16"
     *
     * @param nextFilter
     * @param ctx
     */
    public static String unescape(String subject, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        String encoding = "UTF-8";
        if (FIND_ENCODING_PATTERN.matcher(subject).find()) {
            encoding = subject.substring(0, subject.indexOf(':'));
            subject = subject.substring(subject.indexOf(':') + 1);
            if (!Charset.isSupported(encoding)) encoding = "UTF-8";
        }
        subject = nextFilter.substitute(subject, ctx);
        try {
            return URLDecoder.decode(subject, encoding);
        } catch (UnsupportedEncodingException e) {
            log.error(e, e);
        }
        return "";
    }

    /**
     * unescape path segment string "as+as%20as" will return "as+as as"
     * note, encoding can be specified after colon eg, "as:UTF-16"
     *
     * @param nextFilter
     * @param ctx
     */
    public static String unescapePath(String subject, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        String encoding = "UTF-8";
        if (FIND_ENCODING_PATTERN.matcher(subject).find()) {
            encoding = subject.substring(0, subject.indexOf(':'));
            subject = subject.substring(subject.indexOf(':') + 1);
            if (!Charset.isSupported(encoding)) encoding = "UTF-8";
        }
        subject = nextFilter.substitute(subject, ctx);
        try {
            return org.tuckey.web.filters.urlrewrite.utils.URLDecoder.decodePath(subject, encoding);
        } catch (URISyntaxException e) {
            log.error(e, e);
        }
        return "";
    }

    public static String replaceAll(String subject, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        String replace = "";
        String with = "";
        if (FIND_COLON_PATTERN.matcher(subject).find()) {
            replace = subject.substring(subject.indexOf(':') + 1);
            subject = subject.substring(0, subject.indexOf(':'));
            if (FIND_COLON_PATTERN.matcher(replace).find()) {
                with = replace.substring(replace.indexOf(':') + 1);
                replace = replace.substring(0, replace.indexOf(':'));
            }
        }
        subject = nextFilter.substitute(subject, ctx);
        return subject.replaceAll(replace, with);
    }

    public static String replaceFirst(String subject, SubstitutionFilterChain nextFilter, SubstitutionContext ctx) {
        String replace = "";
        String with = "";
        if (FIND_COLON_PATTERN.matcher(subject).find()) {
            replace = subject.substring(subject.indexOf(':') + 1);
            subject = subject.substring(0, subject.indexOf(':'));
            if (FIND_COLON_PATTERN.matcher(replace).find()) {
                with = replace.substring(replace.indexOf(':') + 1);
                replace = replace.substring(0, replace.indexOf(':'));
            }
        }
        subject = nextFilter.substitute(subject, ctx);
        return subject.replaceFirst(replace, with);
    }

}


text:
<?xml version="1.0" encoding="UTF-8"?>
<testcase xmlns="http://xlt.xceptance.com/xlt-script/2" version="6">
  <tags>qv,quickview,navigation</tags>
  <description>Verifies product navigation on quickview

# 1. Setup and preparation

- start session, open homepage and delete all visible cookies
- search products
- refine by color
- sort by randomly

# 2. Scope of test
- store some product positions and names used for validation of navigation
- open quickview for first product
- navigate to next quickview product
- close quickview
- open quickview for last product
- navigate to previous product
</description>
  <codecomment># Setup - Start Session</codecomment>
  <module name="modules.helper.FHelper_Session_flowToStartSession"/>
  <action name="SearchProducts"/>
  <module name="modules.pages.search.MSearch_enterTextAndSubmit">
    <parameter name="searchText" value="${searchText}"/>
  </module>
  <action name="RefineColorByLabel"/>
  <module name="modules.pages.catalog.MCatalog_Refinement_clickColorByLabel">
    <parameter name="colorLabel" value="${colorLabel}"/>
    <parameter name="breadcrumbRefinementValueCount" value="1"/>
  </module>
  <action name="SelectSortByViaRandom"/>
  <module name="modules.pages.catalog.MCatalog_Option_selectSortByRandomly">
    <parameter name="sortByLabel_varName" value="sortByLabel_random"/>
  </module>
  <codecomment># Scope</codecomment>
  <codecomment>store product positions of last and before last from search result items</codecomment>
  <command name="storeElementCount" target="css=#search-result-items .grid-tile" value="positionLastProduct_varDynamic"/>
  <command name="storeEval" target="${positionLastProduct_varDynamic}-1" value="positionLastBeforeProduct_varDynamic"/>
  <codecomment>store product names of first, second, last and before last</codecomment>
  <command name="storeText" target="css=#search-result-items .grid-tile:nth-of-type(1) .product-name" value="productNameFirst_varDynamic"/>
  <command name="storeText" target="css=#search-result-items .grid-tile:nth-of-type(2) .product-name" value="productNameSecond_varDynamic"/>
  <command name="storeText" target="css=#search-result-items .grid-tile:nth-of-type(${positionLastProduct_varDynamic}) .product-name" value="productNameLast_varDynamic"/>
  <command name="storeText" target="css=#search-result-items .grid-tile:nth-of-type(${positionLastBeforeProduct_varDynamic}) .product-name" value="productNameLastBefore_varDynamic"/>
  <action name="ProductQuickView"/>
  <codecomment>open quickview first product, validate name and button states</codecomment>
  <module name="modules.pages.catalog.MCatalog_clickQuickViewByProductPosition">
    <parameter name="position" value="1"/>
  </module>
  <command name="waitForText" target="css=#pdpMain .product-name" value="${productNameFirst_varDynamic}"/>
  <module name="modules.pages.product.productquickview.VProductQV_validateHeader">
    <parameter name="prevButtonState" value="[disabled='disabled']"/>
    <parameter name="nextButtonState" value=":not([disabled])"/>
  </module>
  <action name="ProductQuickViewNext"/>
  <codecomment>navigate to next quickview product, validate name and button states</codecomment>
  <module name="modules.pages.product.productquickview.MProductQV_clickNext"/>
  <command name="waitForText" target="css=#pdpMain .product-name" value="${productNameSecond_varDynamic}"/>
  <module name="modules.pages.product.productquickview.VProductQV_validateHeader">
    <parameter name="prevButtonState" value=":not([disabled])"/>
    <parameter name="nextButtonState" value=":not([disabled])"/>
  </module>
  <action name="ProductQuickViewClose"/>
  <codecomment>close quickview</codecomment>
  <module name="modules.pages.product.productquickview.MProductQV_closeQuickview"/>
  <action name="ProductQuickView"/>
  <codecomment>open quickview last product, validate name and button states</codecomment>
  <module name="modules.pages.catalog.MCatalog_clickQuickViewByProductPosition">
    <parameter name="position" value="${positionLastProduct_varDynamic}"/>
  </module>
  <command name="waitForText" target="css=#pdpMain .product-name" value="${productNameLast_varDynamic}"/>
  <module name="modules.pages.product.productquickview.VProductQV_validateHeader">
    <parameter name="prevButtonState" value=":not([disabled])"/>
    <parameter name="nextButtonState" value="[disabled='disabled']"/>
  </module>
  <action name="ProductQuickViewPrevious"/>
  <codecomment>open quickview previous product, validate name and button states</codecomment>
  <module name="modules.pages.product.productquickview.MProductQV_clickPrev"/>
  <command name="waitForText" target="css=#pdpMain .product-name" value="${productNameLastBefore_varDynamic}"/>
  <module name="modules.pages.product.productquickview.VProductQV_validateHeader">
    <parameter name="prevButtonState" value=":not([disabled])"/>
    <parameter name="nextButtonState" value=":not([disabled])"/>
  </module>
</testcase>


text:
// Code generated by protoc-gen-go.
// source: backend/consensus.proto
// DO NOT EDIT!

/*
Package backend is a generated protocol buffer package.

It is generated from these files:
	backend/consensus.proto

It has these top-level messages:
	Handshake
*/
package backend

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import simplebft "github.com/hyperledger/fabric/orderer/sbft/simplebft"

import (
	context "golang.org/x/net/context"
	grpc "google.golang.org/grpc"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package

type Handshake struct {
}

func (m *Handshake) Reset()                    { *m = Handshake{} }
func (m *Handshake) String() string            { return proto.CompactTextString(m) }
func (*Handshake) ProtoMessage()               {}
func (*Handshake) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }

func init() {
	proto.RegisterType((*Handshake)(nil), "backend.handshake")
}

// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn

// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion3

// Client API for Consensus service

type ConsensusClient interface {
	Consensus(ctx context.Context, in *Handshake, opts ...grpc.CallOption) (Consensus_ConsensusClient, error)
}

type consensusClient struct {
	cc *grpc.ClientConn
}

func NewConsensusClient(cc *grpc.ClientConn) ConsensusClient {
	return &consensusClient{cc}
}

func (c *consensusClient) Consensus(ctx context.Context, in *Handshake, opts ...grpc.CallOption) (Consensus_ConsensusClient, error) {
	stream, err := grpc.NewClientStream(ctx, &_Consensus_serviceDesc.Streams[0], c.cc, "/backend.consensus/consensus", opts...)
	if err != nil {
		return nil, err
	}
	x := &consensusConsensusClient{stream}
	if err := x.ClientStream.SendMsg(in); err != nil {
		return nil, err
	}
	if err := x.ClientStream.CloseSend(); err != nil {
		return nil, err
	}
	return x, nil
}

type Consensus_ConsensusClient interface {
	Recv() (*simplebft.MultiChainMsg, error)
	grpc.ClientStream
}

type consensusConsensusClient struct {
	grpc.ClientStream
}

func (x *consensusConsensusClient) Recv() (*simplebft.MultiChainMsg, error) {
	m := new(simplebft.MultiChainMsg)
	if err := x.ClientStream.RecvMsg(m); err != nil {
		return nil, err
	}
	return m, nil
}

// Server API for Consensus service

type ConsensusServer interface {
	Consensus(*Handshake, Consensus_ConsensusServer) error
}

func RegisterConsensusServer(s *grpc.Server, srv ConsensusServer) {
	s.RegisterService(&_Consensus_serviceDesc, srv)
}

func _Consensus_Consensus_Handler(srv interface{}, stream grpc.ServerStream) error {
	m := new(Handshake)
	if err := stream.RecvMsg(m); err != nil {
		return err
	}
	return srv.(ConsensusServer).Consensus(m, &consensusConsensusServer{stream})
}

type Consensus_ConsensusServer interface {
	Send(*simplebft.MultiChainMsg) error
	grpc.ServerStream
}

type consensusConsensusServer struct {
	grpc.ServerStream
}

func (x *consensusConsensusServer) Send(m *simplebft.MultiChainMsg) error {
	return x.ServerStream.SendMsg(m)
}

var _Consensus_serviceDesc = grpc.ServiceDesc{
	ServiceName: "backend.consensus",
	HandlerType: (*ConsensusServer)(nil),
	Methods:     []grpc.MethodDesc{},
	Streams: []grpc.StreamDesc{
		{
			StreamName:    "consensus",
			Handler:       _Consensus_Consensus_Handler,
			ServerStreams: true,
		},
	},
	Metadata: fileDescriptor0,
}

func init() { proto.RegisterFile("backend/consensus.proto", fileDescriptor0) }

var fileDescriptor0 = []byte{
	// 172 bytes of a gzipped FileDescriptorProto
	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x4c, 0xce, 0xbd, 0xae, 0x82, 0x40,
	0x10, 0x05, 0xe0, 0x7b, 0x1b, 0x0d, 0xd8, 0xd1, 0xa8, 0x94, 0x3c, 0xc0, 0x8e, 0x41, 0x5b, 0x1b,
	0xed, 0x4c, 0x78, 0x01, 0xbb, 0xfd, 0x19, 0xd8, 0x0d, 0xb0, 0x4b, 0x66, 0x96, 0xc2, 0xb7, 0x37,
	0x2a, 0x59, 0xed, 0x66, 0x72, 0x4e, 0xbe, 0x9c, 0x7c, 0xab, 0xa4, 0xee, 0xd1, 0x1b, 0xd0, 0xc1,
	0x33, 0x7a, 0x9e, 0x59, 0x4c, 0x14, 0x62, 0x28, 0xd6, 0x4b, 0x50, 0xee, 0xd9, 0x8d, 0xd3, 0x80,
	0xaa, 0x8d, 0x90, 0xae, 0x4f, 0xa7, 0xda, 0xe4, 0x99, 0x95, 0xde, 0xb0, 0x95, 0x3d, 0xd6, 0xb7,
	0x3c, 0x4b, 0x46, 0x71, 0xfe, 0x7d, 0x0a, 0xb1, 0x58, 0x22, 0xb5, 0xcb, 0x9d, 0xf8, 0x62, 0xcd,
	0x3c, 0x44, 0x77, 0xb5, 0xd2, 0xf9, 0x86, 0xbb, 0xea, 0xef, 0xf0, 0x7f, 0x39, 0xdd, 0xeb, 0xce,
	0x45, 0x3b, 0x2b, 0xa1, 0xc3, 0x08, 0xf6, 0x31, 0x21, 0x0d, 0x68, 0x3a, 0x24, 0x68, 0xa5, 0x22,
	0xa7, 0x21, 0x90, 0x41, 0x42, 0x02, 0x7e, 0xcd, 0x5a, 0x74, 0xb5, 0x7a, 0xaf, 0x3a, 0x3e, 0x03,
	0x00, 0x00, 0xff, 0xff, 0x1d, 0x79, 0xa9, 0x9a, 0xd4, 0x00, 0x00, 0x00,
}


text:
package org.jivesoftware.spark.ui.conferences;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.resource.Res;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.muc.Affiliate;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.spark.component.renderer.ListIconRenderer;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.rooms.GroupChatRoom;
import org.jivesoftware.spark.util.log.Log;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;

/**
 * Handles unbanning banned users in Chat Rooms.
 */
public class BannedUsers extends JPanel {
    private static final long serialVersionUID = 6422162361752646645L;

    private MultiUserChat chat;

    private DefaultListModel listModel = new DefaultListModel();
    private JList list = new JList(listModel);
    private JMenuItem unBanMenuItem = new JMenuItem(Res.getString("menuitem.unban"));

    /**
     * Construct UI
     */
    public BannedUsers() {
        setLayout(new BorderLayout());
        list.setCellRenderer(new ListIconRenderer());
        add(list, BorderLayout.CENTER);
        // Respond to Double-Click in Agent List to start a chat
        list.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent evt) {
                if (evt.isPopupTrigger()) {
                    int index = list.locationToIndex(evt.getPoint());
                    list.setSelectedIndex(index);
                    ImageIcon icon = (ImageIcon)list.getModel().getElementAt(index);
                    String jid = icon.getDescription();
                    showPopup(evt, jid);
                }
            }

            public void mouseReleased(MouseEvent evt) {
                if (evt.isPopupTrigger()) {
                    int index = list.locationToIndex(evt.getPoint());
                    list.setSelectedIndex(index);
                    ImageIcon icon = (ImageIcon)list.getModel().getElementAt(index);
                    String jid = icon.getDescription();
                    showPopup(evt, jid);
                }
            }
        });

        unBanMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int index = list.getSelectedIndex();
                ImageIcon icon = (ImageIcon)list.getModel().getElementAt(index);
                String jid = icon.getDescription();
                try {
                    chat.grantMembership(jid);
                }
                catch (XMPPException memEx) {
                    Log.error("Error granting membership", memEx);
                }
                listModel.removeElementAt(index);

            }
        });
    }

    /**
     * Binds a ChatRoom to listen to.
     *
     * @param cRoom the group chat room.
     */
    public void setChatRoom(ChatRoom cRoom) {
        GroupChatRoom chatRoom = (GroupChatRoom) cRoom;
        chat = chatRoom.getMultiUserChat();
    }

    /**
     * Loads all banned users in a ChatRoom.
     */
    public void loadAllBannedUsers() {
        // Clear all elements from model
        listModel.clear();

        Iterator<Affiliate> bannedUsers = null;
        try {
            bannedUsers = chat.getOutcasts().iterator();
        }
        catch (XMPPException e) {
            Log.error("Error loading all banned users", e);
        }

        while (bannedUsers != null && bannedUsers.hasNext()) {
            Affiliate bannedUser = bannedUsers.next();
            ImageIcon icon = SparkRes.getImageIcon(SparkRes.STAR_RED_IMAGE);
            icon.setDescription(bannedUser.getJid());
            listModel.addElement(icon);
        }
    }

    /**
     * Responsible for popping up the menu items.
     *
     * @param e   the MouseEvent that triggered it.
     * @param jid the JID to handle.
     */
    private void showPopup(MouseEvent e, String jid) {
        final JPopupMenu popup = new JPopupMenu();
        popup.add(unBanMenuItem);
        popup.show(this, e.getX(), e.getY());
    }
}


text:
using System.Threading.Tasks;
using Nest;
using Tests.Framework;
using static Tests.Framework.UrlTester;

namespace Tests.Indices.Analyze
{
	public class AnalyzeUrlTests
	{
		[U] public async Task Urls()
		{
			var hardcoded = "hardcoded";
			var index = "index";
			await POST($"/{index}/_analyze")
				.Fluent(c=>c.Analyze(a=>a.Text(hardcoded).Index(index)))
				.Request(c=>c.Analyze(new AnalyzeRequest(index, hardcoded)))
				.FluentAsync(c=>c.AnalyzeAsync(a=>a.Text(hardcoded).Index(index)))
				.RequestAsync(c=>c.AnalyzeAsync(new AnalyzeRequest(index, hardcoded)))
				;

			await POST($"/_analyze")
				.Fluent(c=>c.Analyze(a=>a.Text(hardcoded)))
				.Request(c=>c.Analyze(new AnalyzeRequest() { Text = new[] { hardcoded } }))
				.FluentAsync(c=>c.AnalyzeAsync(a=>a.Text(hardcoded)))
				.RequestAsync(c=>c.AnalyzeAsync(new AnalyzeRequest() { Text = new[] { hardcoded } }))
				;

		}
	}
}


text:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appacitive.Sdk.Tests
{
    [TestClass]
    public class EntityPropertyFixture
    {
        [TestMethod]
        public void ItemAddTest()
        {

            APObject obj = new APObject("object");
            obj.SetList<int>("field1", new[] { 1, 2, 3, 4, 5, 6 });
            var field1 = obj.GetList<int>("field1");
            Assert.IsFalse(field1.Contains(10));
            Assert.IsFalse(field1.Contains(11));
            obj.AddItems("field1", 10, 11);
            field1 = obj.GetList<int>("field1");
            Assert.IsTrue(field1.Contains(10));
            Assert.IsTrue(field1.Contains(11));
        }

        [TestMethod]
        public void ItemAddTestWithoutDuplication()
        {
            APObject obj = new APObject("object");
            obj.SetList<int>("field1", new[] { 1, 2, 3, 4, 5, 6 });
            var field1 = obj.GetList<int>("field1");
            Assert.IsTrue(field1.Count() == 6);
            obj.AddItems("field1", false, 5, 6);
            field1 = obj.GetList<int>("field1");
            Assert.IsTrue(field1.Count() == 6);
        }

        [TestMethod]
        public void RemoveFirstOccurenceOfItemTest()
        {
            APObject obj = new APObject("object");
            obj.SetList<int>("field1", new [] { 1,2,3,1,2,3 });
            var removed = obj.RemoveItems("field1", 1, true);
            Assert.IsTrue(removed == true);
            var list = obj.GetList<int>("field1").ToList();
            Assert.IsTrue(list.Count == 5);
            Assert.IsTrue(list.Count(x => x == 1) == 1 );
        }

        [TestMethod]
        public void InvalidAddItemsTest()
        {
            var obj = new APObject("object");
            obj["age"] = 10;
            try
            {
                obj.AddItems("age", 2, 3);
                Assert.Fail("Add item to integer property should have failed.");
            }
            catch (ArgumentException)
            {
            }

        }

        [TestMethod]
        public void InvalidRemoveItemsTest()
        {
            var obj = new APObject("object");
            obj["age"] = 10;
            try
            {
                obj.RemoveItems("age", 2);
                Assert.Fail("Add item to integer property should have failed.");
            }
            catch (ArgumentException)
            {
            }

        }

        [TestMethod]
        public void RemoveAllOccurencesOfItemTest()
        {
            APObject obj = new APObject("object");
            obj.SetList<int>("field1", new[] { 1, 2, 3, 1, 2, 3 });
            var removed = obj.RemoveItems("field1", 1, false);
            Assert.IsTrue(removed == true);
            var list = obj.GetList<int>("field1").ToList();
            Assert.IsTrue(list.Count == 4);
            Assert.IsTrue(list.Count(x => x == 1) == 0);
        }

        [TestMethod]
        public void AddItemsToNullPropertyTest()
        {
            APObject obj = new APObject("object");
            obj.AddItems("field1", 5, 6);
            Assert.IsTrue(obj.GetList<string>("field1").Count() == 2);
        }

        [TestMethod]
        public void ItemAddTestWithDuplication()
        {
            APObject obj = new APObject("object");
            obj.SetList<int>("field1", new[] { 1, 2, 3, 4, 5, 6 });
            var field1 = obj.GetList<int>("field1");
            Assert.IsTrue(field1.Count() == 6);
            obj.AddItems("field1", true, 5, 6);
            field1 = obj.GetList<int>("field1");
            Assert.IsTrue(field1.Count() == 8);
        }

        [TestMethod]
        public void EntityIndexerReadWriteTest()
        {
            dynamic apObject = new APObject("object");
            AssertIndexerGetSet<int>(apObject, "intfield", 10);
            AssertIndexerGetSet<uint>(apObject, "intfield", 10);
            AssertIndexerGetSet<long>(apObject, "intfield", 10);
            AssertIndexerGetSet<float>(apObject, "decimalfield", 10.0f);
            AssertIndexerGetSet<double>(apObject, "decimalfield", 10.0);
            AssertIndexerGetSet<decimal>(apObject, "decimalfield", 10.0m);
            AssertIndexerGetSet<string>(apObject, "strinfield", "test value");
            AssertIndexerGetSet<bool>(apObject, "boolfield", true);
            AssertIndexerGetSet<char>(apObject, "stringfield", 'x');
            AssertIndexerGetSet<DateTime>(apObject, "datetimefield", DateTime.Now);
        }

        [TestMethod]
        public void MultiValuedGetSetTest()
        {
            dynamic apObject = new APObject("object");
            AssertMultiValuedGetSet<int>(apObject, "field1", new int[] { 1, 2, 3, 4, 5, 6 });
            AssertMultiValuedGetSet<uint>(apObject, "field2", new uint[] { 1, 2, 3, 4, 5, 6 });
            AssertMultiValuedGetSet<long>(apObject, "field3", new long[] { 1, 2, 3, 4, 5, 6 });
            AssertMultiValuedGetSet<ulong>(apObject, "field4", new ulong[] { 1, 2, 3, 4, 5, 6 });

            AssertMultiValuedGetSet<float>(apObject, "field5", new float[] { 1, 2, 3, 4, 5, 6 });
            AssertMultiValuedGetSet<double>(apObject, "field6", new double[] { 1, 2, 3, 4, 5, 6 });
            AssertMultiValuedGetSet<decimal>(apObject, "field7", new decimal[] { 1, 2, 3, 4, 5, 6 });

            AssertMultiValuedGetSet<string>(apObject, "field8", new string[] { "a", "b", "c" });
            AssertMultiValuedGetSet<bool>(apObject, "field9", new bool[] { true, true, false, true, false});
            AssertMultiValuedGetSet<DateTime>(apObject, "field10", new DateTime[] { DateTime.Now, DateTime.Now, DateTime.Now });
            AssertMultiValuedGetSet<char>(apObject, "field11", new char[] { 'a', 'b', 'c' });
        }

        private void AssertMultiValuedGetSet<T>(dynamic apObject, string field, IEnumerable<T> values)
        {
            Console.WriteLine("Testing for Object.GetList<{0}>() and Object.SetList<{0}>().", typeof(T).Name);
            var initial = values.ToList();
            apObject.SetList<T>(field, initial);
            List<T> other = new List<T>();
            other.AddRange(apObject.GetList<T>(field));
            Assert.IsTrue(initial.Count == other.Count);
            for (int i = 0; i < initial.Count; i++)
                Assert.IsTrue(initial[i].Equals(other[i]));

        }

        [TestMethod]
        public void EntityGetSetTest()
        {
            dynamic apObject = new APObject("object");
            AssertGetSet<int>(apObject, "field1", 10);
            AssertGetSet<uint>(apObject, "field2", 10);
            AssertGetSet<long>(apObject, "field3", 10);
            AssertGetSet<float>(apObject, "field4", 10.0f);
            AssertGetSet<double>(apObject, "field5", 10.0d);
            AssertGetSet<decimal>(apObject, "field6", 10.0m);
            AssertGetSet<string>(apObject, "field7", "test string");
            AssertGetSet<char>(apObject, "field8", 'x');
            AssertGetSet<DateTime>(apObject, "field9", DateTime.Now);
        }

        [TestMethod]
        public void TryGetSetMultiValuedPropertyTest()
        {
            var apObject = new APObject("object");
            try
            {
                apObject.Set<int[]>("field1", new[] { 1, 3, 4, 5, 6 });
                Assert.Fail("Multivalue set should not be allowed on Object.Set<T>().");
            }
            catch (ArgumentException)
            {
            }

            try
            {
                var values = apObject.Get<int[]>("field1");
                Assert.Fail("Multivalue get should not be allowed on Object.Get<T>().");
            }
            catch (ArgumentException)
            {
            }
        }

        [TestMethod]
        public void TestGeocodeSet()
        {
            var apObject = new APObject("object");
            apObject.Set("geofield", new Geocode(80.0m, 81.0m));
            var value = apObject.Get<string>("geofield");
            Assert.IsTrue(value == "80.0,81.0");
        }

        [TestMethod]
        public void TestGeocodeGet()
        {
            var apObject = new APObject("object");
            apObject.Set("geofield", new Geocode(80.0m, 81.0m));
            var geo = apObject.Get<Geocode>("geofield");
            Assert.IsTrue(geo.ToString() == "80.0,81.0");
        }

        private void AssertIndexerGetSet<T>(dynamic apObject, string field, T value)
        {
            apObject["field"] = value;
            Assert.IsTrue(apObject["field"].GetValue<T>() == value);
        }

        private void AssertGetSet<T>(dynamic apObject, string field, T value)
        {
            apObject.Set<T>(field, value);
            Assert.IsTrue(apObject.Get<T>(field) == value);
        }

    }
}


text:
%% Memoir layout setup

%% NOTE: You are strongly advised not to change any of them unless you
%% know what you are doing.  These settings strongly interact in the
%% final look of the document.

% Dependencies
\usepackage{0general/ETHlogo}

% Turn extra space before chapter headings off.
\setlength{\beforechapskip}{0pt}

\nonzeroparskip
\parindent=0pt
\defaultlists

% Chapter style redefinition
\makeatletter

\if@twoside
  \pagestyle{Ruled}
  \copypagestyle{chapter}{Ruled}
\else
  \pagestyle{ruled}
  \copypagestyle{chapter}{ruled}
\fi
\makeoddhead{chapter}{}{}{}
\makeevenhead{chapter}{}{}{}
\makeheadrule{chapter}{\textwidth}{0pt}
\copypagestyle{abstract}{empty}

\makechapterstyle{bianchimod}{%
  \chapterstyle{default}
  \renewcommand*{\chapnamefont}{\normalfont\Large\sffamily}
  \renewcommand*{\chapnumfont}{\normalfont\Large\sffamily}
  \renewcommand*{\printchaptername}{%
    \chapnamefont\centering\@chapapp}
  \renewcommand*{\printchapternum}{\chapnumfont {\thechapter}}
  \renewcommand*{\chaptitlefont}{\normalfont\huge\sffamily}
  \renewcommand*{\printchaptertitle}[1]{%
    \hrule\vskip\onelineskip \centering \chaptitlefont\textbf{\vphantom{gyM}##1}\par}
  \renewcommand*{\afterchaptertitle}{\vskip\onelineskip \hrule\vskip
    \afterchapskip}
  \renewcommand*{\printchapternonum}{%
    \vphantom{\chapnumfont {9}}\afterchapternum}}

% Use the newly defined style
\chapterstyle{bianchimod}

\setsecheadstyle{\Large\bfseries\sffamily}
\setsubsecheadstyle{\large\bfseries\sffamily}
\setsubsubsecheadstyle{\bfseries\sffamily}
\setparaheadstyle{\normalsize\bfseries\sffamily}
\setsubparaheadstyle{\normalsize\itshape\sffamily}
\setsubparaindent{0pt}

% Set captions to a more separated style for clearness
\captionnamefont{\sffamily\bfseries\footnotesize}
\captiontitlefont{\sffamily\footnotesize}
\setlength{\intextsep}{16pt}
\setlength{\belowcaptionskip}{1pt}

% subcaptions by Lukas
\captionsetup[sub]{font=footnotesize, labelfont={sf,bf}, textfont=sf}
\captionsetup{font=footnotesize, labelfont={sf,bf}, textfont=sf}

% Set section and TOC numbering depth to subsection
\setsecnumdepth{subsection}
\settocdepth{subsection}

%% Titlepage adjustments
\pretitle{\vspace{0pt plus 0.7fill}\begin{center}\HUGE\sffamily\bfseries}
\posttitle{\end{center}\par}
\preauthor{\par\begin{center}\let\and\\\Large\sffamily}
\postauthor{\end{center}}
\predate{\par\begin{center}\Large\sffamily}
\postdate{\end{center}}

\def\@advisors{}
\newcommand{\advisors}[1]{\def\@advisors{#1}}
\def\@department{}
\newcommand{\department}[1]{\def\@department{#1}}
\def\@thesistype{}
\newcommand{\thesistype}[1]{\def\@thesistype{#1}}

\renewcommand{\maketitlehooka}{\noindent\ETHlogo[2in]}

\renewcommand{\maketitlehookb}{\vspace{1in}%
  \par\begin{center}\Large\sffamily\@thesistype\end{center}}

\renewcommand{\maketitlehookd}{%
  \vfill\par
  \begin{flushright}
    \sffamily
    \@advisors\par
    \@department, ETH Z\"urich
  \end{flushright}
}

\checkandfixthelayout

\setlength{\droptitle}{-48pt}

\makeatother

% This defines how theorems should look. Best leave as is.
\theoremstyle{plain}
\setlength\theorempostskipamount{0pt}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "thesis"
%%% End:


text:
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */

/*
 * This file provides what C99 standard requires for <string.h>
 * for some functions
 */

#ifndef STRING_H
#define STRING_H

#include <stddef.h>
#include <sys/cdefs.h>

void *memcpy(void *__restrict s1, const void *__restrict s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
void *memset(void *s, int c, size_t n);

int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
size_t strlen(const char *s);
size_t strnlen(const char *s, size_t n);
char *strdup(const char *s);
char *strndup(const char *s, size_t n);
char *strchr(const char *s, int c);
char *strstr(const char *big, const char *little);
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
char *strrchr(const char *s, int i);

void *memchr(const void *buf, int c, size_t length);

#endif /* STRING_H */


text:
package org.ndexbio.common.models.dao.orientdb;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.Date;

import org.ndexbio.common.NdexClasses;
import org.ndexbio.common.access.NdexDatabase;
import org.ndexbio.common.util.NdexUUIDFactory;
import org.ndexbio.common.util.Security;
import org.ndexbio.model.exceptions.*;
import org.ndexbio.model.object.Membership;
import org.ndexbio.model.object.MembershipType;
import org.ndexbio.model.object.Permissions;
import org.ndexbio.model.object.Request;
import org.ndexbio.model.object.ResponseType;
import org.ndexbio.model.object.Status;
import org.ndexbio.model.object.Task;
import org.ndexbio.model.object.User;
import org.ndexbio.model.object.NewUser;
import org.ndexbio.model.object.SimpleUserQuery;
import org.ndexbio.task.Configuration;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Direction;

public class UserDocDAO extends OrientdbDAO {

	private static final Logger logger = Logger.getLogger(UserDAO.class
			.getName());

	/*
	 * User operations can be achieved with Orient Document API methods. The
	 * constructor will need to accept a OrientGraph object if we wish to use
	 * the Graph API.
	 */
	/**************************************************************************
	 * UserDAO
	 *
	 * @param db
	 *            Database instance from the Connection pool, should be opened
	 * @param graph
	 *            OrientGraph instance for Graph API operations
	 **************************************************************************/
	public UserDocDAO(ODatabaseDocumentTx dbConnection) {
		super(dbConnection);
	}

	public UserDocDAO() throws NdexException {
		super(NdexDatabase.getInstance().getAConnection());
	}

	/**************************************************************************
	 * Authenticates a user trying to login.
	 *
	 * @param accountName
	 *            The accountName.
	 * @param password
	 *            The password.
	 * @throws SecurityException
	 *             Invalid accountName or password.
	 * @throws NdexException
	 *             Can't authenticate users against the database.
	 * @return The user, from NDEx Object Model.
	 **************************************************************************/
	public User authenticateUser(String accountName, String password)
			throws UnauthorizedOperationException, NdexException,ObjectNotFoundException {

		if (Strings.isNullOrEmpty(accountName)
				|| Strings.isNullOrEmpty(password))
			throw new UnauthorizedOperationException("No accountName or password entered.");

		try {
			final ODocument OAuthUser = this.getRecordByAccountName(
					accountName, NdexClasses.User);
			if (!Security.authenticateUser(password, (String)OAuthUser.field("password"))) {
				throw new UnauthorizedOperationException("Invalid accountName or password.");
			}
			return UserDocDAO.getUserFromDocument(OAuthUser);
		} catch (UnauthorizedOperationException se) {
			logger.info("Authentication failed: " + se.getMessage());
			throw se;
		} catch (ObjectNotFoundException e) {
			throw e;
		} catch (Exception e) {
			throw new NdexException(
					"Ndex internal error when authenticate user. "+e.getMessage());
		}
	}

	/**************************************************************************
	 * Create a new user
	 *
	 * @param newUser
	 *            A User object, from the NDEx Object Model
	 * @throws NdexException
	 *             Attempting to save an ODocument to the database
	 * @throws IllegalArgumentException
	 *             The newUser does not contain proper fields
	 * @throws DuplicateObjectException
	 *             The account name and/or email already exist
	 * @returns User object, from the NDEx Object Model
	 **************************************************************************/
	public User createNewUser(NewUser newUser, String verificationCode ) throws NdexException,
			IllegalArgumentException, DuplicateObjectException {

		Preconditions.checkArgument(null != newUser,
				"A user object is required");
		Preconditions.checkArgument(
				!Strings.isNullOrEmpty(newUser.getAccountName()),
				"A accountName is required");

		Preconditions.checkArgument(
				!Strings.isNullOrEmpty(newUser.getPassword()),
				"A user password is required");
		Preconditions.checkArgument(
				!Strings.isNullOrEmpty(newUser.getEmailAddress()),
				"A user email address is required");

		this.checkForExistingUser(newUser);

		try {

			ODocument user = new ODocument(NdexClasses.User).
					fields("description", newUser.getDescription(),
			               "websiteURL", newUser.getWebsite(),
			               "imageURL", newUser.getImage(),
			               "emailAddress", newUser.getEmailAddress(),
			               "firstName", newUser.getFirstName(),
			               "lastName", newUser.getLastName(),
			               //, newUser.getAccountName(),
			               "password", Security.hashText(newUser.getPassword()),
			               NdexClasses.ExternalObj_ID, NdexUUIDFactory.INSTANCE.createNewNDExUUID(),
			               NdexClasses.ExternalObj_cTime, new Date(),
			               NdexClasses.ExternalObj_mTime, new Date()
			               //NdexClasses.ExternalObj_isDeleted, false
					      );

			if ( verificationCode !=null) {

//				String verCode= Security.generatePassword(10);

				user.fields(NdexClasses.User_verification_code, verificationCode,
							NdexClasses.ExternalObj_isDeleted, true,
							NdexClasses.account_P_oldAcctName, newUser.getAccountName());
			} else {
				user.fields(NdexClasses.ExternalObj_isDeleted, false,
						NdexClasses.account_P_accountName, newUser.getAccountName());
			}

			user = user.save();

			logger.info("A new user with accountName "
					+ newUser.getAccountName() + " has been created");

			User u = UserDocDAO.getUserFromDocument(user);
	//		if ( needEmailVerification)
	//			u.set

			return u;

		} catch (Exception e) {
			logger.severe("Could not save new user to the database: " + e.getMessage());
			throw new NdexException(e.getMessage());
		}
	}

	public String verifyUser ( String userUUID, String verificationCode) throws ObjectNotFoundException, NdexException {
		ODocument userDoc = this.getRecordByUUIDStr(userUUID, NdexClasses.User);

		Boolean isDeleted = userDoc.field(NdexClasses.ExternalObj_isDeleted);
		if ( !isDeleted)
			throw new NdexException ( "User has already been verified.");

		String vCode = userDoc.field(NdexClasses.User_verification_code);

		String acc = userDoc.field(NdexClasses.account_P_oldAcctName);
		Date t = (Date)userDoc.field(NdexClasses.ExternalObj_cTime);

		long t2 = Calendar.getInstance().getTimeInMillis();
		boolean within = (t2 - t.getTime()) < 8 * 3600 * 1000;  // within 8 hours

		if ( vCode != null  && verificationCode.equals(vCode) && within) {
			userDoc.removeField(NdexClasses.account_P_oldAcctName);
			userDoc.removeField(NdexClasses.User_verification_code);
			userDoc.fields(NdexClasses.ExternalObj_isDeleted, false,
					NdexClasses.account_P_accountName, acc,
					NdexClasses.ExternalObj_mTime, t2).save();
			return acc;
		}

		throw new NdexException ( "Verification information not found");

	}

	/**************************************************************************
	 * Get a user
	 *
	 * @param id
	 *            UUID for User
	 * @throws NdexException
	 *             Attempting to query the database
	 * @returns User object, from the NDEx Object Model
	 **************************************************************************/

	public User getUserById(UUID id) throws NdexException,
			IllegalArgumentException, ObjectNotFoundException {

		Preconditions.checkArgument(null != id, "UUID required");

		final ODocument user = this.getRecordByUUID(id, NdexClasses.User);
		return UserDocDAO.getUserFromDocument(user);

	}

	/**************************************************************************
	 * Get a user
	 *
	 * @param accountName
	 *            accountName for User
	 * @throws NdexException
	 *             Attempting to query the database
	 * @returns User object, from the NDEx Object Model
	 **************************************************************************/
	public User getUserByAccountName(String accountName) throws NdexException,
			IllegalArgumentException, ObjectNotFoundException {

		Preconditions.checkArgument(!Strings.isNullOrEmpty(accountName),
				"accountName required");

		final ODocument user = this.getRecordByAccountName(accountName,
				NdexClasses.User);
		return UserDocDAO.getUserFromDocument(user);

	}

	public String getUserUUIDByEmail(String userEmail) throws IllegalArgumentException {

		Preconditions.checkArgument(!Strings.isNullOrEmpty(userEmail),
				"userEmail required");

		OIndex<?> Idx = this.db.getMetadata().getIndexManager().getIndex( NdexClasses.Index_userEmail );
		OIdentifiable user = (OIdentifiable) Idx.get(userEmail);
		if(user == null)
				return null;

		ODocument doc = user.getRecord();
		return doc.field(NdexClasses.ExternalObj_ID);

	}

	/**************************************************************************
	 * Find users
	 *
	 * @param id
	 *            UUID for User
	 * @param skip
	 *            amount of blocks to skip
	 * @param top
	 *            block size
	 * @throws NdexException
	 *             Attempting to query the database
	 * @returns User object, from the NDEx Object Model
	 **************************************************************************/
	public List<User> findUsers(SimpleUserQuery simpleQuery, int skip, int top)
			throws IllegalArgumentException, NdexException {
		Preconditions.checkArgument(simpleQuery != null,
				"Search parameters are required");

		String traversePermission;
		OSQLSynchQuery<ODocument> query;
		Iterable<ODocument> users;
		final List<User> foundUsers = new ArrayList<>();

		String searchStr = simpleQuery.getSearchString().toLowerCase();

		if (searchStr.equals("*") )
			searchStr = "";

		if (simpleQuery.getPermission() == null)
			traversePermission = "in_groupadmin, in_member";
		else
			traversePermission = "in_"
					+ simpleQuery.getPermission().name().toLowerCase();

		//StringEscapeUtils.escapeJava()
		searchStr = Helper.escapeOrientDBSQL(searchStr.toLowerCase().trim());
		final int startIndex = skip * top;

		try {

			if (!Strings.isNullOrEmpty(simpleQuery.getAccountName())) {
				ODocument nGroup = this.getRecordByAccountName(simpleQuery.getAccountName(), NdexClasses.Group);

				if (nGroup == null)
					throw new NdexException("Invalid accountName to filter by");

				String traverseRID = nGroup.getIdentity().toString();
				query = new OSQLSynchQuery<>("SELECT FROM"
						+ " (TRAVERSE "
						+ traversePermission
						+ " FROM"
						+ " "
						+ traverseRID
						+ " WHILE $depth <=1)"
						+ " WHERE @class = '"
						+ NdexClasses.User
						+ "' and ( " + NdexClasses.ExternalObj_isDeleted + "= false)"
						+ " AND (accountName.toLowerCase() LIKE '%"
						+ searchStr
						+ "%'"
						+ "  OR lastName.toLowerCase() LIKE '%"
						+ searchStr
						+ "%'"
						+ "  OR firstName.toLowerCase() LIKE '%"
						+ searchStr
						+ "%' )"
						+ " ORDER BY " + NdexClasses.ExternalObj_cTime + " DESC "
						+ " SKIP "
						+ startIndex + " LIMIT " + top);

				users = this.db.command(query).execute();

		/*		if (!users.iterator().hasNext()  && simpleQuery.getSearchString().equals("")) {

					query = new OSQLSynchQuery<>("SELECT FROM"
							+ " (TRAVERSE " + traversePermission + " FROM"
							+ " " + traverseRID + " WHILE $depth <=1)"
							+ " WHERE @class = '" + NdexClasses.User + "'"
							+ " ORDER BY " + NdexClasses.ExternalObj_cTime + " DESC " + " SKIP "
							+ startIndex + " LIMIT " + top);

					users = this.db.command(query).execute();

				} */

				for (final ODocument user : users) {
					foundUsers.add(UserDocDAO.getUserFromDocument(user));
				}
				return foundUsers;

			}

			query = new OSQLSynchQuery<>("SELECT FROM "
						+ NdexClasses.User + " "
						+ "WHERE ( "+ NdexClasses.ExternalObj_isDeleted
						+ " = false ) and ( accountName.toLowerCase() LIKE '%"
						+ searchStr + "%'"
						+ "  OR lastName.toLowerCase() LIKE '%"
						+ searchStr + "%'"
						+ "  OR firstName.toLowerCase() LIKE '%"
						+ searchStr + "%'"
						+ ")  ORDER BY " + NdexClasses.ExternalObj_cTime + " DESC " + " SKIP "
						+ startIndex + " LIMIT " + top);

			users = this.db.command(query).execute();

			for (final ODocument user : users) {
					foundUsers.add(UserDocDAO.getUserFromDocument(user));
			}
			return foundUsers;

		} catch (Exception e) {
			logger.severe("Unable to query the database");
			throw new NdexException("Failed to search for users.\n"
					+ e.getMessage());

		}

	}

	/**************************************************************************
	 * Email a new password
	 *
	 * @param accountName
	 *            accountName for the User
	 * @throws NdexException
	 *             Attempting to query the database
	 * @throws IllegalArgumentException
	 *             accountName is required
	 * @throws ObjectNotFoundException
	 *             user with account name does not exist
	 * @returns response
	 **************************************************************************/
	public String setNewPassword(String accountName)
			throws IllegalArgumentException, ObjectNotFoundException,
			NdexException {

		Preconditions.checkArgument(!Strings.isNullOrEmpty(accountName),
				"An accountName is required");

		try {

			ODocument userToSave = this.getRecordByAccountName(accountName,
					NdexClasses.User);

			final String newPassword = Security.generatePassword();
			final String password = Security.hashText(newPassword);
			userToSave.fields("password", password,
					NdexClasses.ExternalObj_mTime, new Date()).save();

			return newPassword;

		} catch (ObjectNotFoundException onfe) {

			throw onfe;

		} catch (Exception e) {

			throw new NdexException("Failed to recover your password: \n"
					+ e.getMessage());

		}
	}

	/**************************************************************************
	 * Change a user's password
	 *
	 * @param id
	 *            UUID for user
	 * @param password
	 *            new password for user
	 * @throws NdexException
	 *             Attempting to access the database
	 * @throws IllegalArgumentException
	 *             new password and user id are required
	 * @throws ObjectNotFoundException
	 *             user does not exist
	 * @returns response
	 **************************************************************************/
	public void changePassword(String password, UUID id)
			throws IllegalArgumentException, NdexException,
			ObjectNotFoundException {

		Preconditions.checkNotNull(id, "A user id is required");
		Preconditions.checkArgument(!Strings.isNullOrEmpty(password),
				"A password is required");

		ODocument user = this.getRecordByUUID(id, NdexClasses.User);

		try {
			// Remove quotes around the password
			if (password.startsWith("\"") && password.endsWith("\"") )
				password = password.substring(1, password.length() - 2);

			user.fields("password", Security.hashText(password.trim()),
					    NdexClasses.ExternalObj_mTime, new Date());

			user.save();

			logger.info("Changed password for user with UUID " + id);

		} catch (Exception e) {

			logger.severe("An error occured while saving password for user with UUID "
					+ id);
			throw new NdexException("Failed to change your password.\n"
					+ e.getMessage());

		}

	}

	/**************************************************************************
	 * Change a user's email Address
	 *
	 * @param id
	 *            UUID for user
	 * @param emailAddress
	 *            new email address
	 * @throws NdexException
	 *             Attempting to access the database
	 * @throws IllegalArgumentException
	 *             new password and user id are required
	 * @throws ObjectNotFoundException
	 *             user does not exist
	 * @returns response
	 **************************************************************************/
/*	public Response changeEmailAddress(String emailAddress, UUID id)
			throws IllegalArgumentException, NdexException,
			ObjectNotFoundException, DuplicateObjectException {

		Preconditions.checkNotNull(id, "A user id is required");
		Preconditions.checkArgument(!Strings.isNullOrEmpty(emailAddress),
				"A password is required");

		ODocument user = this.getRecordById(id, NdexClasses.User);

		try {

			// check for unique emailAddress
			String query = "select emailAddress from " + NdexClasses.User
					+ " where emailAddress = ?";
			List<ODocument> existingUser = db.command(new OCommandSQL(query))
					.execute(emailAddress);

			if (!existingUser.isEmpty()) {
				logger.severe("Email address already exists in the database.");
				throw new NdexException("email address is taken");
			}

			final String oldEmail = (String) user.field("emailAddress");
			user.field("emailAddress", emailAddress);
			user.save();

			// send emails to new and old address
			final File ChangeEmailFile = new File(Configuration.getInstance()
					.getProperty("Change-Email-File"));

			if (!ChangeEmailFile.exists()) {
				logger.severe("Could not retrieve change email file");
				throw new java.io.FileNotFoundException(
						"File containing change email content doesn't exist.");
			}

			final BufferedReader fileReader = Files.newBufferedReader(
					ChangeEmailFile.toPath(), Charset.forName("US-ASCII"));

			final StringBuilder changeEmailText = new StringBuilder();

			String lineOfText = null;
			while ((lineOfText = fileReader.readLine()) != null)
				changeEmailText.append(lineOfText.replace("{oldEmail}",
						oldEmail).replace("{newEmail}", emailAddress));

			Email.sendEmail(
					Configuration.getInstance().getProperty(
							"Change-Email-Email"), oldEmail, "Email Change",
					changeEmailText.toString());
			Email.sendEmail(
					Configuration.getInstance().getProperty(
							"Change-Email-Email"), emailAddress,
					"Email Change", changeEmailText.toString());

			logger.info("Changed email address for user with UUID " + id);

			return Response.ok().build();

		} catch (Exception e) {

			logger.severe("An error occured while changing email for user with UUID "
					+ id);
			throw new NdexException("Failed to change your email.\n"
					+ e.getMessage());

		}

	}
*/
	/**************************************************************************
	 * Update a user
	 *
	 * @param updatedUser
	 *            User with new information
	 * @param id
	 *            UUID for user
	 * @throws NdexException
	 *             Attempting to access the database
	 * @throws IllegalArgumentException
	 *             new password and user id are required
	 * @throws ObjectNotFoundException
	 *             user does not exist
	 * @return User object
	 **************************************************************************/
	public User updateUser(User updatedUser, UUID id)
			throws IllegalArgumentException, NdexException,
			ObjectNotFoundException {

		Preconditions.checkArgument(id != null, "A user id is required");
		Preconditions.checkArgument(updatedUser != null,
				"An updated user is required");

		ODocument user = this.getRecordByUUID(id, NdexClasses.User);

		try {
			// updatedUser.getDescription().isEmpty();
				user.fields("description", updatedUser.getDescription(),
						"websiteURL", updatedUser.getWebsite(),
						"imageURL", updatedUser.getImage(),
						"firstName", updatedUser.getFirstName(),
						"lastName", updatedUser.getLastName(),
						NdexClasses.ExternalObj_mTime, updatedUser.getModificationTime());

			user = user.save();
			logger.info("Updated user profile with UUID " + id);

			return getUserFromDocument(user);

		} catch (Exception e) {

			logger.severe("An error occured while updating user profile with UUID "
					+ id);
			throw new NdexException(e.getMessage());

		}

	}

	/**************************************************************************
	 * getUserNetworkMemberships
	 *
	 * @param userId
	 *            UUID for associated user
	 * @param permission
	 *            Type of memberships to retrieve, ADMIN, WRITE, or READ
	 * @param skipBlocks
	 *            amount of blocks to skip
	 * @param blockSize
	 *            The size of blocks to be skipped and retrieved
	 * @throws NdexException
	 *             Invalid parameters or an error occurred while accessing the
	 *             database
	 * @throws ObjectNotFoundException
	 *             Invalid userId
	 **************************************************************************/

	public List<Membership> getUserNetworkMemberships(UUID userId,
			Permissions permission, int skipBlocks, int blockSize)
			throws ObjectNotFoundException, NdexException {

		Preconditions.checkArgument(!Strings.isNullOrEmpty(userId.toString()),
				"A user UUID is required");
		Preconditions.checkArgument((permission == Permissions.ADMIN)
				|| (permission == Permissions.READ)
				|| (permission == Permissions.WRITE),
				"Valid permissions required");

		ODocument user = this.getRecordByUUID(userId, NdexClasses.User);

		final int startIndex = skipBlocks * blockSize;

		try {
			List<Membership> memberships = new ArrayList<>();

			String userRID = user.getIdentity().toString();

			OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>(
					"SELECT FROM" + " (TRAVERSE out_"
							+ Permissions.GROUPADMIN.name().toLowerCase()
							+ ", out_"
							+ permission.name().toString().toLowerCase()
							+ " FROM" + " " + userRID + "  WHILE $depth <=2)"
							+ " WHERE @class = '" + NdexClasses.Network + "' and ( " + NdexClasses.ExternalObj_isDeleted +" = false) "
							+ " ORDER BY " + NdexClasses.ExternalObj_cTime + " DESC " + " SKIP "
							+ startIndex + " LIMIT " + blockSize);

			List<ODocument> records = this.db.command(query).execute();
			for (ODocument network : records) {

				Membership membership = new Membership();
				membership.setMembershipType(MembershipType.NETWORK);
				membership.setMemberAccountName((String) user
						.field("accountName"));
				membership.setMemberUUID(userId);
				membership.setPermissions(permission);
				membership.setResourceName((String) network.field("name"));
				membership.setResourceUUID(UUID.fromString((String) network
						.field("UUID")));

				memberships.add(membership);
			}

			logger.info("Successfuly retrieved user-network memberships");
			return memberships;

		} catch (Exception e) {
			logger.severe("An unexpected error occured while retrieving user-network memberships");
			throw new NdexException(e.getMessage());
		}
	}

	/**************************************************************************
	 * getUsergroupMemberships
	 *
	 * @param userId
	 *            UUID for associated user
	 * @param permission
	 *            Type of memberships to retrieve, ADMIN, WRITE, or READ
	 * @param skipBlocks
	 *            amount of blocks to skip
	 * @param blockSize
	 *            The size of blocks to be skipped and retrieved
	 * @throws NdexException
	 *             Invalid parameters or an error occurred while accessing the
	 *             database
	 * @throws ObjectNotFoundException
	 *             Invalid userId
	 **************************************************************************/

	public List<Membership> getUserGroupMemberships(UUID userId,
			Permissions permission, int skipBlocks, int blockSize)
			throws ObjectNotFoundException, NdexException {

		Preconditions.checkArgument(!Strings.isNullOrEmpty(userId.toString()),
				"A user UUID is required");
		Preconditions.checkArgument((permission.equals(Permissions.GROUPADMIN))
				|| (permission.equals(Permissions.MEMBER)),
				"Valid permissions required");

		ODocument user = this.getRecordByUUID(userId, NdexClasses.User);

		final int startIndex = skipBlocks * blockSize;

		try {
			List<Membership> memberships = new ArrayList<>();

			String userRID = user.getIdentity().toString();
			OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>(
					"SELECT FROM" + " (TRAVERSE " + NdexClasses.User + ".out_"
							+ permission.name().toString().toLowerCase()
							+ " FROM" + " " + userRID + "  WHILE $depth <=1)"
							+ " WHERE @class = '" + NdexClasses.Group + "' and ( " + NdexClasses.ExternalObj_isDeleted +" = false) "
							+ " ORDER BY " + NdexClasses.ExternalObj_cTime + " DESC " + " SKIP "
							+ startIndex + " LIMIT " + blockSize);

			List<ODocument> records = this.db.command(query).execute();
			for (ODocument group : records) {

				Membership membership = new Membership();
				membership.setMembershipType(MembershipType.GROUP);
				membership.setMemberAccountName((String) user
						.field("accountName"));
				membership.setMemberUUID(userId);
				membership.setPermissions(permission);
				membership.setResourceName((String) group
						.field(NdexClasses.GRP_P_NAME));
				membership.setResourceUUID(UUID.fromString((String) group
						.field("UUID")));

				memberships.add(membership);
			}

			logger.info("Successfuly retrieved user-group memberships");
			return memberships;

		} catch (Exception e) {
			logger.severe("An unexpected error occured while retrieving user-group memberships");
			throw new NdexException(e.getMessage());
		}
	}

	/**
	 * Get all the group memberships this user belongs to.
	 * @param userId
	 * @return
	 * @throws ObjectNotFoundException
	 * @throws NdexException
	 */
	public List<String> getUserAllGroupMemberships(String userAccountName)
			throws ObjectNotFoundException, NdexException {

		Preconditions.checkArgument(!Strings.isNullOrEmpty(userAccountName),
				"A user UUID is required");

		ODocument user = this.getRecordByAccountName(userAccountName,NdexClasses.User);

		try {
			List<String> memberships = new ArrayList<>();

			String userRID = user.getIdentity().toString();
			OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>(
					"SELECT FROM" + " (TRAVERSE " + NdexClasses.User + ".out_"
							+ Permissions.GROUPADMIN.name().toString().toLowerCase()
							+ "," + NdexClasses.User + ".out_"
							+ Permissions.MEMBER.name().toString().toLowerCase()
							+ " FROM" + " " + userRID + "  WHILE $depth <=1)"
							+ " WHERE @class = '" + NdexClasses.Group + "' and ( " + NdexClasses.ExternalObj_isDeleted +" = false) ");

			List<ODocument> records = this.db.command(query).execute();
			for (ODocument group : records) {
				memberships.add((String) group
						.field(NdexClasses.account_P_accountName));
			}

			logger.info("Successfuly retrieved user-group memberships");
			return memberships;

		} catch (Exception e) {
			logger.severe("An unexpected error occured while retrieving user-group memberships");
			throw new NdexException(e.getMessage());
		}
	}

	/**************************************************************************
	 * getMembership
	 *
	 * @param account
	 *            UUID for user or group
	 * @param resource
	 *            UUID for resource
	 * @throws NdexException
	 *             Invalid parameters or an error occurred while accessing the
	 *             database
	 * @throws ObjectNotFoundException
	 *             Invalid userId
	 **************************************************************************/

	public Membership getMembership(UUID account, UUID resource, int depth)
			throws IllegalArgumentException, ObjectNotFoundException,
			NdexException {

		Preconditions.checkArgument(account != null, "Account UUID required");
		Preconditions.checkArgument(resource != null, "Resource UUID required");
		Preconditions.checkArgument(depth > 0 && depth < 3, "Depth range: [1,2]");

		ODocument OAccount = this.getRecordByUUID(account, null);
		ODocument OResource = this.getRecordByUUID(resource, null);

		Permissions permission = null;
		Membership membership = new Membership();

		if (OResource.getClassName().equals(NdexClasses.Group)) {
			if (checkPermission(OAccount.getIdentity(),
					OResource.getIdentity(), Direction.OUT, depth,
					Permissions.GROUPADMIN))
				permission = Permissions.GROUPADMIN;
			if (checkPermission(OAccount.getIdentity(),
					OResource.getIdentity(), Direction.OUT, depth,
					Permissions.MEMBER))
				permission = Permissions.MEMBER;

			membership.setMemberAccountName((String) OAccount
					.field(NdexClasses.account_P_accountName));
			membership.setMemberUUID(account);
			membership.setResourceName((String) OResource
					.field(NdexClasses.GRP_P_NAME));
			membership.setResourceUUID(resource);
			membership.setPermissions(permission);
			membership.setMembershipType(MembershipType.GROUP);

		} else {
			// order allows us to return most permissive permission
			if (checkPermission(OAccount.getIdentity(),
					OResource.getIdentity(), Direction.OUT, depth,
					Permissions.READ, Permissions.GROUPADMIN, Permissions.MEMBER))
				permission = Permissions.READ;
			if (checkPermission(OAccount.getIdentity(),
					OResource.getIdentity(), Direction.OUT, depth,
					Permissions.WRITE, Permissions.GROUPADMIN, Permissions.MEMBER))
				permission = Permissions.WRITE;
			if (checkPermission(OAccount.getIdentity(),
					OResource.getIdentity(), Direction.OUT, depth,
					Permissions.ADMIN, Permissions.GROUPADMIN, Permissions.MEMBER))
				permission = Permissions.ADMIN;

			membership.setMemberAccountName((String) OAccount
					.field(NdexClasses.account_P_accountName));
			membership.setMemberUUID(account);
			membership.setResourceName((String) OResource.field("name"));
			membership.setResourceUUID(resource);
			membership.setPermissions(permission);
			membership.setMembershipType(MembershipType.NETWORK);

		}

		if (permission != null)
			return membership;

		return null;
	}

	/**************************************************************************
	 * getSentRequest
	 *
	 * @param account
	 *            User object
	 * @param skipBlocks
	 *            amount of blocks to skip
	 * @param blockSize
	 *            The size of blocks to be skipped and retrieved
	 * @throws NdexException
	 *             An error occurred while accessing the database
	 * @throws ObjectNotFoundException
	 *             Invalid userId
	 **************************************************************************/

	public List<Request> getSentRequest(User account, int skipBlocks,
			int blockSize) throws ObjectNotFoundException, NdexException {
		Preconditions.checkArgument(account != null, "Must be logged in");
		// TODO May possibly add extra parameter to specify type of request to
		// return

		final List<Request> requests = new ArrayList<>();

		ODocument user = this.getRecordByUUID(account.getExternalId(),
				NdexClasses.User);
		final int startIndex = skipBlocks * blockSize;

			OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>(
					"SELECT FROM" + " (TRAVERSE out_requests FROM" + " "
							+ user.getIdentity().toString()
							+ "  WHILE $depth <=1)" + " WHERE @class = '"
							+ NdexClasses.Request + "'"
							+ " ORDER BY " + NdexClasses.ExternalObj_cTime + " DESC " + " SKIP "
							+ startIndex + " LIMIT " + blockSize);

			List<ODocument> records = this.db.command(query).execute();

			for (ODocument request : records) {
				requests.add(RequestDAO.getRequestFromDocument(request));
			}

			return requests;
	}

	/**************************************************************************
	 * getPendingRequest
	 *
	 * @param account
	 *            User object
	 * @param skipBlocks
	 *            amount of blocks to skip
	 * @param blockSize
	 *            The size of blocks to be skipped and retrieved
	 * @throws NdexException
	 *             An error occurred while accessing the database
	 * @throws ObjectNotFoundException
	 *             Invalid userId
	 **************************************************************************/
	public List<Request> getPendingRequest(User account, int skipBlocks,
			int blockSize) throws ObjectNotFoundException, NdexException {
		Preconditions.checkArgument(account != null, "Must be logged in");
		// TODO May possibly add extra parameter to specify type of request to
		// return

		final List<Request> requests = new ArrayList<>();

		ODocument user = this.getRecordByUUID(account.getExternalId(),
				NdexClasses.User);
		final int startIndex = skipBlocks * blockSize;

		try {
			OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>(
					"SELECT FROM" + " (TRAVERSE in_requests FROM" + " "
							+ user.getIdentity().toString()
							+ "  WHILE $depth <=1)" + " WHERE @class = '"
							+ NdexClasses.Request + "'" + " AND response = '"
							+ ResponseType.PENDING + "'"
							+ " ORDER BY " + NdexClasses.ExternalObj_cTime + " DESC " + " SKIP "
							+ startIndex + " LIMIT " + blockSize);

			List<ODocument> records = this.db.command(query).execute();

			for (ODocument request : records) {
				requests.add(RequestDAO.getRequestFromDocument(request));
			}

			return requests;
		} catch (Exception e) {
			throw new NdexException("Unable to retrieve sent requests");
		}
	}

	public List<Task> getTasks(User account, Status status,
			int skipBlocks, int blockSize) throws ObjectNotFoundException,
			NdexException {

		Preconditions.checkArgument(account != null, "Must be logged in");

		final List<Task> tasks = new ArrayList<>();

		ODocument user = this.getRecordByUUID(account.getExternalId(),
				NdexClasses.User);
		final int startIndex = skipBlocks * blockSize;

		String statusFilter = "";
		if (status != Status.ALL){
			statusFilter = " and status = '" + status + "'";
		}

		try {
			OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>(
					"SELECT FROM" + " (TRAVERSE in_ownedBy FROM" + " "
							+ user.getIdentity().toString()
							+ "  WHILE $depth <=1)"
							+ " WHERE @class = '" + NdexClasses.Task + "' and ( " + NdexClasses.ExternalObj_isDeleted +" = false) "
							+ statusFilter
							+ " ORDER BY " + NdexClasses.ExternalObj_cTime + " DESC "
							+ " SKIP " + startIndex
							+ " LIMIT " + blockSize);

			List<ODocument> records = this.db.command(query).execute();

			for (ODocument task : records) {
				tasks.add(TaskDAO.getTaskFromDocument(task));
			}

			return tasks;

		} catch (Exception e) {
			throw new NdexException("Unable to retrieve user tasks:" + e.getMessage());
		}
	}

	/*
	 * Convert the database results into our object model
	 */
	/*
	 * Convert the database results into our object model
	 */
	public static User getUserFromDocument(ODocument n) {

		User result = new User();

		Helper.populateExternalObjectFromDoc (result, n);

		result.setFirstName((String) n.field("firstName"));
		result.setLastName((String) n.field("lastName"));
		result.setWebsite((String) n.field("websiteURL"));
		result.setDescription((String) n.field("description"));
		result.setImage((String) n.field("imageURL"));

		if ( result.getIsDeleted() ) {
			result.setAccountName ((String) n.field(NdexClasses.account_P_oldAcctName));
			result.setEmailAddress((String) n.field(NdexClasses.User_P_oldEmailAddress));
		} else {
			result.setAccountName((String) n.field(NdexClasses.account_P_accountName));
			result.setEmailAddress((String) n.field("emailAddress"));
		}

		return result;
	}

	/*
	 * Both a User's AccountName and emailAddress must be unique in the
	 * database. Throw a DuplicateObjectException if that is not the case
	 */

	protected void checkForExistingUser(final NewUser newUser)
			throws DuplicateObjectException, NdexException {
		try {
			OIndex<?> Idx = this.db.getMetadata().getIndexManager()
					.getIndex("index-user-username");
			OIdentifiable user = (OIdentifiable) Idx.get(newUser
					.getAccountName()); // account to traverse by

			if (user != null) {
				logger.info("User with accountName " + newUser.getAccountName()
						+ " already exists");
				throw new DuplicateObjectException(
						CommonDAOValues.DUPLICATED_ACCOUNT_FLAG);
			}
			OIndex<?> emailIdx = this.db.getMetadata().getIndexManager()
					.getIndex("index-user-emailAddress");
			user = (OIdentifiable) emailIdx.get(newUser.getEmailAddress()); // account
																			// to
																			// traverse
																			// by

			if (user != null) {
				logger.info("User with emailAddress "
						+ newUser.getEmailAddress() + " already exists");
				throw new DuplicateObjectException(
						CommonDAOValues.DUPLICATED_EMAIL_FLAG);
			}
		} catch (DuplicateObjectException e) {
			throw e;
		} catch (Exception e) {
			logger.info("Unexpected error on existing user check");
			throw new NdexException(e.getMessage());
		}

	}

}