text:
@interface AboutViewController ()
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation AboutViewController
@synthesize versionLabel;
@synthesize titleLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    versionLabel.text = [@"Build " stringByAppendingString:[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey]];
#if vPRO
    self.titleLabel.text = @"Quick Discount Pro";
#endif
}

- (void)viewDidUnload
{
    [self setVersionLabel:nil];
    [self setTitleLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;//(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)byteappsWebsite:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.byteapps.com"]];
}
- (IBAction)backAction:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

@end


text:
\h1{ Extended Euclidean algorithm }

While \algohref=euclid_algorithm{"normal" Euclidean algorithm} just finds the greatest common divisor of two numbers $a$ and $b$, the extended Euclidean algorithm finds GCD in addition to the coefficients of $x$ and $y$ such that:

$$a \cdot x + b \cdot y = {\rm gcd} (a, b).$$

Ie, it finds the factors by which the GCD of two numbers is expressed using these numbers.

\h2{Algorithm}

To make the calculation of these coecients in the Euclidian algorithm is easy enough to deduce the formula, which they change during the transition from a pair $(a,b)$ to the pair $(b\%a,a)$ (a percent sign denotes taking the remainder of division).

So, suppose we have found a solution $(x_1,y_1)$ of the problem for a new pair $(b\%a,a)$:

$$ (b \% a) \cdot x_1 + a \cdot y_1 = g, $$

and I want to get the solution $(x,y)$ for our pair $(a,b)$:

$$ a \cdot x + b \cdot y = g. $$

To do this, we transform the magnitude of $b \% a$:

$$ b \% a = b - \left\lfloor \frac{b}{a} \right\rfloor \cdot a. $$

Substitute this in the above expression with $x_1$ and $y_1$ and get:

$$ g = (b \% a) \cdot x_1 + a \cdot y_1 = \left( b - \left\lfloor \frac{b}{a} \right\rfloor \cdot a \right) \cdot x_1 + a \cdot y_1, $$

and, carrying out a regrouping of summands, we get:

$$ g = b \cdot x_1 + a \cdot \left( y_1 - \left\lfloor \frac{b}{a} \right\rfloor \cdot x_1 \right). $$

Comparing this with the original expression over the unknown $x$ and $y$, we get the required expression:

$$ \cases{
x = y_1 - \left\lfloor \frac{b}{a} \right\rfloor \cdot x_1, \cr
y = x_1.
} $$

\h2{the Implementation}

\code

int gcd (int a, int b, int & x, int & y) {
if (a == 0) {
x = 0; y = 1;
return b;
}
int x1, y1;
int d = gcd (b%a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
\endcode

This is a recursive function which still returns the GCD of the numbers $a$ and $b$, but beyond that --- also looking for coefficients $x$ and $y$ as parameters to functions, transmitted through the links.

The base of the recursion --- case $a = 0$. Then the GCD is $b$, and, obviously, the required coefficients of $x$ and $y$ equal to $0$ and $1$ respectively. In other cases, the normal solution, and the coefficients are recalculated according to the above formulas.

Extended Euclidean algorithm in such an implementation works correctly even for negative numbers.

\h2{ Literature }

\ul{
\li \book{Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein}{Algorithms: Construction and analysis}{2005}{cormen.djvu}
}

redpajama_set_name: RedPajamaGithub


text:
void random_op ();
void *monitor_thread(void *arg);


text:
export type TODO = any; // eslint-disable-line @typescript-eslint/no-explicit-any

export type TODOObject = {[key: string]: TODO};


text:
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';

import { News } from '../pages/News/news';
import { Fixtures } from '../pages/Fixtures/fixtures';
import { Rugby101 } from '../pages/Rugby101/rugby101';
import { RugbyClubs } from '../pages/RugbyClubs/rugbyclubs';
import { TeamPhotos } from '../pages/TeammatePhotos/teamphotos';
import { FrameApplier } from '../pages/FrameApplier/frameapplier';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage: any = News;

  pages: Array<{title: string, component: any}>;

  constructor(public platform: Platform) {
    this.initializeApp();

    // used for an example of ngFor and navigation
    this.pages = [

      { title: 'News', component: News },
      { title: 'Fixtures and Results', component: Fixtures },
      { title: 'Rugby 101', component: Rugby101 },
      { title: 'Rugby Clubs', component: RugbyClubs },
      { title: 'Teammate Photos', component: TeamPhotos }

    ];

  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
      Splashscreen.hide();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }
}


text:
package com.plagchain.rest;

import com.plagchain.GenericResponse;
import com.plagchain.domain.ResponseItem;
import com.plagchain.service.PlagService;
import com.plagchain.service.UtilService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by Jagrut on 27-04-2017.
 *
 * REST controller to handle requests related to submitting a document to check for plagiarism
 */

@RestController
public class PlagCheckREST {

    private final Logger log = LoggerFactory.getLogger(PlagCheckREST.class);
    private PlagService plagService;

    public PlagCheckREST(PlagService plagService) {
        this.plagService = plagService;
    }

    @PostMapping("/submitDoc")
    @ResponseBody
    public GenericResponse putDocFeaturesInBlockchain(@RequestBody @RequestParam("file")MultipartFile file,
                                                      @RequestParam(required = false, value = "contactInfo")String contactInfo,
                                                      @RequestParam("publisherWalletAddress")String publisherWalletAddress) {
        log.info("REST request to extract document features and store them");
        return plagService.extractDocFeaturesAndStore(file, contactInfo, publisherWalletAddress);
    }

    @PostMapping("/checkSim")
    @ResponseBody
    public ResponseItem checkDocSim(@RequestBody @RequestParam("file")MultipartFile file) {
        log.info("REST request to find similar documents");
        ResponseItem responseItem = new ResponseItem();
        responseItem = plagService.extractDocFeaturesAndCheckSim(file, responseItem);
        return  responseItem;
    }
}


text:
package androidx.media3.extractor.mkv;

import static androidx.media3.common.C.RESULT_END_OF_INPUT;
import static androidx.media3.common.C.RESULT_MAX_LENGTH_EXCEEDED;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import androidx.media3.common.C;
import androidx.media3.extractor.ExtractorInput;
import androidx.media3.test.utils.FakeExtractorInput;
import androidx.media3.test.utils.FakeExtractorInput.SimulatedIOException;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.EOFException;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Tests for {@link VarintReader}. */
@RunWith(AndroidJUnit4.class)
public final class VarintReaderTest {

  private static final byte MAX_BYTE = (byte) 0xFF;

  private static final byte[] DATA_1_BYTE_0 = new byte[] {(byte) 0x80};
  private static final byte[] DATA_2_BYTE_0 = new byte[] {0x40, 0};
  private static final byte[] DATA_3_BYTE_0 = new byte[] {0x20, 0, 0};
  private static final byte[] DATA_4_BYTE_0 = new byte[] {0x10, 0, 0, 0};
  private static final byte[] DATA_5_BYTE_0 = new byte[] {0x08, 0, 0, 0, 0};
  private static final byte[] DATA_6_BYTE_0 = new byte[] {0x04, 0, 0, 0, 0, 0};
  private static final byte[] DATA_7_BYTE_0 = new byte[] {0x02, 0, 0, 0, 0, 0, 0};
  private static final byte[] DATA_8_BYTE_0 = new byte[] {0x01, 0, 0, 0, 0, 0, 0, 0};

  private static final byte[] DATA_1_BYTE_64 = new byte[] {(byte) 0xC0};
  private static final byte[] DATA_2_BYTE_64 = new byte[] {0x40, 0x40};
  private static final byte[] DATA_3_BYTE_64 = new byte[] {0x20, 0, 0x40};
  private static final byte[] DATA_4_BYTE_64 = new byte[] {0x10, 0, 0, 0x40};
  private static final byte[] DATA_5_BYTE_64 = new byte[] {0x08, 0, 0, 0, 0x40};
  private static final byte[] DATA_6_BYTE_64 = new byte[] {0x04, 0, 0, 0, 0, 0x40};
  private static final byte[] DATA_7_BYTE_64 = new byte[] {0x02, 0, 0, 0, 0, 0, 0x40};
  private static final byte[] DATA_8_BYTE_64 = new byte[] {0x01, 0, 0, 0, 0, 0, 0, 0x40};

  private static final byte[] DATA_1_BYTE_MAX = new byte[] {MAX_BYTE};
  private static final byte[] DATA_2_BYTE_MAX = new byte[] {0x7F, MAX_BYTE};
  private static final byte[] DATA_3_BYTE_MAX = new byte[] {0x3F, MAX_BYTE, MAX_BYTE};
  private static final byte[] DATA_4_BYTE_MAX = new byte[] {0x1F, MAX_BYTE, MAX_BYTE, MAX_BYTE};
  private static final byte[] DATA_5_BYTE_MAX =
      new byte[] {0x0F, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE};
  private static final byte[] DATA_6_BYTE_MAX =
      new byte[] {0x07, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE};
  private static final byte[] DATA_7_BYTE_MAX =
      new byte[] {0x03, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE};
  private static final byte[] DATA_8_BYTE_MAX =
      new byte[] {0x01, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE, MAX_BYTE};

  private static final long VALUE_1_BYTE_MAX = 0x7F;
  private static final long VALUE_1_BYTE_MAX_WITH_MASK = 0xFF;
  private static final long VALUE_2_BYTE_MAX = 0x3FFF;
  private static final long VALUE_2_BYTE_MAX_WITH_MASK = 0x7FFF;
  private static final long VALUE_3_BYTE_MAX = 0x1FFFFF;
  private static final long VALUE_3_BYTE_MAX_WITH_MASK = 0x3FFFFF;
  private static final long VALUE_4_BYTE_MAX = 0xFFFFFFF;
  private static final long VALUE_4_BYTE_MAX_WITH_MASK = 0x1FFFFFFF;
  private static final long VALUE_5_BYTE_MAX = 0x7FFFFFFFFL;
  private static final long VALUE_5_BYTE_MAX_WITH_MASK = 0xFFFFFFFFFL;
  private static final long VALUE_6_BYTE_MAX = 0x3FFFFFFFFFFL;
  private static final long VALUE_6_BYTE_MAX_WITH_MASK = 0x7FFFFFFFFFFL;
  private static final long VALUE_7_BYTE_MAX = 0x1FFFFFFFFFFFFL;
  private static final long VALUE_7_BYTE_MAX_WITH_MASK = 0x3FFFFFFFFFFFFL;
  private static final long VALUE_8_BYTE_MAX = 0xFFFFFFFFFFFFFFL;
  private static final long VALUE_8_BYTE_MAX_WITH_MASK = 0x1FFFFFFFFFFFFFFL;

  @Test
  public void readVarintEndOfInputAtStart() throws IOException {
    VarintReader reader = new VarintReader();
    // Build an input with no data.
    ExtractorInput input = new FakeExtractorInput.Builder().setSimulateUnknownLength(true).build();
    // End of input allowed.
    long result = reader.readUnsignedVarint(input, true, false, 8);
    assertThat(result).isEqualTo(RESULT_END_OF_INPUT);
    // End of input not allowed.
    try {
      reader.readUnsignedVarint(input, false, false, 8);
      fail();
    } catch (EOFException e) {
      // Expected.
    }
  }

  @Test
  public void readVarintExceedsMaximumAllowedLength() throws IOException {
    VarintReader reader = new VarintReader();
    ExtractorInput input =
        new FakeExtractorInput.Builder()
            .setData(DATA_8_BYTE_0)
            .setSimulateUnknownLength(true)
            .build();
    long result = reader.readUnsignedVarint(input, false, true, 4);
    assertThat(result).isEqualTo(RESULT_MAX_LENGTH_EXCEEDED);
  }

  @Test
  public void readVarint() throws IOException {
    VarintReader reader = new VarintReader();
    testReadVarint(reader, true, DATA_1_BYTE_0, 1, 0);
    testReadVarint(reader, true, DATA_2_BYTE_0, 2, 0);
    testReadVarint(reader, true, DATA_3_BYTE_0, 3, 0);
    testReadVarint(reader, true, DATA_4_BYTE_0, 4, 0);
    testReadVarint(reader, true, DATA_5_BYTE_0, 5, 0);
    testReadVarint(reader, true, DATA_6_BYTE_0, 6, 0);
    testReadVarint(reader, true, DATA_7_BYTE_0, 7, 0);
    testReadVarint(reader, true, DATA_8_BYTE_0, 8, 0);
    testReadVarint(reader, true, DATA_1_BYTE_64, 1, 64);
    testReadVarint(reader, true, DATA_2_BYTE_64, 2, 64);
    testReadVarint(reader, true, DATA_3_BYTE_64, 3, 64);
    testReadVarint(reader, true, DATA_4_BYTE_64, 4, 64);
    testReadVarint(reader, true, DATA_5_BYTE_64, 5, 64);
    testReadVarint(reader, true, DATA_6_BYTE_64, 6, 64);
    testReadVarint(reader, true, DATA_7_BYTE_64, 7, 64);
    testReadVarint(reader, true, DATA_8_BYTE_64, 8, 64);
    testReadVarint(reader, true, DATA_1_BYTE_MAX, 1, VALUE_1_BYTE_MAX);
    testReadVarint(reader, true, DATA_2_BYTE_MAX, 2, VALUE_2_BYTE_MAX);
    testReadVarint(reader, true, DATA_3_BYTE_MAX, 3, VALUE_3_BYTE_MAX);
    testReadVarint(reader, true, DATA_4_BYTE_MAX, 4, VALUE_4_BYTE_MAX);
    testReadVarint(reader, true, DATA_5_BYTE_MAX, 5, VALUE_5_BYTE_MAX);
    testReadVarint(reader, true, DATA_6_BYTE_MAX, 6, VALUE_6_BYTE_MAX);
    testReadVarint(reader, true, DATA_7_BYTE_MAX, 7, VALUE_7_BYTE_MAX);
    testReadVarint(reader, true, DATA_8_BYTE_MAX, 8, VALUE_8_BYTE_MAX);
    testReadVarint(reader, false, DATA_1_BYTE_MAX, 1, VALUE_1_BYTE_MAX_WITH_MASK);
    testReadVarint(reader, false, DATA_2_BYTE_MAX, 2, VALUE_2_BYTE_MAX_WITH_MASK);
    testReadVarint(reader, false, DATA_3_BYTE_MAX, 3, VALUE_3_BYTE_MAX_WITH_MASK);
    testReadVarint(reader, false, DATA_4_BYTE_MAX, 4, VALUE_4_BYTE_MAX_WITH_MASK);
    testReadVarint(reader, false, DATA_5_BYTE_MAX, 5, VALUE_5_BYTE_MAX_WITH_MASK);
    testReadVarint(reader, false, DATA_6_BYTE_MAX, 6, VALUE_6_BYTE_MAX_WITH_MASK);
    testReadVarint(reader, false, DATA_7_BYTE_MAX, 7, VALUE_7_BYTE_MAX_WITH_MASK);
    testReadVarint(reader, false, DATA_8_BYTE_MAX, 8, VALUE_8_BYTE_MAX_WITH_MASK);
  }

  @Test
  public void readVarintFlaky() throws IOException {
    VarintReader reader = new VarintReader();
    testReadVarintFlaky(reader, true, DATA_1_BYTE_0, 1, 0);
    testReadVarintFlaky(reader, true, DATA_2_BYTE_0, 2, 0);
    testReadVarintFlaky(reader, true, DATA_3_BYTE_0, 3, 0);
    testReadVarintFlaky(reader, true, DATA_4_BYTE_0, 4, 0);
    testReadVarintFlaky(reader, true, DATA_5_BYTE_0, 5, 0);
    testReadVarintFlaky(reader, true, DATA_6_BYTE_0, 6, 0);
    testReadVarintFlaky(reader, true, DATA_7_BYTE_0, 7, 0);
    testReadVarintFlaky(reader, true, DATA_8_BYTE_0, 8, 0);
    testReadVarintFlaky(reader, true, DATA_1_BYTE_64, 1, 64);
    testReadVarintFlaky(reader, true, DATA_2_BYTE_64, 2, 64);
    testReadVarintFlaky(reader, true, DATA_3_BYTE_64, 3, 64);
    testReadVarintFlaky(reader, true, DATA_4_BYTE_64, 4, 64);
    testReadVarintFlaky(reader, true, DATA_5_BYTE_64, 5, 64);
    testReadVarintFlaky(reader, true, DATA_6_BYTE_64, 6, 64);
    testReadVarintFlaky(reader, true, DATA_7_BYTE_64, 7, 64);
    testReadVarintFlaky(reader, true, DATA_8_BYTE_64, 8, 64);
    testReadVarintFlaky(reader, true, DATA_1_BYTE_MAX, 1, VALUE_1_BYTE_MAX);
    testReadVarintFlaky(reader, true, DATA_2_BYTE_MAX, 2, VALUE_2_BYTE_MAX);
    testReadVarintFlaky(reader, true, DATA_3_BYTE_MAX, 3, VALUE_3_BYTE_MAX);
    testReadVarintFlaky(reader, true, DATA_4_BYTE_MAX, 4, VALUE_4_BYTE_MAX);
    testReadVarintFlaky(reader, true, DATA_5_BYTE_MAX, 5, VALUE_5_BYTE_MAX);
    testReadVarintFlaky(reader, true, DATA_6_BYTE_MAX, 6, VALUE_6_BYTE_MAX);
    testReadVarintFlaky(reader, true, DATA_7_BYTE_MAX, 7, VALUE_7_BYTE_MAX);
    testReadVarintFlaky(reader, true, DATA_8_BYTE_MAX, 8, VALUE_8_BYTE_MAX);
    testReadVarintFlaky(reader, false, DATA_1_BYTE_MAX, 1, VALUE_1_BYTE_MAX_WITH_MASK);
    testReadVarintFlaky(reader, false, DATA_2_BYTE_MAX, 2, VALUE_2_BYTE_MAX_WITH_MASK);
    testReadVarintFlaky(reader, false, DATA_3_BYTE_MAX, 3, VALUE_3_BYTE_MAX_WITH_MASK);
    testReadVarintFlaky(reader, false, DATA_4_BYTE_MAX, 4, VALUE_4_BYTE_MAX_WITH_MASK);
    testReadVarintFlaky(reader, false, DATA_5_BYTE_MAX, 5, VALUE_5_BYTE_MAX_WITH_MASK);
    testReadVarintFlaky(reader, false, DATA_6_BYTE_MAX, 6, VALUE_6_BYTE_MAX_WITH_MASK);
    testReadVarintFlaky(reader, false, DATA_7_BYTE_MAX, 7, VALUE_7_BYTE_MAX_WITH_MASK);
    testReadVarintFlaky(reader, false, DATA_8_BYTE_MAX, 8, VALUE_8_BYTE_MAX_WITH_MASK);
  }

  private static void testReadVarint(
      VarintReader reader, boolean removeMask, byte[] data, int expectedLength, long expectedValue)
      throws IOException {
    ExtractorInput input =
        new FakeExtractorInput.Builder().setData(data).setSimulateUnknownLength(true).build();
    long result = reader.readUnsignedVarint(input, false, removeMask, 8);
    assertThat(input.getPosition()).isEqualTo(expectedLength);
    assertThat(result).isEqualTo(expectedValue);
  }

  private static void testReadVarintFlaky(
      VarintReader reader, boolean removeMask, byte[] data, int expectedLength, long expectedValue)
      throws IOException {
    ExtractorInput input =
        new FakeExtractorInput.Builder()
            .setData(data)
            .setSimulateUnknownLength(true)
            .setSimulateIOErrors(true)
            .setSimulatePartialReads(true)
            .build();
    long result = -1;
    while (result == -1) {
      try {
        result = reader.readUnsignedVarint(input, false, removeMask, 8);
        if (result == C.RESULT_END_OF_INPUT || result == C.RESULT_MAX_LENGTH_EXCEEDED) {
          // Unexpected.
          fail();
        }
      } catch (SimulatedIOException e) {
        // Expected.
      }
    }
    assertThat(input.getPosition()).isEqualTo(expectedLength);
    assertThat(result).isEqualTo(expectedValue);
  }
}


text:
<?php

namespace Illuminate\Cache\Console;

use Illuminate\Console\Command;
use Illuminate\Cache\CacheManager;
use Symfony\Component\Console\Input\InputArgument;

class ClearCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'cache:clear';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Flush the application cache';

    /**
     * The cache manager instance.
     *
     * @var \Illuminate\Cache\CacheManager
     */
    protected $cache;

    /**
     * Create a new cache clear command instance.
     *
     * @param  \Illuminate\Cache\CacheManager  $cache
     * @return void
     */
    public function __construct(CacheManager $cache)
    {
        parent::__construct();

        $this->cache = $cache;
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $storeName = $this->argument('store');

        $this->laravel['events']->fire('cache:clearing', [$storeName]);

        $this->cache->store($storeName)->flush();

        $this->laravel['events']->fire('cache:cleared', [$storeName]);

        $this->info('Application cache cleared!');
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['store', InputArgument::OPTIONAL, 'The name of the store you would like to clear.'],
        ];
    }
}


