text:
<!---
Copyright 2021 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# Language model training examples

The following example showcases how to train a language model from scratch
using the JAX/Flax backend.

JAX/Flax allows you to trace pure functions and compile them into efficient, fused accelerator code on both GPU and TPU.
Models written in JAX/Flax are **immutable** and updated in a purely functional
way which enables simple and efficient model parallelism.

## Masked language modeling

In the following, we demonstrate how to train a bi-directional transformer model
using masked language modeling objective as introduced in [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805).
More specifically, we demonstrate how JAX/Flax can be leveraged
to pre-train [**`roberta-base`**](https://huggingface.co/roberta-base)
in Norwegian on a single TPUv3-8 pod.

The example script uses the 🤗 Datasets library. You can easily customize them to your needs if you need extra processing on your datasets.

To setup all relevant files for training, let's create a directory.

```bash
mkdir ./norwegian-roberta-base
```

### Train tokenizer

In the first step, we train a tokenizer to efficiently process the text input for the model. Similar to how it is shown in [How to train a new language model from scratch using Transformers and Tokenizers](https://huggingface.co/blog/how-to-train), we use a **`ByteLevelBPETokenizer`**.
The tokenizer is trained on the complete Norwegian dataset of OSCAR
and consequently saved in the cloned model directory.
This can take up to 10 minutes depending on your hardware ☕.

```python
from datasets import load_dataset
from tokenizers import trainers, Tokenizer, normalizers, ByteLevelBPETokenizer

# load dataset
dataset = load_dataset("oscar", "unshuffled_deduplicated_no", split="train")

# Instantiate tokenizer
tokenizer = ByteLevelBPETokenizer()

def batch_iterator(batch_size=1000):
    for i in range(0, len(dataset), batch_size):
        yield dataset[i: i + batch_size]["text"]

# Customized training
tokenizer.train_from_iterator(batch_iterator(), vocab_size=50265, min_frequency=2, special_tokens=[
    "<s>",
    "<pad>",
    "</s>",
    "<unk>",
    "<mask>",
])

# Save files to disk
tokenizer.save("./norwegian-roberta-base/tokenizer.json")
```

### Create configuration

Next, we create the model's configuration file. This is as simple
as loading and storing [`**roberta-base**`](https://huggingface.co/roberta-base)
in the local model folder:

```python
from transformers import RobertaConfig

config = RobertaConfig.from_pretrained("roberta-base", vocab_size=50265)
config.save_pretrained("./norwegian-roberta-base")
```

Great, we have set up our model repository. During training, we will automatically
push the training logs and model weights to the repo.

### Train model

Next we can run the example script to pretrain the model:

```bash
python run_mlm_flax.py \
    --output_dir="./norwegian-roberta-base" \
    --model_type="roberta" \
    --config_name="./norwegian-roberta-base" \
    --tokenizer_name="./norwegian-roberta-base" \
    --dataset_name="oscar" \
    --dataset_config_name="unshuffled_deduplicated_no" \
    --max_seq_length="128" \
    --weight_decay="0.01" \
    --per_device_train_batch_size="128" \
    --per_device_eval_batch_size="128" \
    --learning_rate="3e-4" \
    --warmup_steps="1000" \
    --overwrite_output_dir \
    --num_train_epochs="18" \
    --adam_beta1="0.9" \
    --adam_beta2="0.98" \
    --logging_steps="500" \
    --save_steps="2500" \
    --eval_steps="2500" \
    --push_to_hub
```

Training should converge at a loss and accuracy
of 1.78 and 0.64 respectively after 18 epochs on a single TPUv3-8.
This should take less than 18 hours.
Training statistics can be accessed on [tfhub.dev](https://tensorboard.dev/experiment/GdYmdak2TWeVz0DDRYOrrg).

For a step-by-step walkthrough of how to do masked language modeling in Flax, please have a
look at [this](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/masked_language_modeling_flax.ipynb) google colab.

## Causal language modeling

In the following, we demonstrate how to train an auto-regressive causal transformer model
in JAX/Flax.
More specifically, we pretrain a randomely initialized [**`gpt2`**](https://huggingface.co/gpt2) model in Norwegian on a single TPUv3-8.
to pre-train 124M [**`gpt2`**](https://huggingface.co/gpt2)
in Norwegian on a single TPUv3-8 pod.

The example script uses the 🤗 Datasets library. You can easily customize them to your needs if you need extra processing on your datasets.

To setup all relevant files for training, let's create a directory.

```bash
mkdir ./norwegian-gpt2
```

### Train tokenizer

In the first step, we train a tokenizer to efficiently process the text input for the model. Similar to how it is shown in [How to train a new language model from scratch using Transformers and Tokenizers](https://huggingface.co/blog/how-to-train), we use a **`ByteLevelBPETokenizer`**.
The tokenizer is trained on the complete Norwegian dataset of OSCAR
and consequently saved in the cloned model directory.
This can take up to 10 minutes depending on your hardware ☕.

```python
from datasets import load_dataset
from tokenizers import trainers, Tokenizer, normalizers, ByteLevelBPETokenizer

# load dataset
dataset = load_dataset("oscar", "unshuffled_deduplicated_no", split="train")

# Instantiate tokenizer
tokenizer = ByteLevelBPETokenizer()

def batch_iterator(batch_size=1000):
    for i in range(0, len(dataset), batch_size):
        yield dataset[i: i + batch_size]["text"]

# Customized training
tokenizer.train_from_iterator(batch_iterator(), vocab_size=50257, min_frequency=2, special_tokens=[
    "<s>",
    "<pad>",
    "</s>",
    "<unk>",
    "<mask>",
])

# Save files to disk
tokenizer.save("./norwegian-gpt2/tokenizer.json")
```

### Create configuration

Next, we create the model's configuration file. This is as simple
as loading and storing [`**gpt2**`](https://huggingface.co/gpt2)
in the local model folder:

```python
from transformers import GPT2Config

config = GPT2Config.from_pretrained("gpt2", resid_pdrop=0.0, embd_pdrop=0.0, attn_pdrop=0.0, vocab_size=50257)
config.save_pretrained("./norwegian-gpt2")
```

Great, we have set up our model repository. During training, we will now automatically
push the training logs and model weights to the repo.

### Train model

Finally, we can run the example script to pretrain the model:

```bash
python run_clm_flax.py \
    --output_dir="./norwegian-gpt2" \
    --model_type="gpt2" \
    --config_name="./norwegian-gpt2" \
    --tokenizer_name="./norwegian-gpt2" \
    --dataset_name="oscar" \
    --dataset_config_name="unshuffled_deduplicated_no" \
    --do_train --do_eval \
    --block_size="512" \
    --per_device_train_batch_size="64" \
    --per_device_eval_batch_size="64" \
    --learning_rate="5e-3" --warmup_steps="1000" \
    --adam_beta1="0.9" --adam_beta2="0.98" --weight_decay="0.01" \
    --overwrite_output_dir \
    --num_train_epochs="20" \
    --logging_steps="500" \
    --save_steps="2500" \
    --eval_steps="2500" \
    --push_to_hub
```

Training should converge at a loss and perplexity
of 3.24 and 25.72 respectively after 20 epochs on a single TPUv3-8.
This should take less than ~21 hours.
Training statistics can be accessed on [tfhub.de](https://tensorboard.dev/experiment/2zEhLwJ0Qp2FAkI3WVH9qA).

For a step-by-step walkthrough of how to do causal language modeling in Flax, please have a
look at [this](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/causal_language_modeling_flax.ipynb) google colab.

## T5-like span-masked language modeling

In the following, we demonstrate how to train a T5 model using the span-masked language model
objective as proposed in the [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683).
More specifically, we demonstrate how JAX/Flax can be leveraged
to pre-train [**`google/t5-v1_1-base`**](https://huggingface.co/google/t5-v1_1-base)
in Norwegian on a single TPUv3-8 pod.

The example script uses the 🤗 Datasets library. You can easily customize them to your needs if you need extra processing on your datasets.

Let's start by creating a model repository to save the trained model and logs.
Here we call the model `"norwegian-t5-base"`, but you can change the model name as you like.

To setup all relevant files for training, let's create a directory.

```bash
cd ./norwegian-t5-base
```

### Train tokenizer

In the first step, we train a tokenizer to efficiently process the text input for the model.
We make use of the [tokenizers](https://github.com/huggingface/tokenizers) library to train
a sentencepiece unigram tokenizer as shown in [t5_tokenizer_model.py](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling/t5_tokenizer_model.py)
which is heavily inspired from [yandex-research/DeDLOC's tokenizer model](https://github.com/yandex-research/DeDLOC/blob/5c994bc64e573702a9a79add3ecd68b38f14b548/sahajbert/tokenizer/tokenizer_model.py) .

The tokenizer is trained on the complete Norwegian dataset of OSCAR
and consequently saved in the cloned model directory.
This can take up to 120 minutes depending on your hardware ☕☕☕ .

```python
import datasets

from t5_tokenizer_model import SentencePieceUnigramTokenizer

vocab_size = 32_000
input_sentence_size = None

# Initialize a dataset
dataset = datasets.load_dataset("oscar", name="unshuffled_deduplicated_no", split="train")

tokenizer = SentencePieceUnigramTokenizer(unk_token="<unk>", eos_token="</s>", pad_token="<pad>")

# Build an iterator over this dataset
def batch_iterator(input_sentence_size=None):
    if input_sentence_size is None:
        input_sentence_size = len(dataset)
    batch_length = 100
    for i in range(0, input_sentence_size, batch_length):
        yield dataset[i: i + batch_length]["text"]

# Train tokenizer
tokenizer.train_from_iterator(
    iterator=batch_iterator(input_sentence_size=input_sentence_size),
    vocab_size=vocab_size,
    show_progress=True,
)

# Save files to disk
tokenizer.save("./norwegian-t5-base/tokenizer.json")
```

### Create configuration

Next, we create the model's configuration file. This is as simple
as loading and storing [`**google/t5-v1_1-base**`](https://huggingface.co/google/t5-v1_1-base)
in the local model folder:

```python
from transformers import T5Config

config = T5Config.from_pretrained("google/t5-v1_1-base", vocab_size=tokenizer.get_vocab_size())
config.save_pretrained("./norwegian-t5-base")
```

Great, we have set up our model repository. During training, we will automatically
push the training logs and model weights to the repo.

### Train model

Next we can run the example script to pretrain the model:

```bash
python run_t5_mlm_flax.py \
	--output_dir="./norwegian-t5-base" \
	--model_type="t5" \
	--config_name="./norwegian-t5-base" \
	--tokenizer_name="./norwegian-t5-base" \
	--dataset_name="oscar" \
	--dataset_config_name="unshuffled_deduplicated_no" \
	--max_seq_length="512" \
	--per_device_train_batch_size="32" \
	--per_device_eval_batch_size="32" \
	--adafactor \
	--learning_rate="0.005" \
	--weight_decay="0.001" \
	--warmup_steps="2000" \
	--overwrite_output_dir \
	--logging_steps="500" \
	--save_steps="10000" \
	--eval_steps="2500" \
	--push_to_hub
```

Training should converge at a loss and accuracy
of 2.36 and 57.0 respectively after 3 epochs on a single TPUv3-8.
This should take around 4.5 hours.
Training statistics can be accessed on directly on the 🤗 [hub](https://huggingface.co/patrickvonplaten/t5-base-norwegian/tensorboard)

## BART: Denoising language modeling

In the following, we demonstrate how to train a BART model
using denoising language modeling objective as introduced in [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://arxiv.org/abs/1910.13461).
More specifically, we demonstrate how JAX/Flax can be leveraged
to pre-train [**`bart-base`**](https://huggingface.co/facebook/bart-base)
in Norwegian on a single TPUv3-8 pod.

The example script uses the 🤗 Datasets library. You can easily customize them to your needs if you need extra processing on your datasets.

To setup all relevant files for training, let's create a directory.

```bash
mkdir ./norwegian-bart-base
```

### Train tokenizer
In the first step, we train a tokenizer to efficiently process the text input for the model. Similar to how it is shown in [How to train a new language model from scratch using Transformers and Tokenizers](https://huggingface.co/blog/how-to-train), we use a **`ByteLevelBPETokenizer`**.
The tokenizer is trained on the complete Norwegian dataset of OSCAR
and consequently saved in the cloned model directory.
This can take up to 10 minutes depending on your hardware ☕.

```python
from datasets import load_dataset
from tokenizers import trainers, Tokenizer, normalizers, ByteLevelBPETokenizer

# load dataset
dataset = load_dataset("oscar", "unshuffled_deduplicated_no", split="train")

# Instantiate tokenizer
tokenizer = ByteLevelBPETokenizer()

def batch_iterator(batch_size=1000):
    for i in range(0, len(dataset), batch_size):
        yield dataset[i: i + batch_size]["text"]

# Customized training
tokenizer.train_from_iterator(batch_iterator(), vocab_size=50265, min_frequency=2, special_tokens=[
    "<s>",
    "<pad>",
    "</s>",
    "<unk>",
    "<mask>",
])

# Save files to disk
tokenizer.save("./norwegian-bart-base/tokenizer.json")
```

### Create configuration

Next, we create the model's configuration file. This is as simple
as loading and storing [`**facebook/bart-base**`](https://huggingface.co/facebook/bart-base)
in the local model folder:

```python
from transformers import BartConfig
config = BartConfig.from_pretrained("facebook/bart-base", vocab_size=50265)
config.save_pretrained("./norwegian-bart-base")
```

Great, we have set up our model repository. During training, we will automatically
push the training logs and model weights to the repo.

### Train model

Next we can run the example script to pretrain the model:

```bash
python run_bart_dlm_flax.py \
    --output_dir="./norwegian-bart-base" \
    --config_name="./norwegian-bart-base" \
    --tokenizer_name="./norwegian-bart-base" \
    --dataset_name="oscar" \
    --dataset_config_name="unshuffled_deduplicated_no" \
    --max_seq_length="1024" \
    --per_device_train_batch_size="32" \
    --per_device_eval_batch_size="32" \
    --learning_rate="1e-4" \
    --warmup_steps="2000" \
    --overwrite_output_dir \
    --logging_steps="500" \
    --save_steps="2000" \
    --eval_steps="2000" \
    --push_to_hub
```

Training should converge at a loss and accuracy
of 1.36 and 0.77 respectively after 3 epochs on a single TPUv3-8.
This should take less than 6 hours.
Training statistics can be accessed on [tfhub.dev](https://tensorboard.dev/experiment/Maw62QlaSXWS0MOf2V2lbg/).

## Runtime evaluation

We also ran masked language modeling using PyTorch/XLA on a TPUv3-8, and PyTorch on 8 V100 GPUs. We report the
overall training time below.
For reproducibility, we state the training commands used for PyTorch/XLA and PyTorch further below.

| Task  | [TPU v3-8 (Flax)](https://tensorboard.dev/experiment/GdYmdak2TWeVz0DDRYOrrg/)  | [TPU v3-8 (Pytorch/XLA)](https://tensorboard.dev/experiment/7Jq1kcQQRAmy12KOdXek7A/)| [8 GPU (PyTorch)](https://tensorboard.dev/experiment/PJneV8FQRxa2unPw1QnVHA)  |
|-------|-----------|------------|------------|
| MLM   |  15h32m   |  23h46m    | 44h14m     |

*All experiments are ran on Google Cloud Platform.
GPU experiments are ran without further optimizations besides JAX
transformations. GPU experiments are ran with full precision (fp32). "TPU v3-8"
are 8 TPU cores on 4 chips (each chips has 2 cores), while "8 GPU" are 8 GPU chips.

### Script to run MLM with PyTorch/XLA on TPUv3-8

For comparison one can run the same pre-training with PyTorch/XLA on TPU. To set up PyTorch/XLA on Cloud TPU VMs, please
refer to [this](https://cloud.google.com/tpu/docs/pytorch-xla-ug-tpu-vm) guide.
Having created the tokenzier and configuration in `norwegian-roberta-base`, we create the following symbolic links:

```bash
ln -s ~/transformers/examples/pytorch/language-modeling/run_mlm.py ./
ln -s ~/transformers/examples/pytorch/xla_spawn.py ./
```

, set the following environment variables:

```bash
export XRT_TPU_CONFIG="localservice;0;localhost:51011"
unset LD_PRELOAD

export NUM_TPUS=8
export TOKENIZERS_PARALLELISM=0
export MODEL_DIR="./norwegian-roberta-base"
mkdir -p ${MODEL_DIR}
```

, and start training as follows:

```bash
python3 xla_spawn.py --num_cores ${NUM_TPUS} run_mlm.py --output_dir="./runs" \
    --model_type="roberta" \
    --config_name="${MODEL_DIR}" \
    --tokenizer_name="${MODEL_DIR}" \
    --dataset_name="oscar" \
    --dataset_config_name="unshuffled_deduplicated_no" \
    --max_seq_length="128" \
    --weight_decay="0.01" \
    --per_device_train_batch_size="128" \
    --per_device_eval_batch_size="128" \
    --learning_rate="3e-4" \
    --warmup_steps="1000" \
    --overwrite_output_dir \
    --num_train_epochs="18" \
    --adam_beta1="0.9" \
    --adam_beta2="0.98" \
    --do_train \
    --do_eval \
    --logging_steps="500" \
    --evaluation_strategy="epoch" \
    --report_to="tensorboard" \
    --save_strategy="no"
```

### Script to compare pre-training with PyTorch on 8 GPU V100's

For comparison you can run the same pre-training with PyTorch on GPU. Note that we have to make use of `gradient_accumulation`
because the maximum batch size that fits on a single V100 GPU is 32 instead of 128.
Having created the tokenzier and configuration in `norwegian-roberta-base`, we create the following symbolic links:

```bash
ln -s ~/transformers/examples/pytorch/language-modeling/run_mlm.py ./
```

, set some environment variables:

```bash
export NUM_GPUS=8
export TOKENIZERS_PARALLELISM=0
export MODEL_DIR="./norwegian-roberta-base"
mkdir -p ${MODEL_DIR}
```

, and can start training as follows:

```bash
python3 -m torch.distributed.launch --nproc_per_node ${NUM_GPUS} run_mlm.py \
    --output_dir="${MODEL_DIR}" \
    --model_type="roberta" \
    --config_name="${MODEL_DIR}" \
    --tokenizer_name="${MODEL_DIR}" \
    --dataset_name="oscar" \
    --dataset_config_name="unshuffled_deduplicated_no" \
    --max_seq_length="128" \
    --weight_decay="0.01" \
    --per_device_train_batch_size="32" \
    --per_device_eval_batch_size="32" \
    --gradient_accumulation="4" \
    --learning_rate="3e-4" \
    --warmup_steps="1000" \
    --overwrite_output_dir \
    --num_train_epochs="18" \
    --adam_beta1="0.9" \
    --adam_beta2="0.98" \
    --do_train \
    --do_eval \
    --logging_steps="500" \
    --evaluation_strategy="steps" \
    --report_to="tensorboard" \
    --save_strategy="no"
```


text:
<!DOCTYPE html>

  <link href="/app.css" media="screen" rel="stylesheet" type="text/css">
  <title>Incompatible Browser</title>

Incompatible Browser

    We're sorry. This website uses cutting-edge websocket technology which is currently only supported by
    Chrome or Safari (Mac users only).

Please download one of the modern browsers above then revisit this site for an amazing real time experience.

redpajama_set_name: RedPajamaGithub


text:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<filesystem>
    <folder name="Actions">
        <folder name="Tools">
            <file name="net-java-nboglpack-glcapabilities-OpenGLCapabilitiesAction.instance"/>
        </folder>
    </folder>
    <folder name="Menu">
        <folder name="Tools">
            <file name="net-java-nboglpack-glcapabilities-OpenGLCapabilitiesAction.instance">
                <attr name="position" intvalue="1210"/>
            </file>
        </folder>
    </folder>
</filesystem>


text:
<?php

namespace Application\Models\Db;

use Application\Models\Db\Article;
use Application\Models\Db\Category;
use Application\Models\Db\Location;
use Application\Models\Db\User;
use Shareshop\Application;

/**
 * ****************************************************************************
 * Database access class - performs all database actions.
 * ****************************************************************************
 */
class DBAccess {
	protected $_conn;
	protected static $_instance = null;

	/**
	 *
	 * @return \Application\Models\Db\DBAccess
	 */
	public static function getInstance() {
		if (self::$_instance === null) {
			self::$_instance = new self ();
		}
		return self::$_instance;
	}

	private function __construct() {
		$config = Application::getConfig ();
		try {
			$this->_conn = new \PDO ( 'mysql:host=' . $config->db->host . ';dbname=' . $config->db->database, $config->db->user, $config->db->password );
			$this->_conn->setAttribute ( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
			$this->_conn->exec("set names utf8");
		} catch ( \PDOException $e ) {
			echo $e->getMessage ();
		}
	}

	// ------------------------ Helpers ---------------------------- //
	private function createArticleFromDatabaseRow($row) {
		$article = Article::create ()->setId ( $row->art_id )->setName ( $row->art_name )->setDescription ( $row->art_description )->setImage ( $row->art_image )->setUserId( $row->art_usr_id );
		//print_r($this->findCategoriesByArticle($article->getId()));
		$article->setCategories($this->findCategoriesByArticle($article->getId()));
		return $article;
	}

	private function createCategoryFromDatabaseRow($row) {
		$category = Category::create ()->setId ( $row->cat_id )->setName ( $row->cat_name )->setParentId ( $row->cat_parent_id );
		return $category;
	}

	private function createLocationFromDatabaseRow($row) {
		$location = Location::create ()->setId( $row->loc_id )->setStreet($row->loc_street)->setPostcode($row->loc_postcode)->setTown($row->loc_town)->setMapLat($row->loc_mapLat)->setMapLng($row->loc_mapLng);
		return $location;
	}

	// ------------------------ Article ---------------------------- //
	public function loadArticles($arrArticleIds) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_articles WHERE art_id IN (:ids)' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':ids', implode(',', $arrArticleIds) );
			$stmt->execute ();

			$articles = array();
			while ( $row = $stmt->fetch () ) {
				$articles [] = $this->createArticleFromDatabaseRow ($row);
			}
			return $articles;

		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function searchForArticles($arrSearchParams) {
		try {
			$paramBindings=array();
			$queryString='';
			foreach ( $arrSearchParams as $param ) {
				$queryString .= 'art_' . $param->getField() . ' LIKE :' . $param->getField();
				$queryString .= ' OR ';
				$paramBindings[':' . $param->getField()] = '%' . $param->getSearchString() . '%';
			}
			$queryString = substr($queryString, 0,-3);

			$stmt = $this->_conn->prepare( 'SELECT * FROM sha_articles WHERE ' . $queryString );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->execute ($paramBindings);

			$articles = array();
			$index = 0;
			while ( $row = $stmt->fetch () ) {
				$articles[$index++] = $this->createArticleFromDatabaseRow ( $row );
			}

			return $articles;

		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findArticleById($id) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_articles WHERE art_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createArticleFromDatabaseRow ( $row );
			}

			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findArticlesByCategoryId($id) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_art_cat_rel WHERE cat_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();

			$articles = array();
			$index = 0;
			while ( $row = $stmt->fetch () ) {
				$articles[$index++] = $this->findArticleById($row->art_id);
			}
			return $articles;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findArticlesByUserId($id) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_articles WHERE art_usr_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();

			$articles = array();
			while ( $row = $stmt->fetch () ) {
				$articles [] = $this->createArticleFromDatabaseRow ($row);
			}
			return $articles;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function saveArticle($article) {
		$success = false;
		try {
			$stmt = $this->_conn->prepare ( 'INSERT INTO sha_articles VALUES(:id, :name, :description, :image, :art_usr_id, null)' );

			$stmt->bindParam ( ':id', $article->getId () );
			$stmt->bindParam ( ':name', $article->getName () );
			$stmt->bindParam ( ':description', $article->getDescription () );
			$stmt->bindParam ( ':image', $article->getImage () );
			$stmt->bindParam ( ':art_usr_id', $article->getUserId ());

			$success = $stmt->execute ();
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
		if ($success) {
			$this->insertCategoryRelation($this->_conn->lastInsertId(), $article->getCategories ());
		}
	}

	public function modifyArticle($article) {
		$success = false;
		$this->deleteCategoryRelation($article->getId());

		try {
			$stmt = $this->_conn->prepare ( 'UPDATE sha_articles SET art_name = :name, art_description = :description, art_image = :image, art_usr_id = :art_usr_id WHERE art_id = :id' );

			$stmt->bindParam ( ':id', $article->getId () );
			$stmt->bindParam ( ':name', $article->getName () );
			$stmt->bindParam ( ':description', $article->getDescription () );
			$stmt->bindParam ( ':image', $article->getImage () );
			$stmt->bindParam ( ':art_usr_id', $article->getUserId ());

			$success = $stmt->execute ();
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
		if ($success) {
			$this->insertCategoryRelation($article->getId(), $article->getCategories ());
		}
	}

	public function deleteArticle($article) {
		$success = false;
		$id = $article->getId();
		$this->deleteCategoryRelation($id);
		try {
			$stmt = $this->_conn->prepare ( 'DELETE FROM sha_articles WHERE art_id = :id' );

			$stmt->bindParam ( ':id', $id );

			$success = $stmt->execute ();
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}

	}

	private function insertCategoryRelation($art_id, $arr) {
		try {
			foreach ($arr as $cat) {
				if ($cat->getParentId() != null) {

					$stmt = $this->_conn->prepare ( 'INSERT INTO sha_art_cat_rel VALUES(:id, :art_id, :cat_id)' );

					$id = '';
					$stmt->bindParam ( ':id', $id );
					$stmt->bindParam ( ':art_id', $art_id );
					$stmt->bindParam ( ':cat_id', $cat->getId() );

					$stmt->execute ();
				}
			}
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	private function deleteCategoryRelation($art_id) {
		try {

			$stmt = $this->_conn->prepare ( 'DELETE FROM sha_art_cat_rel WHERE art_id = :id' );

			$stmt->bindParam ( ':id', $art_id );

			$stmt->execute ();
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findAllArticles() {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_articles' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->execute ();

			$articles = array();
			while ( $row = $stmt->fetch () ) {
				$articles [] = $this->createArticleFromDatabaseRow ( $row );
			}
			return $articles;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function deleteAllArticles() {
		try {
			$stmt = $this->_conn->prepare ( 'DELETE FROM sha_articles' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->execute ();

		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	// ------------------------ Category ---------------------------- //
	public function findCategoryById($id) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_categories WHERE cat_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createCategoryFromDatabaseRow ( $row );
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function saveCategory($category) {
		try {
			$stmt = $this->_conn->prepare ( 'INSERT INTO sha_categories VALUES(:id, :name, :parentId)' );

			$stmt->execute ( array (
					':id' => $category->getId (),
					':name' => $category->getName (),
					':parentId' => $category->getParentId ()
			) );
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findAllCategories() {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_categories' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->execute ();

			$categories = array();
			while ( $row = $stmt->fetch () ) {
				$categories [] = $this->createCategoryFromDatabaseRow ( $row );
			}
			return $categories;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}
	public function findParentCategories() {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_categories WHERE cat_parent_id IS NULL' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->execute ();

			$categories = array();
			while ( $row = $stmt->fetch () ) {
				$categories [] = $this->createCategoryFromDatabaseRow ( $row );
			}
			return $categories;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findSubCategories($id) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_categories WHERE cat_parent_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );
			$stmt->execute ();

			$categories = array();
			while ( $row = $stmt->fetch () ) {
				$categories [] = $this->createCategoryFromDatabaseRow ( $row );
			}
			return $categories;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	private function findCategoriesByArticle($id) {
		try {

			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_art_cat_rel WHERE art_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );
			$stmt->execute ();

			$categories = array();
			$index = 0;
			while ( $row = $stmt->fetch () ) {
				$categories[$index++] = $this->findCategoryById($row->cat_id);
			}
			return $categories;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}
	// ------------------------ Location ---------------------------- //
	public function findLocationById($id) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_location WHERE loc_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createLocationFromDatabaseRow ( $row );
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findLocationByPostCode($postcode) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_location WHERE loc_postcode=:postcode' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':postcode', $postcode );

			$stmt->execute ();
			$locations = array();
			while ( $row = $stmt->fetch () ) {
				$locations [] = $this->createLocationFromDatabaseRow ( $row );
			}
			return $locations;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findLocationNearBy($lng, $lat) {
		try {
			$lngFrom = $lng - 0.07;
			$lngTo = $lng + 0.07;
			$latFrom = $lat - 0.07;
			$latTo = $lat + 0.07;
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_location WHERE loc_mapLng BETWEEN :lngFrom AND :lngTo AND loc_mapLat BETWEEN :latFrom AND :latTo ' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':lngFrom', $lngFrom );
			$stmt->bindParam ( ':lngTo', $lngTo );
			$stmt->bindParam ( ':latFrom', $latFrom );
			$stmt->bindParam ( ':latTo', $latTo );
			$stmt->execute ();
			$locations = array();
			while ( $row = $stmt->fetch () ) {
				$locations [] = $this->createLocationFromDatabaseRow ( $row );
			}
			return $locations;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function saveLocation($location) {
		try {
			$stmt = $this->_conn->prepare ( 'INSERT INTO sha_location VALUES(:id, :street, :postcode, :town, :mapLat, :mapLng)' );
			$stmt->execute ( array (
					':id' => $location->getId (),
					':street' => $location->getStreet(),
					':postcode' => $location->getPostcode(),
					':town' => $location->getTown(),
					':mapLat' => $location->getMapLat(),
					':mapLng' => $location->getMapLng()
			) );
			return $this->_conn->lastInsertId();
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function modifyLocation($location) {
		try {
			$stmt = $this->_conn->prepare ( 'UPDATE sha_location SET loc_street = :street, loc_postcode = :postcode, loc_town = :town, loc_mapLat = :mapLat, loc_mapLng = :mapLng WHERE loc_id = :id' );
			$stmt->execute ( array (
					':id' => $location->getId (),
					':street' => $location->getStreet(),
					':postcode' => $location->getPostcode(),
					':town' => $location->getTown(),
					':mapLat' => $location->getMapLat(),
					':mapLng' => $location->getMapLng()
			) );
			return $this->_conn->lastInsertId();
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findAllLocations() {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_location' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->execute ();

			while ( $row = $stmt->fetch () ) {
				$locations [] = $this->createLocationFromDatabaseRow ( $row );
			}
			return $locations;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	// ------------------------ User ---------------------------- //

	/**
	 * Returns the user data as a User object.
	 *
	 * @param string $username
	 * @return \Application\Models\Db\User|NULL
	 */
	public function findUserByUsername($username) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_user WHERE usr_username=:username' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':username', $username );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createUserFromDatabaseRow($row);
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findUserById($id) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_user WHERE usr_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createUserFromDatabaseRow($row);
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findUserBySession($id)
	{
		try {
			$stmt = $this->_conn->prepare ( 'SELECT u.* FROM sha_session AS s JOIN sha_user AS u ON (session_id=:id AND u.usr_id = s.session_usr_id) ORDER BY session_create_time DESC LIMIT 1' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createUserFromDatabaseRow($row);
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findUserByLocId($locid) {
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_user WHERE usr_loc_id=:locid' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':locid', $locid );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createUserFromDatabaseRow($row);
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	/**
	 * Saves a user based on the given User object.
	 *
	 * @param User $user
	 */
	public function saveUser(User $user)
	{
		try {
			$stmt = $this->_conn->prepare ( 'INSERT INTO sha_user (usr_username, usr_password, usr_email, usr_salt, usr_language, usr_loc_id) VALUES (:username, :password, :email, :salt, :language, :usr_loc_id)' );

			$stmt->execute ( array (
					':username' => $user->getUsername(),
					':password' => $user->getPassword(),
					':email' => $user->getEmail(),
					':salt' => $user->getSalt(),
					':language' => $user->getLanguage(),
					':usr_loc_id' => $user->getLocId()
			) );
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	/**
	 * Updates a user based on the given User object.
	 *
	 * @param User $user
	 */
	public function modifyUser(User $user)
	{
		try {
			$stmt = $this->_conn->prepare ( 'UPDATE sha_user SET usr_password = :password, usr_salt = :salt, usr_email = :email, usr_language = :language, usr_loc_id = :usr_loc_id WHERE usr_id = :id' );

			$stmt->execute ( array (
					':id' => $user->getId(),
					':password' => $user->getPassword(),
					':salt' => $user->getSalt(),
					':email' => $user->getEmail(),
					':language' => $user->getLanguage(),
					':usr_loc_id' => $user->getLocId()
			) );
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	/**
	 * Creates a User object from a database row.
	 *
	 * @param \stdClass $row
	 * @return User
	 */
	private function createUserFromDatabaseRow($row) {
		$user = User::create()->setId($row->usr_id)->setUsername($row->usr_username)->setPassword($row->usr_password)->setSalt($row->usr_salt)->setEmail($row->usr_email)->setLanguage($row->usr_language)->setLocationId($row->usr_loc_id);
		return $user;
	}

	public function findSessionById($id)
	{
		try {
			$stmt = $this->_conn->prepare ( 'SELECT * FROM sha_session WHERE session_id=:id' );
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createSessionFromDatabaseRow ( $row );
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	/**
	 * Saves or updates a session based on the given Session object.
	 *
	 * @param Session $session
	 */
	public function saveSession(Session $session)
	{
		try {
			$stmt = $this->_conn->prepare ( 'REPLACE INTO sha_session (session_id, session_usr_id, session_state, session_update_time) VALUES (:session, :user, :state, :updateTime)' );

			$stmt->execute ( array (
					':session' => $session->getId(),
					':user' => $session->getUserId(),
					':state' => $session->getState(),
					':updateTime' => 'CURRENT_TIMESTAMP'
			) );
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	/**
	 * Creates a Session object from a database row.
	 *
	 * @param \stdClass $row
	 * @return Session
	 */
	private function createSessionFromDatabaseRow($row) {
		$session = Session::create()
			->setId($row->session_id)
			->setCreateTime($row->session_create_time)
			->setUpdateTime($row->session_update_time)
			->setState($row->session_state)
			->setUserId($row->session_usr_id);
		return $session;
	}

	// ------------------------ Exchange ---------------------------- //

	public function findExchangeById($id)
	{
		try {
			$stmt = $this->_conn->prepare (
					'SELECT * FROM sha_exchange'
					.' WHERE exchange_id = :id'
			);
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $id );

			$stmt->execute ();
			$row = $stmt->fetch();

			if ($row != null) {
				return $this->createExchangeFromDatabaseRow( $row );
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findExchangeByUser($userId)
	{
		try {
			$stmt = $this->_conn->prepare (
					'SELECT * FROM sha_exchange'
					.' WHERE answering_user = :id OR requesting_user = :id'
			);
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $userId );

			$stmt->execute ();

			$exchanges = array();
			if ($stmt->rowCount() > 0) {
				while (($row = $stmt->fetch()) != null) {
					$exchanges[] = $this->createExchangeFromDatabaseRow( $row );
				}
			}
			return $exchanges;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findActiveExchangeByArticleAndUser($userId, $articleId)
	{
		try {
			$stmt = $this->_conn->prepare (
					'SELECT e.* FROM sha_exchange AS e JOIN ('
					.' SELECT s.exchange_id FROM sha_exchange_step AS s JOIN sha_exchange_step_item AS i '
					.' ON (i.step_id = s.step_id AND i.art_id = :articleId) ) AS si '
					.' ON (e.exchange_id = si.exchange_id AND e.requesting_user = :userId AND state = 0)'
			);
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':userId', $userId );
			$stmt->bindParam ( ':articleId', $articleId );

			$stmt->execute ();
			$row = $stmt->fetch ();

			if ($row != null) {
				return $this->createExchangeFromDatabaseRow( $row );
			}
			return null;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findExchangeStepsByExchange($exchangeId)
	{
		try {
			$stmt = $this->_conn->prepare (
					'SELECT * FROM sha_exchange_step'
					.' WHERE exchange_id = :id ORDER BY step_created DESC'
			);
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $exchangeId );

			$stmt->execute ();

			$steps = array();
			if ($stmt->rowCount() > 0) {
				while (($row = $stmt->fetch()) != null) {
					$steps[] = $this->createExchangeStepFromDatabaseRow( $row );
				}
			}
			return $steps;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	public function findArticlesIdByExchangeStep($stepId)
	{
		try {
			$stmt = $this->_conn->prepare (
					'SELECT a.* FROM sha_articles AS a JOIN sha_exchange_step_item AS es'
					.' ON (a.art_id = es.art_id AND es.step_id = :id)'
			);
			$stmt->setFetchMode ( \PDO::FETCH_OBJ );
			$stmt->bindParam ( ':id', $stepId );

			$stmt->execute ();

			$articles = array();
			if ($stmt->rowCount() > 0) {
				while (($row = $stmt->fetch()) != null) {
					$articles[] = $this->createArticleFromDatabaseRow( $row );
				}
			}
			return $articles;
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	/**
	 * Saves an exchange based on the given Exchange object.
	 *
	 * @param Exchange $exchange
	 * @return int The insert id.
	 */
	public function saveExchange(Exchange $exchange)
	{
		try {
			$stmt = $this->_conn->prepare (
					'INSERT INTO sha_exchange'
					. ' (exchange_id, requesting_user, answering_user, requesting_rating, answering_rating, state)'
					. ' VALUES (:id, :requestingUser, :answeringUser, :requestingRating, :answeringRating, :state)' );

			$stmt->execute ( array (
					':id' => $exchange->getId(),
					':requestingUser' => $exchange->getRequestingUser()->getId(),
					':answeringUser' => $exchange->getAnsweringUser()->getId(),
					':requestingRating' => $exchange->getRequestingRating(),
					':answeringRating' => $exchange->getAnsweringRating(),
					':state' => $exchange->getState()
			) );
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
		return $this->_conn->lastInsertId();
	}

	/**
	 * Updates an exchange based on the given Exchange object.
	 *
	 * @param Exchange $exchange
	 */
	public function modifyExchange(Exchange $exchange)
	{
		try {
			$stmt = $this->_conn->prepare (
					'UPDATE sha_exchange SET'
					.' requesting_rating = :requestingRating,'
					.' answering_rating = :answeringRating, state = :state'
					.' WHERE exchange_id = :id '
			);

			$stmt->execute ( array (
					':id' => $exchange->getId(),
					':requestingRating' => $exchange->getRequestingRating(),
					':answeringRating' => $exchange->getAnsweringRating(),
					':state' => $exchange->getState()
			) );
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	/**
	 * Saves an exchange step based on the given ExchangeStep object.
	 *
	 * @param ExchangeStep $step
	 * @return int The last insert id.
	 */
	public function saveExchangeStep(ExchangeStep $step)
	{
		try {
			$stmt = $this->_conn->prepare (
					'REPLACE INTO sha_exchange_step'
					. ' (exchange_id, step_created, step_remark, step_type)'
					. ' VALUES (:exchangeId, :created, :remark, :type)' );

			$stmt->execute ( array (
					':exchangeId' => $step->getExchangeId(),
					':created' => $step->getCreated(),
					':remark' => $step->getRemark(),
					':type' => $step->getType()
			) );
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
		return $this->_conn->lastInsertId();
	}

	/**
	 * Saves the items of an exchange step based on the given ExchangeStep object.
	 *
	 * @param ExchangeStep $step
	 * @param int $articleId
	 */
	public function saveExchangeStepArticles(ExchangeStep $step, $articleId)
	{
		try {
			$stmt = $this->_conn->prepare (
					'INSERT INTO sha_exchange_step_item'
					. ' (step_id, art_id)'
					. ' VALUES (:stepId, :artId)' );

			$stmt->execute ( array (
					':stepId' => $step->getId(),
					':artId' => $articleId,
			) );
		} catch ( \PDOException $e ) {
			echo 'Error: ' . $e->getMessage ();
		}
	}

	/**
	 * Creates an Exchange object from a database row.
	 *
	 * @param \stdClass $row
	 * @return Exchange
	 */
	private function createExchangeFromDatabaseRow($row) {
		$exchange = Exchange::create()
		->setId($row->exchange_id)
		->setRequestingUser($row->requesting_user)
		->setAnsweringUser($row->answering_user)
		->setRequestingRating($row->requesting_rating)
		->setAnsweringRating($row->answering_rating)
		->setState($row->state);
		return $exchange;
	}

	/**
	 * Creates an ExchangeStep object from a database row.
	 *
	 * @param \stdClass $row
	 * @return ExchangeStep
	 */
	private function createExchangeStepFromDatabaseRow($row) {
		$step = ExchangeStep::create()
		->setId($row->step_id)
		->setExchangeId($row->exchange_id)
		->setCreated($row->step_created)
		->setRemark($row->step_remark)
		->setType($row->step_type);
		return $step;
	}

}
?>


text:
using System;

namespace Studiotaiha.Toolkit.Rest.Requests
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
	public sealed class ResultParserAttribute : Attribute
	{
		public ResultParserAttribute(
			Type parserType,
			bool forSuccess,
			bool forFailure)
		{
			ParserType = parserType;
			ForSucccess = forSuccess;
			ForFailure = forFailure;
		}

		public Type ParserType { get; }
		public bool ForSucccess { get; }
		public bool ForFailure { get; }
	}
}


text:
package cn.howardliu.gear;

import org.apache.commons.io.Charsets;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

/**
 * <br>created at 16-10-10
 *
 * @author liuxh
 * @since 1.0.0
 */
@Ignore
public class SpringHttpTest {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Test
    public void test() throws Exception {
        String url = "http://10.6.4.22:8042/pcm-inner-sdc/pcmInnerSupplyInfo/getSupCodesFromPcmByShopCodeAndManager.htm";
//        String url = "http://10.6.3.214:8042/pcm-inner-sdc/pcmInnerSupplyInfo/getSupCodesFromPcmByShopCodeAndManager.htm";
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        ClientHttpRequest client = factory.createRequest(new URI(url), HttpMethod.POST);
        client.getHeaders().set(HttpHeaders.CONTENT_TYPE, "application/json");
        String params = "{\"paraList\":[{\"shopCode\":\"D001\",\"managerList\":[{\"level\":\"1\",\"manager\":\"02\"},{\"level\":\"1\",\"manager\":\"04\"},{\"level\":\"1\",\"manager\":\"08\"},{\"level\":\"1\",\"manager\":\"10\"},{\"level\":\"1\",\"manager\":\"90\"},{\"level\":\"2\",\"manager\":\"0101\"},{\"level\":\"2\",\"manager\":\"0702\"},{\"level\":\"2\",\"manager\":\"0703\"},{\"level\":\"2\",\"manager\":\"0704\"},{\"level\":\"2\",\"manager\":\"0716\"},{\"level\":\"2\",\"manager\":\"0717\"}]}]}";
        client.getBody().write(params.getBytes(Charsets.UTF_8));
        ClientHttpResponse res = client.execute();
        BufferedReader br = new BufferedReader(new InputStreamReader(res.getBody()));
        StringBuilder result = new StringBuilder();
        String str;
        while ((str = br.readLine()) != null) {
            result.append(str).append("\n");
        }
        System.out.println(result.toString());
    }
}


text:
package freemarker.core;

import java.text.NumberFormat;

import freemarker.template.TemplateDateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNumberModel;

/**
 * Represents a number format; used in templates for formatting and parsing with that format. This is similar to Java's
 * {@link NumberFormat}, but made to fit the requirements of FreeMarker. Also, it makes easier to define formats that
 * can't be represented with Java's existing {@link NumberFormat} implementations.
 *
 *

 * Implementations need not be thread-safe if the {@link TemplateNumberFormatFactory} doesn't recycle them among
 * different {@link Environment}-s. As far as FreeMarker's concerned, instances are bound to a single
 * {@link Environment}, and {@link Environment}-s are thread-local objects.
 *
 * @since 2.3.24
 */
public abstract class TemplateNumberFormat extends TemplateValueFormat {

    /**
     * @param numberModel
     *            The number to format; not {@code null}. Most implementations will just work with the return value of
     *            {@link TemplateDateModel#getAsDate()}, but some may format differently depending on the properties of
     *            a custom {@link TemplateDateModel} implementation.
     *
     * @return The number as text, with no escaping (like no HTML escaping); can't be {@code null}.
     *
     * @throws TemplateValueFormatException
     *             If any problem occurs while parsing/getting the format. Notable subclass:
     *             {@link UnformattableValueException}.
     * @throws TemplateModelException
     *             Exception thrown by the {@code dateModel} object when calling its methods.
     */
    public abstract String formatToPlainText(TemplateNumberModel numberModel)
            throws TemplateValueFormatException, TemplateModelException;

    /**
     * Formats the model to markup instead of to plain text if the result markup will be more than just plain text
     * escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result of
     * {@link #formatToPlainText(TemplateNumberModel)} escaped, it must return the {@link String} that
     * {@link #formatToPlainText(TemplateNumberModel)} does.
     *
     *

     * The implementation in {@link TemplateNumberFormat} simply calls {@link #formatToPlainText(TemplateNumberModel)}.
     *
     * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not {@code null}.
     */
    public Object format(TemplateNumberModel numberModel)
            throws TemplateValueFormatException, TemplateModelException {
        return formatToPlainText(numberModel);
    }

    /**
     * Tells if this formatter should be re-created if the locale changes.
     */
    public abstract boolean isLocaleBound();

    /**
     * This method is reserved for future purposes; currently it always throws {@link ParsingNotSupportedException}. We
     * don't yet support number parsing with {@link TemplateNumberFormat}-s, because currently FTL parses strings to
     * number with the {@link ArithmeticEngine} ({@link TemplateNumberFormat} were only introduced in 2.3.24). If it
     * will be support, it will be similar to {@link TemplateDateFormat#parse(String, int)}.
     */
    public final Object parse(String s) throws TemplateValueFormatException {
        throw new ParsingNotSupportedException("Number formats currenly don't support parsing");
    }

}


text:
// Generated by CoffeeScript 1.3.3
(function() {
  var balUtilFlow,
    __slice = [].slice,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  balUtilFlow = {
    wait: function(delay, fn) {
      return setTimeout(fn, delay);
    },
    extractOptsAndCallback: function(opts, next) {
      if (typeof opts === 'function' && (next != null) === false) {
        next = opts;
        opts = {};
      } else {
        opts || (opts = {});
      }
      next || (next = opts.next || null);
      return [opts, next];
    },
    fireWithOptionalCallback: function(method, args, context) {
      var callback, err, result;
      args || (args = []);
      callback = args[args.length - 1];
      context || (context = null);
      result = null;
      if (method.length === args.length) {
        try {
          result = method.apply(context, args);
        } catch (caughtError) {
          callback(caughtError);
        }
      } else {
        err = null;
        try {
          result = method.apply(context, args);
          if (result instanceof Error) {
            err = result;
          }
        } catch (caughtError) {
          err = caughtError;
        }
        callback(err, result);
      }
      return result;
    },
    toString: function(obj) {
      return Object.prototype.toString.call(obj);
    },
    isArray: function(obj) {
      return this.toString(obj) === '[object Array]';
    },
    extend: function() {
      var key, obj, objs, target, value, _i, _len;
      target = arguments[0], objs = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
      target || (target = {});
      for (_i = 0, _len = objs.length; _i < _len; _i++) {
        obj = objs[_i];
        obj || (obj = {});
        for (key in obj) {
          if (!__hasProp.call(obj, key)) continue;
          value = obj[key];
          target[key] = value;
        }
      }
      return target;
    },
    clone: function() {
      var args, target;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      target = {};
      args.unshift(target);
      balUtilFlow.extend.apply(balUtilFlow, args);
      return target;
    },
    each: function(obj, callback, context) {
      var broke, item, key, _i, _len;
      broke = false;
      context || (context = obj);
      if (this.isArray(obj)) {
        for (key = _i = 0, _len = obj.length; _i < _len; key = ++_i) {
          item = obj[key];
          if (callback.call(context, item, key, obj) === false) {
            broke = true;
            break;
          }
        }
      } else {
        for (key in obj) {
          if (!__hasProp.call(obj, key)) continue;
          item = obj[key];
          if (callback.call(context, item, key, obj) === false) {
            broke = true;
            break;
          }
        }
      }
      return this;
    },
    flow: function(opts) {
      var action, actions, args, next, object, tasks;
      object = opts.object, action = opts.action, args = opts.args, tasks = opts.tasks, next = opts.next;
      if (!action) {
        throw new Error('balUtilFlow.flow called without any action');
      }
      actions = action.split(/[,\s]+/g);
      tasks || (tasks = new balUtilFlow.Group(next));
      balUtilFlow.each(actions, function(action) {
        return tasks.push(function(complete) {
          var argsClone, fn;
          argsClone = (args || []).slice();
          argsClone.push(complete);
          fn = object[action];
          return fn.apply(object, argsClone);
        });
      });
      tasks.sync();
      return this;
    }
  };

  /*
  Usage:
  	# Add tasks to a queue then fire them in parallel (asynchronously)
  	tasks = new Group (err) -> next err
  	tasks.push (complete) -> someAsyncFunction(arg1, arg2, complete)
  	tasks.push (complete) -> anotherAsyncFunction(arg1, arg2, complete)
  	tasks.async()

  	# Add tasks to a queue then fire them in serial (synchronously)
  	tasks = new Group (err) -> next err
  	tasks.push (complete) -> someAsyncFunction(arg1, arg2, complete)
  	tasks.push (complete) -> anotherAsyncFunction(arg1, arg2, complete)
  	tasks.sync()
  */

  balUtilFlow.Group = (function() {

    _Class.prototype.total = 0;

    _Class.prototype.completed = 0;

    _Class.prototype.running = 0;

    _Class.prototype.exited = false;

    _Class.prototype.breakOnError = true;

    _Class.prototype.autoClear = false;

    _Class.prototype.queue = [];

    _Class.prototype.mode = 'async';

    _Class.prototype.lastResult = null;

    _Class.prototype.results = [];

    _Class.prototype.errors = [];

    _Class.prototype.next = function() {
      throw new Error('Groups require a completion callback');
    };

    function _Class() {
      var arg, args, autoClear, breakOnError, mode, next, _i, _len;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      this.clear();
      for (_i = 0, _len = args.length; _i < _len; _i++) {
        arg = args[_i];
        if (typeof arg === 'string') {
          this.mode = arg;
        } else if (typeof arg === 'function') {
          this.next = arg;
        } else if (typeof arg === 'object') {
          next = arg.next, mode = arg.mode, breakOnError = arg.breakOnError, autoClear = arg.autoClear;
          if (next) {
            this.next = next;
          }
          if (mode) {
            this.mode = mode;
          }
          if (breakOnError) {
            this.breakOnError = breakOnError;
          }
          if (autoClear) {
            this.autoClear = autoClear;
          }
        } else {
          throw new Error('Unknown argument sent to Groups constructor');
        }
      }
    }

    _Class.prototype.clear = function() {
      this.total = 0;
      this.completed = 0;
      this.running = 0;
      this.exited = false;
      this.queue = [];
      this.results = [];
      this.errors = [];
      this.lastResult = null;
      return this;
    };

    _Class.prototype.hasTasks = function() {
      return this.queue.length !== 0;
    };

    _Class.prototype.hasCompleted = function() {
      return this.total !== 0 && this.total === this.completed;
    };

    _Class.prototype.isRunning = function() {
      return this.running !== 0;
    };

    _Class.prototype.hasExited = function(value) {
      if (value != null) {
        this.exited = value;
      }
      return this.exited === true;
    };

    _Class.prototype.logError = function(err) {
      if (this.errors[this.errors.length - 1] !== err) {
        this.errors.push(err);
      }
      return this;
    };

    _Class.prototype.complete = function() {
      var args, err;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      err = args[0] || void 0;
      this.lastResult = args;
      if (err) {
        this.logError(err);
      }
      this.results.push(args);
      if (this.running !== 0) {
        --this.running;
      }
      if (this.hasExited()) {

      } else {
        if (err && this.breakOnError) {
          this.exit();
        } else {
          ++this.completed;
          if (this.hasTasks()) {
            this.nextTask();
          } else if (this.isRunning() === false && this.hasCompleted()) {
            this.exit();
          }
        }
      }
      return this;
    };

    _Class.prototype.completer = function() {
      var _this = this;
      return function() {
        var args;
        args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
        return _this.complete.apply(_this, args);
      };
    };

    _Class.prototype.exit = function(err) {
      var errors, lastResult, results;
      if (err == null) {
        err = null;
      }
      if (err) {
        this.logError(err);
      }
      if (this.hasExited()) {

      } else {
        lastResult = this.lastResult;
        results = this.results;
        if (this.errors.length === 0) {
          errors = null;
        } else if (this.errors.length === 1) {
          errors = this.errors[0];
        } else {
          errors = this.errors;
        }
        if (this.autoClear) {
          this.clear();
        } else {
          this.hasExited(true);
        }
        this.next(errors, lastResult, results);
      }
      return this;
    };

    _Class.prototype.tasks = function(tasks) {
      var task, _i, _len;
      for (_i = 0, _len = tasks.length; _i < _len; _i++) {
        task = tasks[_i];
        this.push(task);
      }
      return this;
    };

    _Class.prototype.push = function() {
      var args;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      ++this.total;
      this.queue.push(args);
      return this;
    };

    _Class.prototype.pushAndRun = function() {
      var args;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      if (this.mode === 'sync' && this.isRunning()) {
        this.push.apply(this, args);
      } else {
        ++this.total;
        this.runTask(args);
      }
      return this;
    };

    _Class.prototype.nextTask = function() {
      var task;
      if (this.hasTasks()) {
        task = this.queue.shift();
        this.runTask(task);
      }
      return this;
    };

    _Class.prototype.runTask = function(task) {
      var me, run;
      me = this;
      try {
        run = function() {
          var complete, _context, _task;
          ++me.running;
          complete = me.completer();
          if (balUtilFlow.isArray(task)) {
            if (task.length === 2) {
              _context = task[0];
              _task = task[1];
            } else if (task.length === 1) {
              _task = task[0];
              _context = null;
            } else {
              throw new Error('balUtilFlow.Group an invalid task was pushed');
            }
          } else {
            _task = task;
          }
          return balUtilFlow.fireWithOptionalCallback(_task, [complete], _context);
        };
        if (this.completed !== 0 && (this.mode === 'async' || (this.completed % 100) === 0)) {
          setTimeout(run, 0);
        } else {
          run();
        }
      } catch (err) {
        this.complete(err);
      }
      return this;
    };

    _Class.prototype.run = function() {
      var task, _i, _len, _ref;
      if (this.isRunning() === false) {
        this.hasExited(false);
        if (this.hasTasks()) {
          if (this.mode === 'sync') {
            this.nextTask();
          } else {
            _ref = this.queue;
            for (_i = 0, _len = _ref.length; _i < _len; _i++) {
              task = _ref[_i];
              this.nextTask();
            }
          }
        } else {
          this.exit();
        }
      }
      return this;
    };

    _Class.prototype.async = function() {
      this.mode = 'async';
      this.run();
      return this;
    };

    _Class.prototype.sync = function() {
      this.mode = 'sync';
      this.run();
      return this;
    };

    return _Class;

  })();

  balUtilFlow.Block = (function(_super) {

    __extends(_Class, _super);

    _Class.prototype.blockBefore = function(block) {};

    _Class.prototype.blockAfter = function(block, err) {};

    _Class.prototype.blockTaskBefore = function(block, task, err) {};

    _Class.prototype.blockTaskAfter = function(block, task, err) {};

    function _Class(opts) {
      var block, complete, fn, name, parentBlock;
      block = this;
      name = opts.name, fn = opts.fn, parentBlock = opts.parentBlock, complete = opts.complete;
      block.blockName = name;
      if (parentBlock != null) {
        block.parentBlock = parentBlock;
      }
      block.mode = 'sync';
      block.fn = fn;
      _Class.__super__.constructor.call(this, function(err) {
        block.blockAfter(block, err);
        return typeof complete === "function" ? complete(err) : void 0;
      });
      block.blockBefore(block);
      if (block.fn != null) {
        if (block.fn.length === 3) {
          block.total = Infinity;
        }
        try {
          block.fn(function(name, fn) {
            return block.block(name, fn);
          }, function(name, fn) {
            return block.task(name, fn);
          }, function(err) {
            return block.exit(err);
          });
          if (block.fn.length !== 3) {
            block.run();
          }
        } catch (err) {
          block.exit(err);
        }
      } else {
        block.total = Infinity;
      }
      this;

    }

    _Class.prototype.block = function(name, fn) {
      var block, pushBlock;
      block = this;
      pushBlock = function(fn) {
        if (block.total === Infinity) {
          return block.pushAndRun(fn);
        } else {
          return block.push(fn);
        }
      };
      pushBlock(function(complete) {
        var subBlock;
        return subBlock = block.createSubBlock({
          name: name,
          fn: fn,
          complete: complete
        });
      });
      return this;
    };

    _Class.prototype.createSubBlock = function(opts) {
      opts.parentBlock = this;
      return new balUtilFlow.Block(opts);
    };

    _Class.prototype.task = function(name, fn) {
      var block, pushTask;
      block = this;
      pushTask = function(fn) {
        if (block.total === Infinity) {
          return block.pushAndRun(fn);
        } else {
          return block.push(fn);
        }
      };
      pushTask(function(complete) {
        var preComplete;
        preComplete = function(err) {
          block.blockTaskAfter(block, name, err);
          return complete(err);
        };
        block.blockTaskBefore(block, name);
        return balUtilFlow.fireWithOptionalCallback(fn, [preComplete]);
      });
      return this;
    };

    return _Class;

  })(balUtilFlow.Group);

  balUtilFlow.Runner = (function() {

    _Class.prototype.runnerBlock = null;

    function _Class() {
      var _ref;
      if ((_ref = this.runnerBlock) == null) {
        this.runnerBlock = new balUtilFlow.Block();
      }
    }

    _Class.prototype.getRunnerBlock = function() {
      return this.runnerBlock;
    };

    _Class.prototype.block = function() {
      var args, _ref;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      return (_ref = this.getRunnerBlock()).block.apply(_ref, args);
    };

    _Class.prototype.task = function() {
      var args, _ref;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      return (_ref = this.getRunnerBlock()).task.apply(_ref, args);
    };

    return _Class;

  })();

  if (typeof module !== "undefined" && module !== null) {
    module.exports = balUtilFlow;
  } else {
    this.balUtilFlow = balUtilFlow;
  }

}).call(this);


text:
import isObjectLike from './isObjectLike.js';

/** `Object#toString` result references. */
var symbolTag = '[object Symbol]';

/** Used for built-in method references. */
var objectProto = Object.prototype;

/**
 * Used to resolve the
 * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
 * of values.
 */
var objectToString = objectProto.toString;

/**
 * Checks if `value` is classified as a `Symbol` primitive or object.
 *
 * @static
 * @memberOf _
 * @since 4.0.0
 * @category Lang
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is correctly classified,
 *  else `false`.
 * @example
 *
 * _.isSymbol(Symbol.iterator);
 * // => true
 *
 * _.isSymbol('abc');
 * // => false
 */
function isSymbol(value) {
  return typeof value == 'symbol' ||
    (isObjectLike(value) && objectToString.call(value) == symbolTag);
}

export default isSymbol;


text:
pageflow.PageLinkItemView = Backbone.Marionette.ItemView.extend({
  template: 'pageflow/editor/templates/page_link_item',

  tagName: 'li',
  className: 'page_link',

  ui: {
    thumbnail: '.page_thumbnail',
    title: '.title',
    label: '.label',
    editButton: '.edit'
  },

  events: {
    'click .edit': function() {
      pageflow.editor.navigate(this.model.editPath(), {trigger: true});
      return false;
    },

    'mouseenter': function() {
      this.model.highlight(true);
    },

    'mouseleave': function() {
      this.model.resetHighlight(false);
    }
  },

  onRender: function() {
    var page = this.model.targetPage();

    if (page) {
      this.subview(new pageflow.PageThumbnailView({
        el: this.ui.thumbnail,
        model: page
      }));

      this.$el.addClass(page.get('template'));
      this.ui.title.text(page.title() || I18n.t('pageflow.editor.views.page_link_item_view.unnamed'));
    }
    else {
      this.ui.title.text(I18n.t('pageflow.editor.views.page_link_item_view.no_page'));
    }

    this.ui.label.text(this.model.label());
    this.ui.label.toggle(!!this.model.label());
    this.ui.editButton.toggle(!!this.model.editPath());

    this.$el.toggleClass('dangling', !page);
  },
});


text:
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.windows;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.shell.ShellUtils;
import com.google.devtools.build.lib.shell.Subprocess;
import com.google.devtools.build.lib.shell.SubprocessBuilder;
import com.google.devtools.build.lib.testutil.TestSpec;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.runfiles.Runfiles;
import java.io.File;
import java.io.InputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Unit tests for {@link WindowsSubprocess}.
 */
@RunWith(JUnit4.class)
@TestSpec(localOnly = true, supportedOs = OS.WINDOWS)
public class WindowsSubprocessTest {
  private String mockSubprocess;
  private String mockBinary;
  private Subprocess process;
  private Runfiles runfiles;

  @Before
  public void loadJni() throws Exception {
    runfiles = Runfiles.create();
    mockSubprocess =
        runfiles.rlocation(
            "io_bazel/src/test/java/com/google/devtools/build/lib/windows/MockSubprocess_deploy.jar");
    mockBinary = System.getProperty("java.home") + "\\bin\\java.exe";

    process = null;
  }

  @After
  public void terminateProcess() throws Exception {
    if (process != null) {
      process.destroy();
      process.close();
      process = null;
    }
  }

  @Test
  public void testSystemRootIsSetByDefault() throws Exception {
    SubprocessBuilder subprocessBuilder = new SubprocessBuilder(WindowsSubprocessFactory.INSTANCE);
    subprocessBuilder.setWorkingDirectory(new File("."));
    subprocessBuilder.setArgv(ImmutableList.of(mockBinary, "-jar", mockSubprocess, "O$SYSTEMROOT"));
    process = subprocessBuilder.start();
    process.waitFor();
    assertThat(process.exitValue()).isEqualTo(0);

    byte[] buf = new byte[11];
    process.getInputStream().read(buf);
    assertThat(new String(buf, UTF_8).trim()).isEqualTo(System.getenv("SYSTEMROOT").trim());
  }

  @Test
  public void testSystemDriveIsSetByDefault() throws Exception {
    SubprocessBuilder subprocessBuilder = new SubprocessBuilder(WindowsSubprocessFactory.INSTANCE);
    subprocessBuilder.setWorkingDirectory(new File("."));
    subprocessBuilder.setArgv(
        ImmutableList.of(mockBinary, "-jar", mockSubprocess, "O$SYSTEMDRIVE"));
    process = subprocessBuilder.start();
    process.waitFor();
    assertThat(process.exitValue()).isEqualTo(0);

    byte[] buf = new byte[3];
    process.getInputStream().read(buf);
    assertThat(new String(buf, UTF_8).trim()).isEqualTo(System.getenv("SYSTEMDRIVE").trim());
  }

  @Test
  public void testSystemRootIsSet() throws Exception {
    SubprocessBuilder subprocessBuilder = new SubprocessBuilder(WindowsSubprocessFactory.INSTANCE);
    subprocessBuilder.setWorkingDirectory(new File("."));
    subprocessBuilder.setArgv(ImmutableList.of(mockBinary, "-jar", mockSubprocess, "O$SYSTEMROOT"));
    // Case shouldn't matter on Windows
    subprocessBuilder.setEnv(ImmutableMap.of("SystemRoot", "C:\\MySystemRoot"));
    process = subprocessBuilder.start();
    process.waitFor();
    assertThat(process.exitValue()).isEqualTo(0);

    byte[] buf = new byte[16];
    process.getInputStream().read(buf);
    assertThat(new String(buf, UTF_8).trim()).isEqualTo("C:\\MySystemRoot");
  }

  @Test
  public void testSystemDriveIsSet() throws Exception {
    SubprocessBuilder subprocessBuilder = new SubprocessBuilder(WindowsSubprocessFactory.INSTANCE);
    subprocessBuilder.setWorkingDirectory(new File("."));
    subprocessBuilder.setArgv(
        ImmutableList.of(mockBinary, "-jar", mockSubprocess, "O$SYSTEMDRIVE"));
    // Case shouldn't matter on Windows
    subprocessBuilder.setEnv(ImmutableMap.of("SystemDrive", "X:"));
    process = subprocessBuilder.start();
    process.waitFor();
    assertThat(process.exitValue()).isEqualTo(0);

    byte[] buf = new byte[3];
    process.getInputStream().read(buf);
    assertThat(new String(buf, UTF_8).trim()).isEqualTo("X:");
  }

  @Test
  public void testStreamAvailable_zeroAfterClose() throws Exception {
    SubprocessBuilder subprocessBuilder = new SubprocessBuilder(WindowsSubprocessFactory.INSTANCE);
    subprocessBuilder.setWorkingDirectory(new File("."));
    subprocessBuilder.setArgv(ImmutableList.of(mockBinary, "-jar", mockSubprocess, "OHELLO"));
    process = subprocessBuilder.start();
    InputStream inputStream = process.getInputStream();
    // We don't know if the process has already written to the pipe
    assertThat(inputStream.available()).isAnyOf(0, 5);
    process.waitFor();
    // Windows allows streams to be read after the process has died.
    assertThat(inputStream.available()).isAnyOf(0, 5);
    inputStream.close();
    assertThat(assertThrows(IllegalStateException.class, inputStream::available))
        .hasMessageThat()
        .contains("Stream already closed");
  }

  /**
   * An argument and its command-line-escaped counterpart.
   *
   * <p>Such escaping ensures that Bazel correctly forwards arguments to subprocesses.
   */
  private static final class ArgPair {
    public final String original;
    public final String escaped;

    public ArgPair(String original, String escaped) {
      this.original = original;
      this.escaped = escaped;
    }
  };

  /** Asserts that a subprocess correctly receives command line arguments. */
  private void assertSubprocessReceivesArgsAsIntended(ArgPair... args) throws Exception {
    // Look up the path of the printarg.exe utility.
    String printArgExe =
        runfiles.rlocation(
            "io_bazel/src/test/java/com/google/devtools/build/lib/windows/printarg.exe");
    assertThat(printArgExe).isNotEmpty();

    for (ArgPair arg : args) {
      // Assert that the command-line encoding logic works as intended.
      assertThat(ShellUtils.windowsEscapeArg(arg.original)).isEqualTo(arg.escaped);

      // Create a separate subprocess just for this argument.
      SubprocessBuilder subprocessBuilder =
          new SubprocessBuilder(WindowsSubprocessFactory.INSTANCE);
      subprocessBuilder.setWorkingDirectory(new File("."));
      subprocessBuilder.setArgv(ImmutableList.of(printArgExe, arg.original));
      process = subprocessBuilder.start();
      process.waitFor();
      assertThat(process.exitValue()).isEqualTo(0);

      // The subprocess printed its argv[1] in parentheses, e.g. (foo).
      // Assert that it printed exactly the *original* argument in parentheses.
      byte[] buf = new byte[1000];
      process.getInputStream().read(buf);
      String actual = new String(buf, UTF_8).trim();
      assertThat(actual).isEqualTo("(" + arg.original + ")");
    }
  }

  @Test
  public void testSubprocessReceivesArgsAsIntended() throws Exception {
    assertSubprocessReceivesArgsAsIntended(
        new ArgPair("", "\"\""),
        new ArgPair(" ", "\" \""),
        new ArgPair("\"", "\"\\\"\""),
        new ArgPair("\"\\", "\"\\\"\\\\\""),
        new ArgPair("\\", "\\"),
        new ArgPair("\\\"", "\"\\\\\\\"\""),
        new ArgPair("with space", "\"with space\""),
        new ArgPair("with^caret", "with^caret"),
        new ArgPair("space ^caret", "\"space ^caret\""),
        new ArgPair("caret^ space", "\"caret^ space\""),
        new ArgPair("with\"quote", "\"with\\\"quote\""),
        new ArgPair("with\\backslash", "with\\backslash"),
        new ArgPair("one\\ backslash and \\space", "\"one\\ backslash and \\space\""),
        new ArgPair("two\\\\backslashes", "two\\\\backslashes"),
        new ArgPair("two\\\\ backslashes \\\\and space", "\"two\\\\ backslashes \\\\and space\""),
        new ArgPair("one\\\"x", "\"one\\\\\\\"x\""),
        new ArgPair("two\\\\\"x", "\"two\\\\\\\\\\\"x\""),
        new ArgPair("a \\ b", "\"a \\ b\""),
        new ArgPair("a \\\" b", "\"a \\\\\\\" b\""),
        new ArgPair("A", "A"),
        new ArgPair("\"a\"", "\"\\\"a\\\"\""),
        new ArgPair("B C", "\"B C\""),
        new ArgPair("\"b c\"", "\"\\\"b c\\\"\""),
        new ArgPair("D\"E", "\"D\\\"E\""),
        new ArgPair("\"d\"e\"", "\"\\\"d\\\"e\\\"\""),
        new ArgPair("C:\\F G", "\"C:\\F G\""),
        new ArgPair("\"C:\\f g\"", "\"\\\"C:\\f g\\\"\""),
        new ArgPair("C:\\H\"I", "\"C:\\H\\\"I\""),
        new ArgPair("\"C:\\h\"i\"", "\"\\\"C:\\h\\\"i\\\"\""),
        new ArgPair("C:\\J\\\"K", "\"C:\\J\\\\\\\"K\""),
        new ArgPair("\"C:\\j\\\"k\"", "\"\\\"C:\\j\\\\\\\"k\\\"\""),
        new ArgPair("C:\\L M ", "\"C:\\L M \""),
        new ArgPair("\"C:\\l m \"", "\"\\\"C:\\l m \\\"\""),
        new ArgPair("C:\\N O\\", "\"C:\\N O\\\\\""),
        new ArgPair("\"C:\\n o\\\"", "\"\\\"C:\\n o\\\\\\\"\""),
        new ArgPair("C:\\P Q\\ ", "\"C:\\P Q\\ \""),
        new ArgPair("\"C:\\p q\\ \"", "\"\\\"C:\\p q\\ \\\"\""),
        new ArgPair("C:\\R\\S\\", "C:\\R\\S\\"),
        new ArgPair("C:\\R x\\S\\", "\"C:\\R x\\S\\\\\""),
        new ArgPair("\"C:\\r\\s\\\"", "\"\\\"C:\\r\\s\\\\\\\"\""),
        new ArgPair("\"C:\\r x\\s\\\"", "\"\\\"C:\\r x\\s\\\\\\\"\""),
        new ArgPair("C:\\T U\\W\\", "\"C:\\T U\\W\\\\\""),
        new ArgPair("\"C:\\t u\\w\\\"", "\"\\\"C:\\t u\\w\\\\\\\"\""));
  }
}


text:
package com.google.clearsilver.jsilver.template;

import com.google.clearsilver.jsilver.autoescape.EscapeMode;
import com.google.clearsilver.jsilver.data.Data;
import com.google.clearsilver.jsilver.resourceloader.ResourceLoader;

import java.io.IOException;

/**
 * Represents a template that can be rendered with data.
 */
public interface Template {

  /**
   * Render the template.
   *
   * @param data Data to merge with template.
   * @param out Target to write to.
   * @param resourceLoader ResourceLoader to use instead of the default template one when loading
   *        files.
   */
  void render(Data data, Appendable out, ResourceLoader resourceLoader) throws IOException;

  /**
   * Render the template with a custom RenderingContext.
   *
   * @param context RenderingContext to use during rendering.
   */
  void render(RenderingContext context) throws IOException;

  /**
   * Create a new RenderingContext.
   *
   * @param data Data to merge with template.
   * @param out Target to write to.
   * @param resourceLoader ResourceLoader to load files.
   */
  RenderingContext createRenderingContext(Data data, Appendable out, ResourceLoader resourceLoader);

  /**
   * Name of template (e.g. mytemplate.cs).
   */
  String getTemplateName();

  /**
   * Name to use when displaying error or log messages. May return the same value as
   * #getTemplateName, or may contain more information.
   */
  String getDisplayName();

  /**
   * Return the EscapeMode in which this template was generated.
   *
   * @return EscapeMode
   */
  EscapeMode getEscapeMode();
}


text:
package com.mercadopago.android.px.internal.features.express.slider;

import android.view.View;
import java.util.List;

public abstract class HubableAdapter<T, V extends View> extends ViewAdapter<T, V> {
    /* default */ HubableAdapter(final V view) {
        super(view);
    }

    public abstract T getNewModels(HubAdapter.Model model);
}


text:
package info.evshiron.ingressdeserializer;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class IngressDeserializer {

    // Reference to https://gist.github.com/sugyan/8396d473ffae62b664e2, thanks to @sugyan.

    public static String Deserializer(File file) {

        try {

            int count = 0;

            ArrayList<String[]> elements = new ArrayList<String[]>();

            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);

            while (ois.available() == 0) {

                Object obj = ois.readObject();
                Class cls = obj.getClass();
                String[] arr = new String[Array.getLength(obj)];

                for (int i = 0; i < Array.getLength(obj); i++) {

                    if (cls.getComponentType() == float.class) {
                        arr[i] = String.valueOf(Array.getFloat(obj, i));
                    }
                    else if (cls.getComponentType() == short.class) {
                        arr[i] = String.valueOf(Array.getShort(obj, i));
                    }
                    else {

                        System.out.println(cls.getTypeName());

                    }

                }

                // 最初の頂点座標は (vx, vy, vz, tx, ty) になっているようなので分離。
                if (count == 0) {

                    ArrayList<String> vertices = new ArrayList<String>();
                    ArrayList<String> texcoords = new ArrayList<String>();

                    for (int i = 0; i < arr.length; i++) {
                        if (i % 5 < 3) {
                            vertices.add(arr[i]);
                        } else {
                            texcoords.add(arr[i]);
                        }
                    }

                    elements.add(vertices.toArray(new String[0]));
                    elements.add(texcoords.toArray(new String[0]));

                } else {

                    elements.add(arr);

                }

                count++;

            }

            fis.close();
            ois.close();

            if(elements.get(0).length / 3 != elements.get(1).length / 2) {

                System.out.println(file.getName());
                System.out.println("VERTICES_TEXCOORDS_NOT_MATCH");
                return null;

            }

            StringBuilder sb = new StringBuilder();

            sb.append(String.format("# (x, y, z) count: %d.\n", elements.get(0).length / 3));

            for(int i = 0; i < elements.get(0).length; i += 3) {

                sb.append(String.format("v %f %f %f\n", Double.parseDouble(elements.get(0)[i+0]), Double.parseDouble(elements.get(0)[i+1]), Double.parseDouble(elements.get(0)[i+2])));

            }

            sb.append(String.format("# (u, v) count: %d.\n", elements.get(1).length / 2));

            for(int i = 0; i < elements.get(1).length; i += 2) {

                sb.append(String.format("vt %f %f\n", Double.parseDouble(elements.get(1)[i+0]), Double.parseDouble(elements.get(1)[i+1])));

            }

            sb.append(String.format("# Faces count: %d.\n", elements.get(2).length / 3));

            for(int i = 0; i < elements.get(2).length; i += 3) {

                int v1 = Integer.parseInt(elements.get(2)[i+0]) + 1;
                int v2 = Integer.parseInt(elements.get(2)[i+1]) + 1;
                int v3 = Integer.parseInt(elements.get(2)[i+2]) + 1;

                sb.append(String.format("f %d/%d %d/%d %d/%d\n", v1, v1, v2, v2, v3, v3));

            }

            return sb.toString();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;

    }

    public static void main(String[] args) {

            try {

                for(String path : args) {

                    File file = new File(path);

                    if(!file.exists() || !file.getName().endsWith(".obj")) continue;

                    String wavefront = Deserializer(file);

                    if(wavefront == null) continue;

                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(String.format("out_%s", file.getName())), "utf-8"));

                    writer.write(wavefront);
                    writer.flush();
                    writer.close();

                }

            } catch (IOException e) {
                e.printStackTrace();
            }

    }

}


text:
package org.mamute.interceptors;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Set;

import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.apache.log4j.Logger;
import org.mamute.providers.GlobalInterceptor;

import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.interceptor.FlashInterceptor;
import br.com.caelum.vraptor.interceptor.Interceptor;

@Intercepts(before=GlobalInterceptor.class, after=FlashInterceptor.class)
public class InternalErrorInterceptor implements Interceptor {

	@Inject private Result result;
	private static Logger log = Logger.getLogger(InternalErrorInterceptor.class);

	@Override
	public boolean accepts(ControllerMethod arg0) {
		return true;
	}

	@Override
	public void intercept(InterceptorStack stack, ControllerMethod method,
			Object instance) throws InterceptionException {
		try {
			stack.next(method, instance);
		}catch (Exception e) {

			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);

			Throwable cause = e.getCause();
			if(cause != null){
				if (cause instanceof ConstraintViolationException) {
					Set<ConstraintViolation<?>> constraintViolations = ((ConstraintViolationException) cause).getConstraintViolations();
					pw.printf("\nConstraint Violations: \n");
					for (ConstraintViolation<?> constraintViolation : constraintViolations) {
						pw.printf("\t" +constraintViolation.getConstraintDescriptor().getAnnotation()+"\n");
					}
					pw.printf("\n");
					log.error(sw.toString());
				}
				cause.printStackTrace(pw);
			}else{
				e.printStackTrace(pw);
			}

			pw.close();
			result.include("stacktrace", sw.toString());
			throw e;
		}
	}
}


text:
from django.core.exceptions import ValidationError

__author__ = 'palmer'
from django import forms
from standards_review.models import Standard, Adduct, FragmentationSpectrum, Molecule, MoleculeTag, LcInfo, MsInfo
import logging
from django.utils.safestring import mark_safe

def mzml_filename_validator(fn):
    if len(fn.name) < 6 or fn.name[-5:].lower() != ".mzml":
        raise ValidationError("Not a valid mzML file name: " + fn.name)

class MoleculeForm(forms.ModelForm):
    class Meta:
        model = Molecule
        fields = ('name', 'sum_formula', 'lipidmaps_id', 'pubchem_id', 'cas_id', 'chebi_id', 'hmdb_id', 'inchi_code',
                  'solubility', 'tags', 'natural_product')
        widgets = {'tags': forms.widgets.CheckboxSelectMultiple}

class StandardForm(forms.ModelForm):
    class Meta:
        model = Standard
        fields = ('inventory_id','molecule','vendor','vendor_cat','lot_num' ,'location' ,'purchase_date')
        help_texts = {
            'molecule': mark_safe('Molecule not in the list?  add it ')
            }

class StandardAddForm(forms.ModelForm):
    class Meta:
        model = Standard
        fields = ('molecule', 'vendor', 'vendor_cat', 'lot_num', 'location', 'purchase_date')
        help_texts = {'molecule': mark_safe('Molecule not in the list?  add it ')}

class AdductForm(forms.ModelForm):
    class Meta:
        model = Adduct
        fields = ('nM', 'delta_formula', 'charge')

class StandardBatchForm(forms.Form):
    tab_delimited_file = forms.FileField()

class UploadFileForm(forms.Form):
    mzml_file = forms.FileField(validators=[mzml_filename_validator])
    adducts = forms.ModelMultipleChoiceField(queryset=Adduct.objects.all())
    standards = forms.ModelMultipleChoiceField(queryset=Standard.objects.all().order_by('inventory_id'))
    mass_accuracy_ppm = forms.FloatField(min_value=0.000001, label="MS1 mass accuracy (ppm)")
    quad_window_mz = forms.FloatField(min_value=0.000001, label='Precursor Window (m/z)')
    lc_info = forms.CharField(label="LC Info")
    ms_info = forms.CharField(label="MS Info")
    instrument_info = forms.CharField(label="Instrument Info (obsolete)")
    ionization_method = forms.CharField(label="Ionization Method")
    ion_analyzer = forms.CharField(label="Ion Analyzer")

class FragSpecReview(forms.Form):
    def __init__(self,*args,**kwargs):
        fragSpecId = kwargs.pop('extra')
        self.user = kwargs.pop('user', None)

        super(FragSpecReview, self).__init__(*args, **kwargs)
        for i in fragSpecId:

            logging.debug(i)
            fs = FragmentationSpectrum.objects.get(pk=i)
            logging.debug(fs.dataset)
            initial = 2
            if fs.reviewed:
                if fs.standard:
                    initial = 1
                else:
                    initial = 0
            self.fields['yesno_%s' % i] = forms.ChoiceField(choices=((1, 'Accept'), (0, 'Reject'), (2, 'Unrated')), widget=forms.RadioSelect, label=i, initial=initial)

    def get_response(self):
        for name, value in self.cleaned_data.items():
            if name.startswith('yesno_'):
                yield (self.fields[name].label, value)

class ExportLibrary(forms.Form):
    data_format = forms.ChoiceField(choices=((0, 'mgf'), (1, 'msp'), (2, 'csv'), (3,'EBI json'), (4,'MassBank')),widget=forms.Select)
    class_to_export = forms.ChoiceField(choices=((0, 'all'), (1, 'positive mode'), (2, 'negative mode')), widget=forms.Select)
    #spectra_to_export = forms.ChoiceField(choices=((0, 'Rated Correct'), (1, 'All Rated'), (2, 'ALL MSMS'),), widget=forms.Select)

class MoleculeTagForm(forms.Form):
    class Meta:
        model = MoleculeTag
        fields = ('name',)