text:
import collections
class Solution:
    def customSortString(self, S: str, T: str) -> str:
        table = collections.Counter(T)
        result = []
        for c in S:
            if c in table:
                result.append(c*table[c])
                table[c] = 0
        for c, v in table.items():
            if v > 0:
                result.append(c*v)
        return ''.join(result)


text:
<component name="libraryTable">
  <library name="android-support-v4">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/HoloGraphLibrarySample/libs/android-support-v4.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
</component>


text:
package com.jlabar.runstats.domain;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Looper;

public class ProgressDialogWrapper implements IProgressDialog {
	private final ProgressDialog _progressDialog;
	private final Activity _activity;

	public ProgressDialogWrapper(ProgressDialog _progressDialog, Activity activity) {
		super();
		this._progressDialog = _progressDialog;
		this._activity = activity;
	}

	private boolean invokeNeeded()
	{
		return Looper.getMainLooper().getThread() != Thread.currentThread();
	}

	private void invoke(Runnable action)
	{
		if (invokeNeeded())
		{
			_activity.runOnUiThread(action);
		}
		else
		{
			action.run();
		}
	}

	public void setMax(final int maxValue)
	{
		invoke(new Runnable()
		{
		    public void run()
		    {
		    	_progressDialog.setIndeterminate(false);
				_progressDialog.setMax(maxValue);
				_progressDialog.setProgress(0);
		    }
		});
	}

	public void tick()
	{
		invoke(new Runnable()
		{
		    public void run()
		    {
				_progressDialog.incrementProgressBy(1);
		    }
		});
	}

	public void setMessage(final String message)
	{
		invoke(new Runnable()
		{
		    public void run()
		    {
				_progressDialog.setMessage(message);
		    }
		});
	}
}


text:
// -----------------------------------------------------------------------------------
// Copyright (c) Microsoft Open Technologies (Shanghai) Co. Ltd.  All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// -----------------------------------------------------------------------------------
/*
          // if (data.status == -1) {
              //   $state.go('create', {
              //     name: toParams.name
              //   });
              // } else {
              $state.go(toState.name, {
                name: toParams.name
              });
              //}*/
angular.module('oh.controllers', [])
  .controller('MainController', MainController = function($scope, $rootScope, $state, $q, api, activityService) {
    $scope.isloaded = true;

    $scope.loading = false;

    function asyncActivity(toParams) {
      var deferred = $q.defer();
      deferred.notify('About to greet ' + name + '.');
      var activity = activityService.getCurrentActivity();
      $scope.currentActivity = {
        name: toParams.name
      };
      if (toParams.name === undefined || activity.name == toParams.name) {
        deferred.resolve(activity)
      } else {
        activity = activityService.getByName(toParams.name)
        if (activity.name != undefined) {
          activity.name = toParams.name;
          activityService.setCurrentActivity(activity);
          deferred.resolve(activity)
        } else {
          api.admin.hackathon.get({
            header: {
              hackathon_name: toParams.name
            }
          }).then(function(data) {
            if (data.error) {
              deferred.reject(data.error)
            } else {
              activityService.add(data);
              activityService.setCurrentActivity(data);
              deferred.resolve(data)
            }
          });
        }
      }

      return deferred.promise;
    }

    $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
      $scope.loading = true;
      if (toState.name == 'create' || toState.name == 'main') {
        if (!toParams.name) {
          activityService.setCurrentActivity({});
        }
        return;
      } else {
        asyncActivity(toParams).then(function(activity) {
            // console.log(activity)
            if (activity.status == -1) {
              activityService.setCurrentActivity(activity);
              $state.go('create', {
                name: toParams.name
              });
            } else if (activity.config.cloud_provide == 0) {
              switch (toState.name) {
                case 'manage.ve':
                case 'manage.monitor':
                case 'manage.cloud':
                case 'manage.servers':
                  $state.go('404');
                  return
              }
            } else {
              $rootScope.$broadcast('changeActivity')
            }
          },
          function(error) {
            $state.go('404');
            event.preventDefault();
          }
        )
      }
    });

    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
      $scope.loading = false;
    });

  })
  .controller('manageController', function($scope, $state, activityService, session, NAV) {
    var activity = $scope.activity = activityService.getCurrentActivity();
    $scope.$on('changeActivity', function(event) {
      activity = $scope.activity = activityService.getCurrentActivity();
      event.preventDefault();
    })
    $scope.page = {
      name: ''
    };

    $scope.currentUser = session.user;

    $scope.$on('pageName', function(event, pageName) {
      $scope.page.name = pageName;
      event.preventDefault();
      event.stopPropagation();
    });

    $scope.currentArea = NAV.manage;
    $scope.$emit('pageName', '');

    $scope.isActive = function(item) {
      return {
        active: $state.includes(item.state)
      }
    }

    $scope.isShowNav = function(type) {
      // type 1: SETTINGS. type 2: ADVANCED_SETTINGS
      if (type == 1 || type == 2) {
        return true;
      }
      if (activity.config) {
        return activity.config.cloud_provider != 0
      } else {
        return false;
      }
    }

    $scope.isShowNavItem = function(type, item) {
      // if cloud_provider is "Azure", show all.
      // if cloud_provider is "Alauda", show "Cloud", "VirtualEnvirontment" and "Monitor".
      // "Cloud" would be shown anyway.
      if (type == 2) {
        if (activity.config && activity.config.cloud_provider == 1)
          return true;
        else if (activity.config && activity.config.cloud_provider == 2)
          switch(item.name) {
            case "ADVANCED_SETTINGS.CLOUD_RESOURCES": return true;
            case "ADVANCED_SETTINGS.VIRTUAL_ENVIRONMENT": return true;
            case "ADVANCED_SETTINGS.ENVIRONMENTAL_MONITOR": return true;
            default: return false;
          }
        else if (item.name == "ADVANCED_SETTINGS.CLOUD_RESOURCES")
          return true;
        else
          return false;
      } else {
        return true;
      }
    }

    $scope.navLink = function(item) {
      return $state.href(item.state, {
        name: activity.name
      }, {});
    }

  })
  .controller('editController', function($rootScope, $scope, $filter, $uibModal, dialog, session, activityService, api, activity, speech) {
    $scope.$emit('pageName', 'SETTINGS.EDIT_ACTIVITY');

    $scope.animationsEnabled = true;

    activity.event_start_time = new Date(activity.event_start_time);
    activity.event_end_time = new Date(activity.event_end_time);
    activity.registration_start_time = new Date(activity.registration_start_time);
    activity.registration_end_time = new Date(activity.registration_end_time);
    activity.judge_start_time = new Date(activity.judge_start_time);
    activity.judge_end_time = new Date(activity.judge_end_time);
    activity.config.max_enrollment = parseInt(activity.config.max_enrollment, 10) || 0;

    $scope.modules = activity;
    $scope.data = {
      newBannerUrl: ""
    };
    $scope.tags = [];
    $scope.activityFormDisabled = false;
    angular.forEach($scope.modules.tags, function(value, key) {
      $scope.tags.push({
        text: value
      });
    });

    $scope.provider = {
      windows: $filter('isProvider')(activity.config.login_provider, 1),
      github: $filter('isProvider')(activity.config.login_provider, 2),
      qq: $filter('isProvider')(activity.config.login_provider, 4),
      weibo: $filter('isProvider')(activity.config.login_provider, 8),
      alauda: $filter('isProvider')(activity.config.login_provider, 32),
      // gitcafe: $filter('isProvider')(activity.config.login_provider, 16),
      wechat: $filter('isProvider')(activity.config.login_provider, 64),
    };

    $scope.open = {
      event_start_time: false,
      event_end_time: false,
      registration_start_time: false,
      registration_end_time: false,
      judge_start_time: false,
      judge_end_time: false
    };

    $scope.showTip = function(level, content, showTime) {
      $scope.$emit('showTip', {
        level: level,
        content: content,
        showTime: showTime || 3000
      });
    };

    //update basic information
    $scope.updateBasicInformation = function() {
      $scope.modules.tags = [];
      angular.forEach($scope.tags, function(value, key) {
        $scope.modules.tags.push(value.text);
      });

      $scope.activityFormDisabled = true;
      api.admin.hackathon.put({
        header: {
          hackathon_name: $scope.modules.name
        },
        body: $scope.modules
      }, function(data) {
        if (data.error) {
          $scope.showTip(level = 'tip-danger', content = data.error.friendly_message);
        } else {
          $scope.showTip(level = 'tip-success', content = '更新成功');
        }
        $scope.activityFormDisabled = false;
      });
    };

    $scope.delBanner = function(index) {
      $scope.modules.banners.splice(index, 1);
      api.admin.hackathon.put({
        header: {
          hackathon_name: $scope.modules.name
        },
        body: {
          banners: $scope.modules.banners
        }
      }, function(data) {
        if (data.error) {
          $scope.showTip(level = 'tip-danger', content = data.error.friendly_message);
        } else {
          $scope.showTip(level = 'tip-success', content = '删除成功');
        }
      });
    };

    function addBanner() {
      $scope.modules.banners.push($scope.data.newBannerUrl);
      $scope.data.newBannerUrl = "";

      api.admin.hackathon.put({
        header: {
          hackathon_name: $scope.modules.name
        },
        body: {
          banners: $scope.modules.banners
        }
      }, function(data) {
        if (data.error) {
          $scope.showTip(level = 'tip-danger', content = data.error.friendly_message);
        } else {
          $scope.showTip(level = 'tip-success', content = '添加成功');
        }
      });
    };

    $scope.showAddBannerDialog = function() {
      var data = $scope.data;
      $uibModal.open({
        templateUrl: 'addBannersModel.html',
        controller: function($scope, $uibModalInstance) {
          $scope.data = data;
          $scope.addBanner = addBanner;
          $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel');
          };
        },
      });
    };

    $scope.updateDescription = function() {
      api.admin.hackathon.put({
        header: {
          hackathon_name: $scope.modules.name
        },
        body: {
          description: $scope.modules.description
        }
      }, function(data) {
        if (data.error) {
          $scope.showTip(level = 'tip-danger', content = data.error.friendly_message);
        } else {
          $scope.showTip(level = 'tip-success', content = '保存成功');
        }
      });
    };

    $scope.providerChange = function(checked, value) {
      if (checked) {
        $scope.modules.config.login_provider = ((+$scope.modules.config.login_provider) || 0) | value;
      } else {
        $scope.modules.config.login_provider = ((+$scope.modules.config.login_provider) || 0) ^ value;
      }

      api.admin.hackathon.config.put({
        header: {
          hackathon_name: $scope.modules.name
        },
        body: {
          login_provider: $scope.modules.config.login_provider
        }
      }).then(function(data) {
        if (data.error) {
          $scope.showTip(level = 'tip-danger', content = data.error.friendly_message);
        } else {
          $scope.showTip(level = 'tip-success', content = '登录方式修改成功');
        }
      });
    };

  })
  .controller('usersController', function($rootScope, $scope, activity, api, util, dialog) {
    $scope.$emit('pageName', 'SETTINGS.USERS');

    $scope.data = {
      registerUsers: [], // we cannot use activity.registration here, its single
      regStatus: {},
      checkAll: false,
      checks: {},
      perPage: 20,
      curPage: 1,

      freedomTeam: false,
      autoApprove: false
    };
    $scope.filterCondition = -1; // see HACK_USER_STATUS, -1 means all types

    var showTip = function(level, content, showTime) {
      $scope.$emit('showTip', {
        level: level,
        content: content,
        showTime: showTime || 3000
      });
    };

    $scope.filterUser = function(item) {
      if ($scope.filterCondition == -1)
        return true;
      return item.status == $scope.filterCondition;
    }

    var refresh = function() {
      api.admin.registration.list.get({
        header: {
          hackathon_name: activity.name
        }
      }, function(data) {
        if (data.error) {
          showTip('tip-danger', data.error.friendly_message);
        } else {
          $scope.data.regStatus = {};
          $scope.data.checkAll = false;
          $scope.data.checks = {};

          var i;
          for (i = 0; i < data.length; i++) {
            $scope.data.regStatus[data[i].id] = '' + data[i].status;
            $scope.data.checks[data[i].id] = false;
          }

          $scope.data.registerUsers = data;
          $scope.data.curPage = 1;

          $scope.data.autoApprove = activity.config.auto_approve;
          $scope.data.freedomTeam = activity.config.freedom_team;
        }
      });
    };

    var _updateBatch = function(updates, status, index) {
      if (index >= updates.length) {
        return;
      }

      var reg = updates[index];

      api.admin.registration.put({
        header: {
          hackathon_name: activity.name
        },
        body: {
          id: reg.id,
          status: status,
        },
      }, function(data) {
        if (data.error) {
          showTip('tip-danger', 'some of the registration updates failed: ' + data.error.friendly_message);
        } else {
          if (index == updates.length - 1)
            showTip('tip-success', '批量修改状态成功');
          reg.status = parseInt(status);
          $scope.data.regStatus[reg.id] = '' + reg.status;
        }

        _updateBatch(updates, status, index + 1);
      });
    };

    $scope.updateStatus = function(reg) {
      return api.admin.registration.put({
        header: {
          hackathon_name: activity.name
        },
        body: {
          id: reg.id,
          status: parseInt($scope.data.regStatus[reg.id]),
        }
      }, function(data) {
        if (data.error) {
          showTip('tip-danger', data.error.friendly_message);
          $scope.data.regStatus[reg.id] = '' + reg.status;
        } else {
          showTip('tip-success', '修改状态成功');
          reg.status = parseInt($scope.data.regStatus[reg.id]);
        }
      });
    };

    $scope.updateStatusBatch = function(status) {
      var i, id, reg;

      var toUpdate = [];
      for (i = 0; i < $scope.data.registerUsers.length; i++) {
        reg = $scope.data.registerUsers[i];
        id = reg.id;

        if ($scope.data.checks[id])
          toUpdate.push(reg);
      }

      _updateBatch(toUpdate, status, 0);
    };

    $scope.toggleCheckAll = function() {
      var i;
      for (i = 0; i < $scope.data.registerUsers.length; i++) {
        $scope.data.checks[$scope.data.registerUsers[i].id] = $scope.data.checkAll;
      }
    };

    $scope.checkCheckAll = function() {
      var i = 0,
        allChecked = true;
      for (i = 0; i < $scope.data.registerUsers.length; i++) {
        if (!$scope.data.checks[$scope.data.registerUsers[i].id]) {
          allChecked = false;
          break;
        }
      }

      $scope.data.checkAll = allChecked;
    };

    $scope.updateConfig = function() {
      api.admin.hackathon.put({
        header: {
          hackathon_name: activity.name
        },
        body: {
          config: {
            auto_approve: $scope.data.autoApprove,
            freedom_team: $scope.data.freedomTeam
          }
        }
      }, function(data) {
        if (data.error) {
          showTip('tip-danger', data.error.friendly_message);
          $scope.data.autoApprove = activity.config.auto_approve;
          $scope.data.freedomTeam = activity.config.freedom_team;
        } else {
          showTip('tip-success', '修改配置成功');
          activity.config.auto_approve = $scope.data.autoApprove;
          activity.config.freedom_team = $scope.data.freedomTeam;
        }
      });
    };

    refresh();
  })
  .controller('adminController', function($rootScope, $scope, $stateParams, $uibModal, api, dialog) {
    $scope.$emit('pageName', 'SETTINGS.ADMINISTRATORS');

    $scope.data = {
        admins: [],
        adminsType: [],
        adminsRemark: [],
        selectedAdmins: [],
        searchKeyword: "", // search users to create admins
        searchResult: [],
        selectedUserId: "", // user selected to create an admin
        selectedUserCSS: {
          color: 'red'
        }, // todo remove this line when CSS done
        selectedUserRole: 1,
        selectedUserRemark: ""
      }
      // 0-no condition, 1-admin, 2-judge
    $scope.filterAdminCondition = 0;

    function showTip(level, content, showTime) {
      $scope.$emit('showTip', {
        level: level,
        content: content,
        showTime: showTime || 3000
      });
    };

    $scope.filterAdmin = function(item) {
      if ($scope.filterAdminCondition == 0)
        return true;
      return item.role == $scope.filterAdminCondition;
    }

    $scope.updateAdmin = function(admin) {
      api.admin.hackathon.administrator.put({
        header: {
          hackathon_name: $stateParams.name
        },
        body: {
          id: admin.id,
          role: parseInt($scope.data.adminsType[admin.id]),
          remark: $scope.data.adminsRemark[admin.id]
        }
      }, function(data) {
        if (data.error)
          showTip('tip-danger', data.error.friendly_message);
        else {
          // update $scope.data.admin
          admin.role = parseInt($scope.data.adminsType[admin.id]);
          admin.remark = $scope.data.adminsRemark[admin.id];
          showTip('tip-success', '修改管理员成功');
        }
      });
    }

    $scope.isAdminSelected = function(admin) {
      return $scope.data.selectedAdmins.indexOf(admin.id) > -1;
    }

    $scope.isAllAdminsSelected = function() {
      return $scope.data.selectedAdmins.length == $scope.data.admins.length;
    }

    $scope.selectAdmin = function(admin) {
      var index = $scope.data.selectedAdmins.indexOf(admin.id)
      if (index > -1) {
        $scope.data.selectedAdmins.splice(index, 1)
      } else {
        $scope.data.selectedAdmins.push(admin.id)
      }
    }

    $scope.toggleAllAdmins = function() {
      if ($scope.data.admins.length == $scope.data.selectedAdmins.length) {
        $scope.data.selectedAdmins = [];
      } else {
        $scope.data.selectedAdmins = [];
        for (var index in $scope.data.admins)
          $scope.data.selectedAdmins.push($scope.data.admins[index].id);
      }
    }

    $scope.deleteAdmins = function() {
      deleteAdminList($scope.data.selectedAdmins, 0);

      // delete admins one by one
      function deleteAdminList(list, index) {
        if (index == list.length) {
          pageLoad();
          return;
        }
        api.admin.hackathon.administrator.delete({
          header: {
            hackathon_name: $stateParams.name
          },
          query: {
            id: list[index]
          }
        }, function(data) {
          if (data.error)
            showTip('tip-danger', data.error.friendly_message);
          else
            showTip('tip-success', '成功删除管理员!');
          deleteAdminList(list, index + 1);
        });
      }
    }

    $scope.showSearchBar = function() {
      var data = $scope.data;

      $uibModal.open({
        templateUrl: 'addAdminModel.html',
        controller: function($scope, $uibModalInstance) {
          $scope.data = data;
          $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel');
          };

          $scope.searchAdmin = searchAdmin;
          $scope.selectSearchedUser = selectSearchedUser;
          $scope.createAdmin = createAdmin;
        },
      });
    }

    function searchAdmin() {
      api.admin.user.list.get({
        header: {
          hackathon_name: $stateParams.name
        },
        query: {
          keyword: $scope.data.searchKeyword
        }
      }, function(data) {
        if (data.error)
          showTip('tip-danger', data.error.friendly_message);
        else
          $scope.data.searchResult = data.items;
      });
    }

    function selectSearchedUser(user) {
      $scope.data.selectedUserId = user.id;
    }

    function createAdmin() {
      if ($scope.data.selectedUserId == "") {
        showTip('tip-danger', "请选择需要设置成管理员的用户!");
      } else {
        api.admin.hackathon.administrator.post({
          header: {
            hackathon_name: $stateParams.name
          },
          body: {
            id: $scope.data.selectedUserId,
            role: $scope.data.selectedUserRole,
            remark: $scope.data.selectedUserRemark
          }
        }, function(data) {
          if (data.error)
            showTip('tip-danger', data.error.friendly_message);
          else {
            showTip('tip-success', '创建管理员成功');
            pageLoad();
          }
        });
      }
    }

    function pageLoad() {
      api.admin.hackathon.administrator.list.get({
        header: {
          hackathon_name: $stateParams.name
        }
      }, function(data) {
        if (data.error)
          showTip('tip-danger', data.error.friendly_message);
        else {
          $scope.data.admins = data;
          $scope.data.adminsType = [];
          $scope.data.adminsRemark = [];
          $scope.data.selectedAdmins = [];
          $scope.data.searchKeyword = "";
          $scope.data.searchResult = [];
          $scope.data.selectedUserId = "";
          $scope.data.selectedUserRole = 1;
          $scope.data.selectedUserRemark = "";
          for (var index in data) {
            $scope.data.adminsType[data[index].id] = data[index].role.toString();
            $scope.data.adminsRemark[data[index].id] = data[index].remark;
          }
        }
      })
    }

    // init the page and load admins-list
    pageLoad();
  })
  .controller('organizersController', function($rootScope, $scope, $stateParams, $uibModal, $cookies, api) {
    $scope.$emit('pageName', 'SETTINGS.ORGANIZERS');

    $scope.filterCondition = 0;
    $scope.data = {
        "organizers": [],
        "selectedOrgs": []
    }

    // very quick and rough implementation, please feel free to update
    var refresh = function(data) {
      if (data) {
        $scope.data.organizers = data.organizers;
        $scope.data.selectedOrgs = [];
        return
      }

      api.admin.hackathon.get({
        header: {
          hackathon_name: $stateParams.name
        }
      }).then(function(data) {
        if (data.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: data.error.friendly_message
          });
        } else {
          $scope.data.organizers = data.organizers;
          $scope.data.selectedOrgs = [];
        }
      })
    }

    var openModel = function(org) {
      $uibModal.open({
        templateUrl: 'orgModel.html',
        controller: function($scope, $stateParams, $uibModalInstance, $cookies, api) {
          $scope.org = org;
          $scope.org.organization_type = $scope.org.organization_type.toString();

          $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel');
          };

          $scope.add_organizer = function() {
            org.organization_type = parseInt(org.organization_type);
            var fn;
            if (org.id) {
              fn = api.admin.hackathon.organizer.put
            } else {
              fn = api.admin.hackathon.organizer.post
            }
            fn({
              body: $scope.org,
              header: {
                hackathon_name: $stateParams.name,
                token: $cookies.get('token')
              }
            }).then(function(data) {
              if (data.error) {
                $scope.$emit('showTip', {
                  level: 'tip-danger',
                  content: data.error.friendly_message
                });
              } else {
                $scope.$emit('showTip', {
                  level: 'tip-success',
                  content: "操作成功"
                });
                refresh(data)
              }
              $uibModalInstance.close();
            })
          };
        }, // end controller
      })
    }

    $scope.edit = function(org) {
      openModel(org)
    }

    $scope.add = function() {
      var newOrg = {
        organization_type: "1"
      }
      openModel(newOrg)
    }

    $scope.delete_organizer = function(organizer_id) {
      api.admin.hackathon.organizer.delete({
        query: {
          id: organizer_id
        },
        header: {
          hackathon_name: $stateParams.name,
          token: $cookies.get('token')
        }
      }).then(function(data) {
        if (data.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: data.error.friendly_message
          });
        } else {
          $scope.$emit('showTip', {
            level: 'tip-success',
            content: "删除成功"
          });
          $scope.data.organizers = $scope.data.organizers.filter(function(organizer) {
            return organizer.id != organizer_id
          })
        }
      })
    }

    $scope.filterOrganizer = function(item) {
      if ($scope.filterCondition == 0)
        return true;
      return item.organization_type == $scope.filterCondition;
    }

    $scope.isOrganizerSelected = function(org) {
      return $scope.data.selectedOrgs.indexOf(org.id) > -1;
    }

    $scope.isAllOrgsSelected = function() {
      return $scope.data.selectedOrgs.length == $scope.data.organizers.length;
    }

    $scope.selectOrg = function(org) {
      var index = $scope.data.selectedOrgs.indexOf(org.id)
      if (index > -1) {
        $scope.data.selectedOrgs.splice(index, 1)
      } else {
        $scope.data.selectedOrgs.push(org.id)
      }
    }

    $scope.toggleAllOrgs = function() {
      if ($scope.data.organizers.length == $scope.data.selectedOrgs.length) {
        $scope.data.selectedOrgs = [];
      } else {
        $scope.data.selectedOrgs = [];
        for (var index in $scope.data.organizers)
          $scope.data.selectedOrgs.push($scope.data.organizers[index].id);
      }
    }

    $scope.deleteSelectedOrgs = function() {
      for (index in $scope.data.selectedOrgs)
        $scope.delete_organizer($scope.data.selectedOrgs[index]);
    }

    refresh();
  })
  .controller('awardsController', function($rootScope, $scope, $stateParams, $q, api) {
    $scope.$emit('pageName', 'SETTINGS.AWARDS');

    var data = $scope.data = {
      grantedAwards: [],
      awards: [],
      teams: [],
      awardsMap: [],
      currentScores: [],
      searchText: '',
      hackathon_name: $stateParams.name,

      perPage: 6,
      curPage: 1,
    };

    $scope.getTeamScore = function(team) {
      getTeamScore(team);
    }

    function getTeamScore(team) {
      return api.admin.team.score.list.get({
         query: {team_id: team.id},
         header: {hackathon_name: $stateParams.name}
      }).then(function(res) {
        if (res.error) {
          showTip('tip-danger', error.friendly_message);
          return;
        }

        var currentScores = [];
        currentScores.scores= res.all;
        var sum = 0;
        var i = 0;
        for (i = 0; i < currentScores.scores.length; i++) {
          sum += currentScores.scores[i].score;
        }
        currentScores.average = sum / currentScores.scores.length;
        $scope.currentScores = currentScores;
        return;
      });
    }

    $scope.grantAward = function(awardId, teamId) {
      grantAward(awardId, teamId);
    };

    $scope.cancelAward = function(awardId, teamId) {
      cancelAward(awardId, teamId);
    }

    function cancelAward(awardId, teamId) {
      return api.admin.team.award.delete({
        header: {
          hackathon_name: $stateParams.name
        },
        query: {
          award_id: awardId,
          team_id: teamId
        }
      }).then(function(data) {
        if (data.error) {
          showTip('tip-danger', error.friendly_message);
          return;
        }

        showTip('tip-success', '删除成功');

        return refresh();
      });
    }

    function grantAward(awardId, teamId) {
      return api.admin.team.award.post({
        header: {
          hackathon_name: $stateParams.name
        },
        body: {
          award_id: awardId,
          team_id: teamId
        }
      }).then(function(data) {
        if (data.error) {
          showTip('tip-danger', error.friendly_message);
          return;
        }

        showTip('tip-success', '颁奖成功');

        return refresh();
      });
    }

    function refresh() {
      return $q.all([
        api.hackathon.grantedawards.get({
          header: {
            hackathon_name: $stateParams.name
          }
        }),
        api.admin.hackathon.award.list.get({
          header: {
            hackathon_name: $stateParams.name
          }
        }),
        api.hackathon.team.list.get({
          header: {
            hackathon_name: $stateParams.name
          }
        }),
      ]).then(function(res) {
        if (res[0].error || res[1].error || res[2].error) {
          throw res[0].error || res[1].error || res[2].error;
        }

        data.grantedAwards = res[0];
        data.awards = res[1];
        data.teams = res[2];

        var i;
        for (i = 0; i < data.awards.length; i++) {
          data.awardsMap[data.awards[i].id] = data.awards[i];
        }

        return data;
      }).catch(function(error) {
        showTip('tip-danger', error.friendly_message);
      });
    }

    function showTip(level, msg) {
      $scope.$emit('showTip', {
        level: level,
        content: msg
      });
    };

    refresh();
  })
  .controller('noticesController', function($rootScope, $scope, $uibModal, activity, api, dialog) {
    $scope.$emit('pageName', 'SETTINGS.NOTICES');

    $scope.data = {
      notices: [],
      selectedNotices: [],
      noticeDescription: {
        0: '黑客松信息',
        1: '用户信息',
        2: '实验信息',
        3: '获奖信息',
        4: '模板信息'
      },
      noticeIcon: {
        0: 'glyphicon glyphicon-bullhorn', //'黑客松信息'
        1: 'glyphicon glyphicon-user', //'用户信息'
        2: 'glyphicon glyphicon-th-list', //'实验信息'
        3: 'glyphicon glyphicon-gift', //'获奖信息'
        4: 'glyphicon glyphicon-edit' //'模板信息'
      }
    };

    var formData = {};

    var showTip = function(level, msg) {
      $scope.$emit('showTip', {
        level: level,
        content: msg
      });
    };

    var getHackathonNotices = function() {
      return api.hackathon.notice.list.get({
        query: {
          hackathon_name: activity.name,
          order_by: 'time'
        }
      }, function(data) {
        if (data.error) {
          showTip('tip-danger', data.error.friendly_message);
          return;
        }

        activity.notices = data.items;
        $scope.data.notices = data.items;
        $scope.data.selectedNotices = []
      });
    };

    var createNotice = function() {
      var notice = formData;
      var fn;

      if (notice.id)
        fn = api.admin.hackathon.notice.put;
      else
        fn = api.admin.hackathon.notice.post;

      return fn({
        body: notice,
        header: {
          hackathon_name: activity.name
        }
      }, function(data) {
        if (data.error)
          showTip('tip-danger', data.error.friendly_message);
        else {
          showTip('tip-success', '创建/修改成功');
          getHackathonNotices();
        }
      });
    };

    var openAddNoticeWizard = function() {
      $uibModal.open({
        templateUrl: 'noticeModel.html',
        controller: function($scope, $uibModalInstance) {
          $scope.data = formData;

          $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel');
          };

          $scope.addNotice = function() {
            createNotice();
            $uibModalInstance.close();
          };
        },
      });
    };

    $scope.delete = function(id) {
      dialog.confirm({
        title: '提示',
        body: '确认删除该条公告?'
      }).then(function() {
        api.admin.hackathon.notice.delete({
          query: {
            id: id
          },
          header: {
            hackathon_name: activity.name
          }
        }).then(function(data) {
          if (data.error)
            showTip('tip-danger', data.error.friendly_message);
          else {
            showTip('tip-success', '删除成功');
            return getHackathonNotices();
          }
        });
      });
    };

    $scope.edit = function(idx) {
      formData = activity.notices[idx];
      openAddNoticeWizard();
    };

    $scope.add = function() {
      formData = {
        content: '',
        link: '',
        category: 0,
      };
      openAddNoticeWizard();
    };

    $scope.isNoticeSelected = function(notice) {
      return $scope.data.selectedNotices.indexOf(notice.id) > -1;
    }

    $scope.isAllNoticesSelected = function() {
      return $scope.data.selectedNotices.length == $scope.data.notices.length;
    }

    $scope.selectNotice = function(notice) {
      var index = $scope.data.selectedNotices.indexOf(notice.id)
      if (index > -1) {
        $scope.data.selectedNotices.splice(index, 1)
      } else {
        $scope.data.selectedNotices.push(notice.id)
      }
    }

    $scope.toggleAllNotices = function() {
      if ($scope.data.notices.length == $scope.data.selectedNotices.length) {
        $scope.data.selectedNotices = [];
      } else {
        $scope.data.selectedNotices = [];
        for (var index in $scope.data.notices)
          $scope.data.selectedNotices.push($scope.data.notices[index].id);
      }
    }

    $scope.deleteSelectedNotices = function() {
      function batchDelete(list, index) {
        if (list.length == index) {
          if (index != 0) {
            showTip('tip-success', '批量删除成功');
            $scope.data.selectedNotices = [];
            getHackathonNotices();
          }
          return
        }
        api.admin.hackathon.notice.delete({
          query: {
            id: list[index]
          },
          header: {
            hackathon_name: activity.name
          }
        }).then(function(data) {
          if (data.error)
            showTip('tip-danger', data.error.friendly_message);
          batchDelete(list, index + 1);
        });
      }
      // batch delete all selected notices.
      batchDelete($scope.data.selectedNotices, 0);
    }

    getHackathonNotices();
  })
  .controller('prizesController', function($rootScope, $scope, $uibModal, activity, api, dialog) {
    $scope.$emit('pageName', 'SETTINGS.PRIZES');

    $scope.data = {
      awards: activity.awards,
      selectedPrizes: [] // use to batch-delete prizes
    };

    var formData = {}; // use to create/edit the prize.

    var showTip = function(level, msg) {
      $scope.$emit('showTip', {
        level: level,
        content: msg
      });
    };

    var refreshAward = function() {
      return api.admin.hackathon.award.list.get({
        header: {
          hackathon_name: activity.name
        }
      }, function(data) {
        if (data.error) {
          showTip('tip-danger', data.error.friendly_message);
          return;
        }

        activity.awards = data;
        $scope.data.awards = data;
        $scope.data.selectedPrizes = [];
      });
    };

    var createAward = function() {
      var award = formData;
      var fn;

      if (award.id)
        fn = api.admin.hackathon.award.put;
      else
        fn = api.admin.hackathon.award.post;

      return fn({
        body: award,
        header: {
          hackathon_name: activity.name
        }
      }, function(data) {
        if (data.error)
          showTip('tip-danger', data.error.friendly_message);
        else {
          showTip('tip-success', '创建/修改成功');
          refreshAward();
        }
      });
    };

    var openAddAwardWizard = function() {
      $uibModal.open({
        templateUrl: 'awardModel.html',
        controller: function($scope, $uibModalInstance) {
          $scope.data = formData;

          $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel');
          };

          $scope.addAward = function() {
            createAward();
            $uibModalInstance.close();
          };
        }, // end controller
      });
    };

    $scope.delete = function(id) {
      dialog.confirm({
        title: '提示',
        body: '确认删除此奖项?'
      }).then(function() {
        api.admin.hackathon.award.delete({
          query: {
            id: id
          },
          header: {
            hackathon_name: activity.name
          }
        }).then(function(data) {
          if (data.error)
            showTip('tip-danger', data.error.friendly_message);
          else {
            showTip('tip-success', '删除成功');
            return refreshAward();
          }
        });
      });
    };

    $scope.edit = function(idx) {
      formData = activity.awards[idx];
      openAddAwardWizard();
    };

    $scope.add = function() {
      formData = {
        name: '',
        award_url: '',
        level: 5,
        quota: 1,
        description: '',
      };
      openAddAwardWizard();
    };

    $scope.isPrizeSelected = function(prize) {
      return $scope.data.selectedPrizes.indexOf(prize.id) > -1;
    }

    $scope.isAllPrizesSelected = function() {
      return $scope.data.selectedPrizes.length == $scope.data.awards.length;
    }

    $scope.selectPrize = function(prize) {
      var index = $scope.data.selectedPrizes.indexOf(prize.id)
      if (index > -1) {
        $scope.data.selectedPrizes.splice(index, 1)
      } else {
        $scope.data.selectedPrizes.push(prize.id)
      }
    }

    $scope.toggleAllPrizes = function() {
      if ($scope.data.awards.length == $scope.data.selectedPrizes.length) {
        $scope.data.selectedPrizes = [];
      } else {
        $scope.data.selectedPrizes = [];
        for (var index in $scope.data.awards)
          $scope.data.selectedPrizes.push($scope.data.awards[index].id);
      }
    }

    $scope.deleteSelectedPrizes = function() {
      function batchDelete(list, index) {
        if (list.length == index) {
          if (index != 0) {
            showTip('tip-success', '批量删除成功');
            $scope.data.selectedPrizes = [];
            refreshAward();
          }
          return
        }
        api.admin.hackathon.award.delete({
          query: {
            id: list[index]
          },
          header: {
            hackathon_name: activity.name
          }
        }).then(function(data) {
          if (data.error)
            showTip('tip-danger', data.error.friendly_message);
          batchDelete(list, index + 1);
        });
      }

      // batch delete all selected prizes.
      batchDelete($scope.data.selectedPrizes, 0);
    }

    refreshAward();
  })
  .controller('veController', function($rootScope, $scope, $filter, $cookies, FileUploader, activity, api) {
    $scope.$emit('pageName', 'ADVANCED_SETTINGS.VIRTUAL_ENVIRONMENT');

    api.template.list.get({}).then(function(data) {
      $scope.image_templates = data;
    });

    api.admin.hackathon.template.list.get({
      header: {
        hackathon_name: activity.name
      }
    }, function(data) {
      $scope.templates = data;
    })

    var uploader = $scope.uploader = new FileUploader({
      url: api.template.file.post._path_,
      headers: {
        token: $cookies.get('token')
      },
      autoUpload: true
    });

    uploader.filters.push({
      name: 'templateFilter',
      fn: function(item, options) {
        return /js|json|txt$/i.test(item.name);
      }
    });

    uploader.onWhenAddingFileFailed = function(item, filter, options) {
      $scope.$emit('showTip', {
        level: 'tip-warning',
        content: '模板文件必须是txt、js、json后缀名'
      });
    };

    uploader.onCompleteItem = function(fileItem, data) {
      if (data.error) {
        $scope.$emit('showTip', {
          level: 'tip-danger',
          content: data.error.friendly_message
        });
      } else {
        api.template.list.get({}).then(function(data) {
          $scope.image_templates = data;
        });
      }
    };

    uploader.onCompleteAll = function() {
      $scope.file = '';
    };

    $scope.oneAtATime = true;
    $scope.templates = [];
    $scope.search = {
      status: '-1'
    }
    $scope.searchTemplates = function() {
      api.template.list.get({
        query: $scope.search
      }).then(function(data) {
        $scope.image_templates = data;
      });
    }

    $scope.testchange = function($event, id) {
      var result
      if ($event.target.checked) {
        result = api.admin.hackathon.template.post({
          body: {
            template_id: id
          },
          header: {
            hackathon_name: activity.name
          }
        });
      } else {
        result = api.admin.hackathon.template.delete({
          query: {
            template_id: id
          },
          header: {
            hackathon_name: activity.name
          }
        });
      }
      result.then(function(data) {
        if (data.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: data.error.friendly_message
          });
        } else {
          $scope.templates = data;
        }
      })
    }

    $scope.expression = function($event) {
      $event.stopPropagation();
    }

    $scope.isChecked = function(list, id) {
      var obj = $filter('filter')(list, {
        id: id
      }, true);
      return obj.length > 0 ? true : false;
    }
    $scope.templateSubmit = function() {
      if ($scope.templates.length > 0) {
        $scope.wizard = 4;
        updateActivityStatus(0);
      } else {
        $scope.$emit('showTip', {
          level: 'tip-danger',
          content: '请绑定模板'
        });
      }
    }

  })
  .controller('monitorController', function($rootScope, $scope, $stateParams, $window, $timeout, api, dialog) {
    $scope.$emit('pageName', 'ADVANCED_SETTINGS.ENVIRONMENTAL_MONITOR');

    $scope.data = {
        "experiments": [],
        "totalExprLength": 0,
        "curPage": 1,
        "perPage": 10
      }
      // filterCondition -1:all, 2:running, 3:stopped, 5:fail
    $scope.filterExperimentCondition = -1;
    $scope.searchKeyword = "";

    $scope.filterExperiments = function(item) {
      if ($scope.filterExperimentCondition == -1)
        return true;
      return item.status == $scope.filterExperimentCondition;
    }

    function showTip(level, content, showTime) {
      $scope.$emit('showTip', {
        level: level,
        content: content,
        showTime: showTime || 3000
      });
    };

    $scope.updateExperiment = function(experiment) {
      showTip('tip-danger', "暂时不支持更新操作");
    }

    $scope.deleteExperiment = function(experiment) {
      showTip('tip-danger', "暂时不支持删除操作");
    }

    $scope.refreshExperiment = function(experiment) {
      dialog.confirm({
        title: '提示',
        body: '是否尝试启动已停止的环境/容器?'
      }).then(function() {
        api.admin.experiment.put({
          body: {
            experiment_id: experiment.id
          },
          header: {
            hackathon_name: $stateParams.name
          }
        }).then(function(data) {
          if (data.error)
            showTip('tip-danger', data.error.friendly_message);
          else {
            showTip('tip-success', '操作成功! 启动环境/容器需要一定时间,三秒后自动刷新查看最新状态。');
            $timeout(function() {
              $window.location.reload()
            }, 3000);
          }
        });
      });
    }

    $scope.searchExperiment = function() {
      $scope.data.curPage = 1;
      if ($scope.searchKeyword == "") {
        pageLoad();
        return
      }

      api.admin.experiment.list.get({
        header: {
          hackathon_name: $stateParams.name
        },
        query: {
          user_name: $scope.searchKeyword,
          page: $scope.data.curPage,
          per_page: $scope.data.perPage
        }
      }).then(function(data) {
        if (data.error) {
          showTip('tip-danger', data.error.friendly_message);
        } else {
          if (data.items.length == 0)
            showTip('tip-danger', "未找到满足要求的结果");
          $scope.data.experiments = data.items;
          $scope.data.totalExprLength = data.total;
          $scope.filterExperimentCondition = -1;
        }
      })
    }

    $scope.changePage = function() {
      pageLoad();
    }

    function pageLoad() {
      api.admin.experiment.list.get({
        header: {
          hackathon_name: $stateParams.name
        },
        query: {
          page: $scope.data.curPage,
          per_page: $scope.data.perPage
        }
      }).then(function(data) {
        if (data.error) {
          showTip('tip-danger', data.error.friendly_message);
        } else {
          $scope.data.experiments = data.items;
          $scope.data.totalExprLength = data.total;
          $scope.filterExperimentCondition = -1;
          $scope.searchKeyword = ""
        }
      })
    }

    pageLoad();
  })
  .controller('cloudController', function($rootScope, $scope, $state, $window, $timeout, api, activity, dialog) {
    $scope.$emit('pageName', 'ADVANCED_SETTINGS.CLOUD_RESOURCES');

    // here $scope.cloudProdiver's type is String, like '1' or null.
    $scope.cloudProvider = null;
    $scope.isProviderSelected = true;
    $scope.isAzureCertSet = true;
    $scope.isTemplateSet = true;

    $scope.redirectTo = function(state) {
      return $state.href(state, {
        name: activity.name
      }, {});
    }

    function getCloudProvider() {
      api.admin.hackathon.config.get({
        header: {
          hackathon_name: activity.name
        }
      }).then(function(data) {
        if (data.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: data.error.friendly_message
          });
          $scope.isProviderSelected = false;
        } else {
          var provider = typeof(data["cloud_provider"]) == "undefined" ? null : data["cloud_provider"].toString();
          if (provider == '0' || provider == '1' || provider == '2'){
            $scope.isProviderSelected = true;
            $scope.cloudProvider = provider;
          } else {
            $scope.isProviderSelected = false;
          }

          // judge whether "AzureCert" and "Template" have been set.
          if ($scope.cloudProvider == '1' && activity.azure_keys.length == 0)
            $scope.isAzureCertSet = false;
          if (($scope.cloudProvider == '1' || $scope.cloudProvider == '2') && activity.templates.length == 0)
            $scope.isTemplateSet = false;
        }
      });
    }

    $scope.setCloudProvider = function() {
      dialog.confirm({
        title: '提示',
        body: '一旦确定服务商就不能在修改!是否确定?',
        icon: 'fa-exclamation',
        size: 'sm',
        status: 'warning'
      }).then(function() {
        api.admin.hackathon.config.put({
          header: {
            hackathon_name: activity.name
          },
          body: {
            cloud_provider: parseInt($scope.cloudProvider)
          }
        }).then(function(data) {
          if (data.error) {
            $scope.$emit('showTip', {
              level: 'tip-danger',
              content: data.error.friendly_message
            });
          } else {
            $scope.$emit('showTip', {
              level: 'tip-success',
              content: "设置服务商成功!等待刷新..."
            });

            $timeout(function() {
              $window.location.reload()
            }, 500);
          }
        });
      })
    };

    getCloudProvider();
  })
  .controller('azurecertController', function($rootScope, $scope, $stateParams, api, dialog) {
    $scope.$emit('pageName', 'ADVANCED_SETTINGS.AZURECERT');
    var isDeleteCerDisabled = false;
    $scope.azureFormDisabled = false;
    $scope.azure = {
      management_host: 'management.core.chinacloudapi.cn',
    };

    function getAzureCer() {
      api.admin.azure.get({
        header: {
          hackathon_name: $stateParams.name
        }
      }).then(function(data) {
        $scope.azurecerts = data;
        // console.log(data);
      });
    }

    $scope.checksubid = function(ecert) {
      api.admin.azure.checksubid.post({
        body: {
          subscription_id: ecert.subscription_id
        },
        header: {
          hackathon_name: $stateParams.name
        }
      }).then(function(data) {
      if (data.error) {
        $scope.$emit('showTip', {
        level: 'tip-warning',
        content: data.error.message
        });
      } else {
          $scope.$emit('showTip', {
            level: 'tip-success',
            content: '检验成功。'
          });
          ecert.verified = true;
        }
      })
    };

    $scope.azureFormSubmit = function() {
      $scope.azureFormDisabled = true;
      api.admin.azure.post({
        body: $scope.azure,
        header: {
          hackathon_name: $stateParams.name
        }
      }).then(function(azure_key) {
        if (azure_key.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: azure_key.error.friendly_message
          });
        } else {
          $scope.$emit('showTip', {
            level: 'tip-success',
            content: '创建SUBSCRIPTION ID成功'
          });
          $scope.azure.subscription_id = '';
          $scope.azure.management_host = 'management.core.chinacloudapi.cn';
        }
        $scope.azureFormDisabled = false;
      });
    };

    $scope.deleteCert = function(id) {
      if (!isDeleteCerDisabled) {
        isDeleteCerDisabled = !isDeleteCerDisabled;
        dialog.confirm({
          title: '提示',
          body: '确定删除此Azure证书?',
          icon: 'fa-exclamation',
          size: 'sm',
          status: 'warning'
        }).then(function() {
          api.admin.azure.delete({
            header: {
              hackathon_name: $stateParams.name
            },
            query: {
              certificate_id: id
            }
          }).then(function(data) {
            if (data.error) {
              $scope.$emit('showTip', {
                level: 'tip-danger',
                content: data.error.friendly_message
              });
            } else {
              $scope.$emit('showTip', {
                level: 'tip-success',
                content: '删除成功'
              });
              getAzureCer();
            }
            isDeleteCerDisabled = !isDeleteCerDisabled;
          });
        })
      }
    }

    getAzureCer();
  })
  .controller('serversController', function($rootScope, $scope, $stateParams, $uibModal, $cookies, api, dialog) {
    $scope.$emit('pageName', 'ADVANCED_SETTINGS.SERVERS');

    var refresh = function() {
      api.admin.hostserver.list.get({
        header: {
          hackathon_name: $stateParams.name,
          token: $cookies.get('token')
        }
      }).then(function(data) {
        if (data.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: data.error.friendly_message
          });
        } else {
          $scope.data = data
        }
      })
    }

    var afterAdd = function(new_server) {
      $scope.$emit('showTip', {
        level: 'tip-success',
        content: "添加成功"
      });
      $scope.data.push(new_server)
    }

    var afterUpdate = function(new_server) {
      $scope.$emit('showTip', {
        level: 'tip-success',
        content: "更新成功"
      });
      $scope.data = $scope.data.map(function(server) {
        if (server.id == new_server.id) {
          return new_server
        } else {
          return server
        }
      })
    }

    var openModel = function(data) {
      $uibModal.open({
        templateUrl: 'serverModel.html',
        controller: function($scope, $stateParams, $uibModalInstance, $cookies, api) {
          $scope.data = data;

          $scope.cancel = function() {
            $uibModalInstance.dismiss('cancel');
          };

          $scope.add_server = function() {
            var fn, af;
            if (data.id) {
              fn = api.admin.hostserver.put
              af = afterUpdate
            } else {
              fn = api.admin.hostserver.post
              af = afterAdd
            }
            fn({
              body: $scope.data,
              header: {
                hackathon_name: $stateParams.name,
                token: $cookies.get('token')
              }
            }).then(function(data) {
              if (data.error) {
                $scope.$emit('showTip', {
                  level: 'tip-danger',
                  content: data.error.friendly_message
                });
              } else {
                af(data)
              }
              $uibModalInstance.close();
            })
          };
        }, // end controller
      })
    }

    $scope.add = function() {
      openModel({})
    }

    $scope.edit = function(server) {
      openModel(server)
    }

    $scope.delete = function(server_id) {
      api.admin.hostserver.delete({
        query: {
          id: server_id
        },
        header: {
          hackathon_name: $stateParams.name,
          token: $cookies.get('token')
        }
      }).then(function(data) {
        if (data.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: data.error.friendly_message
          });
        } else {
          $scope.$emit('showTip', {
            level: 'tip-success',
            content: "删除成功"
          });
          $scope.data = $scope.data.filter(function(server) {
            return server.id != server_id
          })
        }
      })
    }

    $scope.getState = function(state) {
      desc = {
        0: "创建中",
        1: "创建中",
        2: "可用",
        3: "不可用"
      }
      return desc[state] || ""
    }

    refresh();
  })
  .controller('createController', function($scope, $timeout, $filter, $cookies, $state, $stateParams, activityService, FileUploader, dialog, api) {
    var activity = activityService.getCurrentActivity();

    $scope.activity = {
      location: '',
      tags: [],
      config: {
        cloud_provider: 0,
        auto_approve: true,
        recycle_enabled: false,
        recycle_minutes: 0,
        pre_allocate_enabled: false,
        pre_allocate_number: 1,
        freedom_team: false,
        login_provider: 111,
        dev_plan_required: false,
        real_name_auth_21v: false
      }
    }
    if (activity.status == -1) {
      $scope.wizard = 2;
      $scope.activity = activity;
    } else {
      $scope.wizard = 1;
    }

    var request;

    $scope.isShowAdvancedSettings = true;

    $scope.activityFormDisabled = false;

    $scope.$watch('activity.name', function(newValue, oldValue, scope) {
      if (request && newValue) {
        $timeout.cancel(request);
      }
      if (newValue) {
        request = $timeout(function() {
          api.admin.hackathon.checkname.get({
            query: {
              name: newValue
            }
          }, function(data) {
            $scope.activityForm.name.$setValidity('rometname', !data);
          });
        }, 400);
      }
    });

    $scope.$watch('wizard', function(newValue, oldValue, scope) {
      if (newValue == 3) {
        api.template.list.get({}).then(function(data) {
          $scope.image_templates = data;
        });
        api.admin.hackathon.template.list.get({
          header: {
            hackathon_name: $scope.activity.name
          }
        }, function(data) {
          $scope.templates = data;
        })
      } else if (newValue == 4) {
        updateActivityStatus(0).then(function(data) {
          if (data.error) {
            $scope.$emit('showTip', {
              level: 'tip-danger',
              content: data.error.friendly_message
            });
          }
        })
      }
    });

    function getConfigs() {
      var configs = [];
      angular.forEach($scope.configs, function(value, key) {
        configs.push({
          key: key,
          value: value
        });
      })
      return configs;
    }

    $scope.activitySubmit = function() {
      angular.forEach($scope.tags, function(value, key) {
        $scope.activity.tags.push(value.text);
      });

      $scope.activityForm.disabled = true;
      api.admin.hackathon.post({
        body: $scope.activity
      }, function(data) {
        if (data.error) {
          if (data.error.code == 412) {
            $scope.activityForm.name.$setValidity('rometname', false);
          } else {
            $scope.$emit('showTip', {
              level: 'tip-danger',
              content: data.error.friendly_message
            });
          }
          $scope.activityForm.disabled = false;
        } else {
          $scope.wizard = 2;
        }
      });
    };

    /*------------------------------------------------------*/
    $scope.notUseCloud = function() {
      dialog.confirm({
        title: '提示',
        body: '一旦确定服务商就不能在修改!是否确定?',
        icon: 'fa-exclamation',
        size: 'sm',
        status: 'warning'
      }).then(function() {
        updateActivityStatus(0).then(function(data) {
          if (data.error) {
            $scope.$emit('showTip', {
              level: 'tip-danger',
              content: data.error.friendly_message
            });
          } else {
            $scope.isShowAdvancedSettings = false;
            $scope.wizard = 4;
          }
        });
      });
    }

    $scope.cloudFormSubmit = function() {
      dialog.confirm({
        title: '提示',
        body: '一旦确定服务商就不能在修改!是否确定?',
        icon: 'fa-exclamation',
        size: 'sm',
        status: 'warning'
      }).then(function() {
        $scope.isAzureForm = $scope.cloudservices == 1;
        api.admin.hackathon.config.put({
          header: {
            hackathon_name: $scope.activity.name
          },
          body: {
            cloud_provider: parseInt($scope.cloudservices)
          }
        }).then(function(data) {
          if (data.error) {
            $scope.$emit('showTip', {
              level: 'tip-danger',
              content: data.error.friendly_message
            });
          }
        });

        if (!$scope.isAzureForm) {
          $scope.wizard = 3;
        }
      })
    };

    /*------------------------------------------------------*/
    $scope.azureFormDisabled = false;
    $scope.azure_download_cer = false;
    $scope.azure = {
      management_host: 'management.core.chinacloudapi.cn',
    };

    $scope.azureNext = function() {
      $scope.wizard = 3;
    }

    $scope.azureFormSubmit = function() {
      $scope.azureFormDisabled = true;
      api.admin.azure.post({
        body: $scope.azure,
        header: {
          hackathon_name: $scope.activity.name
        }
      }).then(function(azure_key) {
        if (azure_key.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: azure_key.error.friendly_message
          });
        } else {
          api.admin.azure.checksubid.post({
            body: {
              subscription_id: $scope.azure.subscription_id
            },
            header: {
              hackathon_name: $scope.activity.name
            }
          }).then(function(data) {
            if (!data.message) {
              $scope.$emit('showTip', {
                level: 'tip-warning',
                content: '证书授权失败,请检验SUBSCRIPTION ID是否正确。'
              });
            }
          })

          var cert_url = azure_key.cert_url;
          window.location.href = cert_url;
          $scope.azure_download_cer = true;
          $scope.azurecer = cert_url;
        }
        $scope.azureFormDisabled = false;
      });
    };
    /*------------------------------------------------------*/

    var uploader = $scope.uploader = new FileUploader({
      url: api.template.file.post._path_,
      headers: {
        token: $cookies.get('token')
      },
      autoUpload: true
    });

    uploader.filters.push({
      name: 'templateFilter',
      fn: function(item, options) {
        return /js|json|txt$/i.test(item.name);
      }
    });

    uploader.onWhenAddingFileFailed = function(item, filter, options) {
      $scope.$emit('showTip', {
        level: 'tip-warning',
        content: '模板文件必须是txt、js、json后缀名'
      });
    };

    uploader.onCompleteItem = function(fileItem, data) {
      if (data.error) {
        $scope.$emit('showTip', {
          level: 'tip-danger',
          content: data.error.friendly_message
        });
      } else {
        api.template.list.get({}).then(function(data) {
          $scope.image_templates = data;
        });
      }
    };

    uploader.onCompleteAll = function() {
      $scope.file = '';
    };

    $scope.oneAtATime = true;
    $scope.templates = [];
    $scope.search = {
      status: '-1'
    }
    $scope.searchTemplates = function() {
      api.template.list.get({
        query: $scope.search
      }).then(function(data) {
        $scope.image_templates = data;
      });
    }

    $scope.testchange = function($event, id) {
      var result
      if ($event.target.checked) {
        result = api.admin.hackathon.template.post({
          body: {
            template_id: id
          },
          header: {
            hackathon_name: $scope.activity.name
          }
        });
      } else {
        result = api.admin.hackathon.template.delete({
          query: {
            template_id: id
          },
          header: {
            hackathon_name: $scope.activity.name
          }
        });
      }
      result.then(function(data) {
        if (data.error) {
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: data.error.friendly_message
          });
        } else {
          $scope.templates = data;
        }
      })
    }

    $scope.expression = function($event) {
      $event.stopPropagation();
    }

    $scope.isChecked = function(list, id) {
      var obj = $filter('filter')(list, {
        id: id
      }, true);
      return obj.length > 0 ? true : false;
    }
    $scope.templateSubmit = function() {
      if ($scope.templates.length > 0) {
        $scope.wizard = 4;
        updateActivityStatus(0);
      } else {
        $scope.$emit('showTip', {
          level: 'tip-danger',
          content: '请绑定模板'
        });
      }
    }

    $scope.notUseTemplate = function() {
      $scope.wizard = 4;
    }

    /*------------------------------------------------------*/

    function updateActivityStatus(status) {
      return api.admin.hackathon.put({
        body: {
          status: status
        },
        header: {
          hackathon_name: $scope.activity.name
        }
      })
    }

    $scope.onlineDisabled = false;
    $scope.online = function() {
      $scope.onlineDisabled = true;
      api.admin.hackathon.online.post({
        header: {
          hackathon_name: $scope.activity.name
        }
      }).then(function(data) {
        $scope.onlineDisabled = false;
        if (data.error) {
          var message = ''
          if (data.error.code == 412101) {
            message = '当前黑客松没有还未创建成功。'
          } else if (data.error.code == 412102) {
            message = '证书授权失败,请检验SUBSCRIPTION ID是否正确。'
          } else {
            message = data.error.friendly_message
          }
          $scope.$emit('showTip', {
            level: 'tip-danger',
            content: message
          });
        } else {
          $scope.$emit('showTip', {
            level: 'tip-success',
            content: '上线成功'
          });
        }
      })
    }
  });


text:
'''
Files
=====

The following methods allow for interaction into the Tenable.io
:devportal:`file <file>` API endpoints.

Methods available on ``tio.files``:

.. rst-class:: hide-signature
.. autoclass:: FileAPI
    :members:
'''
from typing import BinaryIO
from .base import TIOEndpoint
import uuid

class FileAPI(TIOEndpoint):

    def upload(self, fobj: BinaryIO, encrypted: bool = False):
        '''
        Uploads a file into Tenable.io.

        :devportal:`file: upload <file-upload>`

        Args:
            fobj (FileObject):
                The file object intended to be uploaded into Tenable.io.
            encrypted (bool, optional):
                If the file is encrypted, set the flag to True.

        Returns:
            :obj:`str`:
                The fileuploaded attribute

        Examples:
            >>> with open('file.txt') as fobj:
            ...     file_id = tio.files.upload(fobj)
        '''

        # We will attempt to discover the name of the file stored within the
        # file object.  If none exists however, we will generate a random
        # uuid string to use instead.
        kw = dict()
        if encrypted:
            kw['data'] = {'no_enc': int(encrypted)}
        kw['files'] = {'Filedata': fobj}

        return self._api.post('file/upload', **kw).json()['fileuploaded']


text:
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
	<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Aggregation.V1.xml">
		<edmx:Include Alias="Aggregation" Namespace="Org.OData.Aggregation.V1"/>
	</edmx:Reference>
	<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Authorization.V1.xml">
		<edmx:Include Alias="Auth" Namespace="Org.OData.Authorization.V1"/>
	</edmx:Reference>
	<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Capabilities.V1.xml">
		<edmx:Include Alias="Capabilities" Namespace="Org.OData.Capabilities.V1"/>
	</edmx:Reference>
	<edmx:Reference Uri="https://wiki.scn.sap.com/wiki/download/attachments/448470974/Common.xml?api=v2">
		<edmx:Include Alias="Common" Namespace="com.sap.vocabularies.Common.v1"/>
	</edmx:Reference>
	<edmx:Reference Uri="https://wiki.scn.sap.com/wiki/download/attachments/448470971/Communication.xml?api=v2">
		<edmx:Include Alias="Communication" Namespace="com.sap.vocabularies.Communication.v1"/>
	</edmx:Reference>
	<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.xml">
		<edmx:Include Alias="Core" Namespace="Org.OData.Core.V1"/>
	</edmx:Reference>
	<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Measures.V1.xml">
		<edmx:Include Alias="Measures" Namespace="Org.OData.Measures.V1"/>
	</edmx:Reference>
	<edmx:Reference Uri="https://wiki.scn.sap.com/wiki/download/attachments/448470968/UI.xml?api=v2">
		<edmx:Include Alias="UI" Namespace="com.sap.vocabularies.UI.v1"/>
	</edmx:Reference>
	<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Validation.V1.xml">
		<edmx:Include Alias="Validation" Namespace="Org.OData.Validation.V1"/>
	</edmx:Reference>
	<edmx:DataServices>
		<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm">
			<Annotations Target="GWSAMPLE_BASIC.SalesOrder">
				<Annotation Term="UI.Facets">
					<Collection>
						<Record Type="UI.ReferenceFacet">
							<PropertyValue Property="Target" AnnotationPath="@UI.FieldGroup#SalesOrderCustomer"/>
							<PropertyValue Property="Label" String="{@i18n>CUSTOMER_INFORMATION}"/>
						</Record>
		<!--=======================================================================================================================================
			Records added to configure an object page section referencig UI.LineItem annotation	from different entity type
		===========================================================================================================================================-->
		<!--Start-->
						<Record Type="UI.ReferenceFacet">
							<PropertyValue Property="Target" AnnotationPath="ToLineItems/@UI.LineItem"/>
							<PropertyValue Property="Label" String="{@i18n>SALES_ORDER_ITEMS}"/>
						</Record>
		<!--End-->
					</Collection>
				</Annotation>
				<Annotation Term="UI.FieldGroup" Qualifier="SalesOrderCustomer">
					<Record Type="UI.FieldGroupType">
						<PropertyValue Property="Data">
							<Collection>
								<Record Type="UI.DataField">
									<PropertyValue Property="Value" Path="CustomerName"/>
								</Record>
								<Record Type="UI.DataField">
									<PropertyValue Property="Value" Path="CustomerID"/>
								</Record>
								<Record Type="UI.DataField">
									<PropertyValue Property="Value" Path="ToBusinessPartner/EmailAddress"/>
								</Record>
							</Collection>
						</PropertyValue>
					</Record>
				</Annotation>
				<Annotation Term="UI.HeaderInfo">
					<Record Type="UI.HeaderInfoType">
						<PropertyValue Property="TypeName" String="{@i18n>SALES_ORDER}"/>
						<PropertyValue Property="TypeNamePlural" String="{@i18n>SALES_ORDERS}"/>
						<PropertyValue Property="Title">
							<Record Type="UI.DataField">
								<PropertyValue Property="Value" Path="SalesOrderID"/>
							</Record>
						</PropertyValue>
					</Record>
				</Annotation>
				<Annotation Term="UI.SelectionFields">
					<Collection>
						<PropertyPath>SalesOrderID</PropertyPath>
						<PropertyPath>NetAmount</PropertyPath>
						<PropertyPath>CustomerName</PropertyPath>
					</Collection>
				</Annotation>
				<Annotation Term="UI.LineItem">
					<Collection>
						<Record Type="UI.DataField">
							<PropertyValue Property="Value" Path="SalesOrderID"/>
						</Record>
						<Record Type="UI.DataField">
							<PropertyValue Property="Value" Path="CustomerName"/>
						</Record>
						<Record Type="UI.DataField">
							<PropertyValue Property="Value" Path="LifecycleStatusDescription"/>
							<PropertyValue Property="Label" String="{@i18n>LIFECYCLE_STATUS}"/>
						</Record>
						<Record Type="UI.DataField">
							<PropertyValue Property="Value" Path="NetAmount"/>
						</Record>
					</Collection>
				</Annotation>
			</Annotations>
			<Annotations Target="GWSAMPLE_BASIC.SalesOrderLineItem">
				<Annotation Term="UI.LineItem">
					<Collection>
						<Record Type="UI.DataField">
							<PropertyValue Property="Value" Path="ProductID"/>
						</Record>
						<Record Type="UI.DataField">
							<PropertyValue Property="Value" Path="Quantity"/>
						</Record>
						<Record Type="UI.DataField">
							<PropertyValue Property="Value" Path="NetAmount"/>
						</Record>
						<Record Type="UI.DataField">
							<PropertyValue Property="Value" Path="DeliveryDate"/>
						</Record>
					</Collection>
				</Annotation>
			</Annotations>
		</Schema>
	</edmx:DataServices>
</edmx:Edmx>


text:
import { expect } from 'chai';
import { $$iterator, symbolIteratorPonyfill } from '../../dist/cjs/symbol/iterator';

describe('iterator symbol', () => {
  it('should exist', () => {
    expect($$iterator).to.exist;
  });
});

describe('symbolIteratorPonyfill', () => {
  describe('when root.Symbol is a function', () => {
    describe('and Symbol.iterator exists', () => {
      it('should return Symbol.iterator', () => {
        const FakeSymbol = function () { /* lol */ };
        (<any>FakeSymbol).iterator = {};
        const result = symbolIteratorPonyfill({ Symbol: FakeSymbol });
        expect(result).to.equal((<any>FakeSymbol).iterator);
      });
    });

    describe('and Symbol.iterator does not exist', () => {
      it('should use Symbol to create an return a symbol and polyfill Symbol.iterator', () => {
        const SYMBOL_RETURN = {};
        let passedDescription: string;
        const root = {
          Symbol: function (description) {
            passedDescription = description;
            return SYMBOL_RETURN;
          }
        };

        const result = symbolIteratorPonyfill(root);
        expect(result).to.equal(SYMBOL_RETURN);
        expect((<any>root.Symbol).iterator).to.equal(SYMBOL_RETURN);
      });
    });
  });

  describe('when root.Symbol is NOT a function', () => {
    describe('and root.Set exists with an @@iterator property that is a function (Mozilla bug)', () => {
      it ('should return "$$iterator"', () => {
        const result = symbolIteratorPonyfill({
          Set: function FakeSet() {
            this['@@iterator'] = function () { /* lol */ };
          }
        });
        expect(result).to.equal('@@iterator');
      });
    });

    describe('root.Set does not exit or does not have an @@iterator property', () => {
      describe('but Map exists and a random key on Map.prototype that matches Map.prototype.entries (for es6-shim)', () => {
        it('should return the key that matches the "entries" key on Map.prototype, but not return "size"', () => {
          function FakeMap() { /* lol */ }
          function fakeMethod() { /* lol */ }
          FakeMap.prototype['-omg-lol-i-can-use-whatever-I-want-YOLO-'] = fakeMethod;
          FakeMap.prototype.entries = fakeMethod;
          FakeMap.prototype.size = fakeMethod;
          const root = {
            Map: FakeMap
          };

          const result = symbolIteratorPonyfill(root);
          expect(result).to.equal('-omg-lol-i-can-use-whatever-I-want-YOLO-');
        });
      });

      describe('but Map exists and no other key except "size" on Map.prototype that matches Map.prototype.entries (for es6-shim)', () => {
        it('should return "@@iterator"', () => {
          function FakeMap() { /* lol */ }
          function fakeMethod() { /* lol */ }
          FakeMap.prototype.entries = fakeMethod;
          FakeMap.prototype.size = fakeMethod;
          const root = {
            Map: FakeMap
          };

          const result = symbolIteratorPonyfill(root);
          expect(result).to.equal('@@iterator');
        });
      });

      describe('if Set exists but has no iterator, and Map does not exist (bad polyfill maybe?)', () => {
        it('should return "@@iterator"', () => {
          const result = symbolIteratorPonyfill({
            Set: function () { /* lol */ }
          });
          expect(result).to.equal('@@iterator');
        });
      });

      describe('and root.Set and root.Map do NOT exist', () => {
        it('should return "@@iterator"', () => {
          const result = symbolIteratorPonyfill({});
          expect(result).to.equal('@@iterator');
        });
      });
    });
  });
});