text:
package org.xdi.oxauth.ws.rs;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.jboss.seam.mock.EnhancedMockHttpServletRequest;
import org.jboss.seam.mock.EnhancedMockHttpServletResponse;
import org.jboss.seam.mock.ResourceRequestEnvironment;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.xdi.oxauth.BaseTest;
import org.xdi.oxauth.client.RegisterRequest;
import org.xdi.oxauth.client.RegisterResponse;
import org.xdi.oxauth.model.common.AuthenticationMethod;
import org.xdi.oxauth.model.common.SubjectType;
import org.xdi.oxauth.model.crypto.signature.SignatureAlgorithm;
import org.xdi.oxauth.model.register.ApplicationType;
import org.xdi.oxauth.model.register.RegisterResponseParam;
import org.xdi.oxauth.model.uma.TestUtil;
import org.xdi.oxauth.model.util.StringUtils;
import org.xdi.util.Util;

import javax.ws.rs.core.MediaType;
import java.util.Arrays;
import java.util.List;

import static org.testng.Assert.*;
import static org.xdi.oxauth.model.register.RegisterRequestParam.*;
import static org.xdi.oxauth.model.register.RegisterResponseParam.*;

/**
 * Functional tests for Client Registration Web Services (embedded)
 *
 * @author Javier Rojas Blum
 * @version 0.9 March 11, 2015
 */
public class RegistrationRestWebServiceEmbeddedTest extends BaseTest {

    private String registrationAccessToken1;
    private String registrationClientUri1;

    @Parameters({"registerPath", "redirectUris"})
    @Test
    public void requestClientAssociate1(final String registerPath, final String redirectUris) throws Exception {

        new ResourceRequestEnvironment.ResourceRequest(new ResourceRequestEnvironment(this),
                ResourceRequestEnvironment.Method.POST, registerPath) {

            @Override
            protected void prepareRequest(EnhancedMockHttpServletRequest request) {
                try {
                    super.prepareRequest(request);

                    RegisterRequest registerRequest = new RegisterRequest(ApplicationType.WEB, "oxAuth test app",
                            StringUtils.spaceSeparatedToList(redirectUris));

                    request.setContentType(MediaType.APPLICATION_JSON);
                    String registerRequestContent = registerRequest.getJSONParameters().toString(4);
                    request.setContent(registerRequestContent.getBytes());
                } catch (JSONException e) {
                    e.printStackTrace();
                    fail(e.getMessage());
                }
            }

            @Override
            protected void onResponse(EnhancedMockHttpServletResponse response) {
                super.onResponse(response);
                showResponse("requestClientAssociate1", response);

                assertEquals(response.getStatus(), 200, "Unexpected response code. " + response.getContentAsString());
                assertNotNull(response.getContentAsString(), "Unexpected result: " + response.getContentAsString());
                try {
                    final RegisterResponse registerResponse = RegisterResponse.valueOf(response.getContentAsString());
                    ClientTestUtil.assert_(registerResponse);

                    registrationAccessToken1 = registerResponse.getRegistrationAccessToken();
                    registrationClientUri1 = registerResponse.getRegistrationClientUri();
                } catch (Exception e) {
                    e.printStackTrace();
                    fail(e.getMessage() + "\nResponse was: " + response.getContentAsString());
                }
            }
        }.run();
    }

    @Parameters({"registerPath", "redirectUris", "sectorIdentifierUri", "contactEmail1", "contactEmail2"})
    @Test
    public void requestClientAssociate2(final String registerPath, final String redirectUris,
                                        final String sectorIdentifierUri, final String contactEmail1, final String contactEmail2) throws Exception {

        new ResourceRequestEnvironment.ResourceRequest(new ResourceRequestEnvironment(this),
                ResourceRequestEnvironment.Method.POST, registerPath) {

            @Override
            protected void prepareRequest(EnhancedMockHttpServletRequest request) {
                try {
                    super.prepareRequest(request);

                    List<String> contacts = Arrays.asList(contactEmail1, contactEmail2);

                    RegisterRequest registerRequest = new RegisterRequest(ApplicationType.WEB, "oxAuth test app",
                            StringUtils.spaceSeparatedToList(redirectUris));
                    registerRequest.setContacts(contacts);
                    registerRequest.setScopes(Arrays.asList("openid", "clientinfo", "profile", "email", "invalid_scope"));
                    registerRequest.setLogoUri("http://www.gluu.org/wp-content/themes/gluursn/images/logo.png");
                    registerRequest.setClientUri("http://www.gluu.org/company/team");
                    registerRequest.setTokenEndpointAuthMethod(AuthenticationMethod.CLIENT_SECRET_JWT);
                    registerRequest.setPolicyUri("http://www.gluu.org/policy");
                    registerRequest.setJwksUri("http://www.gluu.org/jwks");
                    registerRequest.setSectorIdentifierUri(sectorIdentifierUri);
                    registerRequest.setSubjectType(SubjectType.PAIRWISE);
                    registerRequest.setRequestObjectSigningAlg(SignatureAlgorithm.RS256);

                    request.setContentType(MediaType.APPLICATION_JSON);
                    String registerRequestContent = registerRequest.getJSONParameters().toString(4);
                    request.setContent(registerRequestContent.getBytes());
                } catch (JSONException e) {
                    e.printStackTrace();
                    fail(e.getMessage());
                }
            }

            @Override
            protected void onResponse(EnhancedMockHttpServletResponse response) {
                super.onResponse(response);
                showResponse("requestClientAssociate2", response);

                assertEquals(response.getStatus(), 200, "Unexpected response code. " + response.getContentAsString());
                assertNotNull(response.getContentAsString(), "Unexpected result: " + response.getContentAsString());
                try {
                    final RegisterResponse registerResponse = RegisterResponse.valueOf(response.getContentAsString());
                    ClientTestUtil.assert_(registerResponse);

                    JSONObject jsonObj = new JSONObject(response.getContentAsString());

                    // Registered Metadata
                    assertTrue(jsonObj.has(CLIENT_URI.toString()));
                    assertTrue(jsonObj.has(SCOPES.toString()));

                    JSONArray scopesJsonArray = jsonObj.getJSONArray(SCOPES.toString());
                    assertEquals(scopesJsonArray.getString(0), "openid");
                    assertEquals(scopesJsonArray.getString(1), "clientinfo");
                    assertEquals(scopesJsonArray.getString(2), "profile");
                    assertEquals(scopesJsonArray.getString(3), "email");
                } catch (JSONException e) {
                    e.printStackTrace();
                    fail(e.getMessage() + "\nResponse was: " + response.getContentAsString());
                }
            }
        }.run();
    }

    @Parameters({"registerPath"})
    @Test(dependsOnMethods = "requestClientAssociate1")
    public void requestClientRead(final String registerPath) throws Exception {

        new ResourceRequestEnvironment.ResourceRequest(new ResourceRequestEnvironment(this),
                ResourceRequestEnvironment.Method.GET, registerPath) {

            @Override
            protected void prepareRequest(EnhancedMockHttpServletRequest request) {
                super.prepareRequest(request);

                request.addHeader("Authorization", "Bearer " + registrationAccessToken1);
                request.setContentType(MediaType.APPLICATION_JSON);
                request.setQueryString(registrationClientUri1.substring(registrationClientUri1.indexOf("?") + 1));
            }

            @Override
            protected void onResponse(EnhancedMockHttpServletResponse response) {
                super.onResponse(response);
                showResponse("requestClientRead", response);
                readResponseAssert(response);
            }
        }.run();
    }

    public static void readResponseAssert(EnhancedMockHttpServletResponse response) {
        assertEquals(response.getStatus(), 200, "Unexpected response code. " + response.getContentAsString());
        assertNotNull(response.getContentAsString(), "Unexpected result: " + response.getContentAsString());
        try {
            JSONObject jsonObj = new JSONObject(response.getContentAsString());
            assertTrue(jsonObj.has(RegisterResponseParam.CLIENT_ID.toString()));
            assertTrue(jsonObj.has(CLIENT_SECRET.toString()));
            assertTrue(jsonObj.has(CLIENT_ID_ISSUED_AT.toString()));
            assertTrue(jsonObj.has(CLIENT_SECRET_EXPIRES_AT.toString()));

            // Registered Metadata
            assertTrue(jsonObj.has(REDIRECT_URIS.toString()));
            assertTrue(jsonObj.has(APPLICATION_TYPE.toString()));
            assertTrue(jsonObj.has(CLIENT_NAME.toString()));
            assertTrue(jsonObj.has(ID_TOKEN_SIGNED_RESPONSE_ALG.toString()));
            assertTrue(jsonObj.has("scopes"));
        } catch (JSONException e) {
            e.printStackTrace();
            fail(e.getMessage() + "\nResponse was: " + response.getContentAsString());
        }
    }

    @Parameters({"registerPath", "redirectUris", "contactEmail1", "contactEmail2"})
    @Test(dependsOnMethods = "requestClientAssociate1")
    public void requestClientUpdate(final String registerPath, final String redirectUris, final String contactEmail1, final String contactEmail2) throws Exception {
        final String contactEmailNewValue = contactEmail2;
        final String logoUriNewValue = "http://www.gluu.org/test/yuriy/logo.png";
        final String clientUriNewValue = "http://www.gluu.org/company/yuriy";
        new ResourceRequestEnvironment.ResourceRequest(new ResourceRequestEnvironment(this),
                ResourceRequestEnvironment.Method.PUT, registerPath) {

            @Override
            protected void prepareRequest(EnhancedMockHttpServletRequest request) {
                super.prepareRequest(request);
                try {

                    final RegisterRequest registerRequest = new RegisterRequest(ApplicationType.WEB, "oxAuth test app",
                            StringUtils.spaceSeparatedToList(redirectUris));

                    registerRequest.setContacts(Arrays.asList(contactEmail1, contactEmailNewValue));
                    registerRequest.setLogoUri(logoUriNewValue);
                    registerRequest.setClientUri(clientUriNewValue);

                    final String registerRequestContent = registerRequest.getJSONParameters().toString(4);
                    request.addHeader("Authorization", "Bearer " + registrationAccessToken1);
                    request.setContentType(MediaType.APPLICATION_JSON);
                    request.setQueryString(registrationClientUri1.substring(registrationClientUri1.indexOf("?") + 1));

                    request.setContent(registerRequestContent.getBytes(Util.UTF8));
                } catch (Exception e) {
                    e.printStackTrace();
                    fail();
                }
            }

            @Override
            protected void onResponse(EnhancedMockHttpServletResponse response) {
                super.onResponse(response);
                showResponse("requestClientRead", response);

                readResponseAssert(response);

                try {
                    // check whether values are really updated
                    RegisterRequest r = RegisterRequest.fromJson(response.getContentAsString());
                    assertTrue(r.getContacts() != null && r.getContacts().contains(contactEmailNewValue));
                    assertTrue(r.getClientUri().equals(clientUriNewValue));
                    assertTrue(r.getLogoUri().equals(logoUriNewValue));
                } catch (JSONException e) {
                    e.printStackTrace();
                    fail(e.getMessage() + "\nResponse was: " + response.getContentAsString());
                }
            }
        }.run();
    }

    @Parameters({"registerPath"})
    @Test
    public void requestClientRegistrationFail1(final String registerPath) throws Exception {

        new ResourceRequestEnvironment.ResourceRequest(new ResourceRequestEnvironment(this),
                ResourceRequestEnvironment.Method.POST, registerPath) {

            @Override
            protected void prepareRequest(EnhancedMockHttpServletRequest request) {
                try {
                    super.prepareRequest(request);

                    RegisterRequest registerRequest = new RegisterRequest(null, null, null);

                    request.setContentType(MediaType.APPLICATION_JSON);
                    String registerRequestContent = registerRequest.getJSONParameters().toString(4);
                    request.setContent(registerRequestContent.getBytes());
                } catch (JSONException e) {
                    e.printStackTrace();
                    fail(e.getMessage());
                }
            }

            @Override
            protected void onResponse(EnhancedMockHttpServletResponse response) {
                super.onResponse(response);
                showResponse("requestClientRegistrationFail 1", response);

                assertEquals(response.getStatus(), 400, "Unexpected response code. " + response.getContentAsString());
                TestUtil.assertErrorResponse(response.getContentAsString());
            }
        }.run();
    }

    @Parameters({"registerPath"})
    @Test
    public void requestClientRegistrationFail2(final String registerPath) throws Exception {

        new ResourceRequestEnvironment.ResourceRequest(new ResourceRequestEnvironment(this),
                ResourceRequestEnvironment.Method.POST, registerPath) {

            @Override
            protected void prepareRequest(EnhancedMockHttpServletRequest request) {
                try {
                    super.prepareRequest(request);

                    RegisterRequest registerRequest = new RegisterRequest(ApplicationType.WEB, "oxAuth test app", null); // Missing redirect URIs

                    request.setContentType(MediaType.APPLICATION_JSON);
                    String registerRequestContent = registerRequest.getJSONParameters().toString(4);
                    request.setContent(registerRequestContent.getBytes());
                } catch (JSONException e) {
                    e.printStackTrace();
                    fail(e.getMessage());
                }
            }

            @Override
            protected void onResponse(EnhancedMockHttpServletResponse response) {
                super.onResponse(response);
                showResponse("requestClientRegistrationFail 2", response);

                assertEquals(response.getStatus(), 400, "Unexpected response code. " + response.getContentAsString());
                TestUtil.assertErrorResponse(response.getContentAsString());
            }
        }.run();
    }

    @Parameters({"registerPath"})
    @Test
    public void requestClientRegistrationFail3(final String registerPath) throws Exception {

        new ResourceRequestEnvironment.ResourceRequest(new ResourceRequestEnvironment(this),
                ResourceRequestEnvironment.Method.POST, registerPath) {

            @Override
            protected void prepareRequest(EnhancedMockHttpServletRequest request) {
                try {
                    super.prepareRequest(request);

                    RegisterRequest registerRequest = new RegisterRequest(ApplicationType.WEB, "oxAuth test app",
                            Arrays.asList("https://client.example.com/cb#fail_fragment"));

                    request.setContentType(MediaType.APPLICATION_JSON);
                    String registerRequestContent = registerRequest.getJSONParameters().toString(4);
                    request.setContent(registerRequestContent.getBytes());
                } catch (JSONException e) {
                    e.printStackTrace();
                    fail(e.getMessage());
                }
            }

            @Override
            protected void onResponse(EnhancedMockHttpServletResponse response) {
                super.onResponse(response);
                showResponse("requestClientRegistrationFail3", response);

                assertEquals(response.getStatus(), 400, "Unexpected response code. " + response.getContentAsString());
                TestUtil.assertErrorResponse(response.getContentAsString());
            }
        }.run();
    }
}


text:
const _ = require('lodash')
const { readFileSync } = require('fs')
const path = require('path')

class argv {
  constructor (config) {
    this.options = config.options || {}
    this.jsonParsedOpts = config.jsonParsedOpts ||
      [
        'searchBody',
        'input-headers',
        'headers',
        'output-headers',
        'input-params',
        'params',
        'output-params',
        's3Options',
        's3Configs'
      ]
    this.parseJSONOpts = config.parseJSONOpts
  }

  parse (argv, defaults, parseJSONOpts = false) {
    // parse passed options & use defaults otherwise
    for (const i in defaults) {
      this.options[i] = argv[i] || defaults[i]

      if (this.options[i] === 'true') { this.options[i] = true }
      if (this.options[i] === 'false') { this.options[i] = false }
      if (this.options[i] === 'Infinity') { this.options[i] = Infinity }
      if (this.options[i] === 'null') { this.options[i] = null }
      if (i === 'interval' && _.isNumber(argv[i])) {
        // special case to handle value == 0
        this.options[i] = argv[i]
      }
    }

    if (parseJSONOpts || this.parseJSONOpts) {
      // parse whitelisted json formatted options
      for (const i of this.jsonParsedOpts) {
        if (/^(searchBody|s3Options|s3Configs)$/.test(i) && _.startsWith(this.options[i], '@')) {
          const resolvedFilePath = path.resolve(process.cwd(), this.options[i].substring(1))
          this.options[i] = readFileSync(resolvedFilePath).toString()
        }
        if (this.options[i]) { this.options[i] = JSON.parse(this.options[i]) }
      }
    }
  }

  log (type, message) {
    if (type === 'debug') {
      if (this.options.debug === true) {
        message = `${new Date().toUTCString()} [debug] | ${message}`
        console.log(message)
      } else {
        return false
      }
    } else if (type === 'error') {
      message = `${new Date().toUTCString()} | ${message}`
      console.error(message)
    } else if (this.options.quiet === false) {
      message = `${new Date().toUTCString()} | ${message}`
      console.log(message)
    } else {
      return false
    }
  }
}

module.exports = argv

redpajama_set_name: RedPajamaGithub


text:
| Azure Pipelines | GitHub Actions | Documentation | PS Gallery | License |
|-----------------|----------------|---------------|------------|---------|
[![Azure Pipelines Build Status][azure-pipeline-badge]][azure-pipeline-build] | [![GitHub Actions Status][github-actions-badge]][github-actions-build] | [![Documentation Status][docs-badge]][docs] | [![PowerShell Gallery][psgallery-badge]][psgallery] | [![License][license-badge]][license]

---

Supporting PoshBot

PoshBot is a MIT-licensed open source project. Ongoing development is made possible thanks to the support of sponsors.
If you'd like to become a sponsor, you can do so through [GitHub Sponsors](https://github.com/users/devblackops/sponsorship) or [Patreon](https://www.patreon.com/bePatron?u=10352866).

Silver Sponsors

---

Want some in-depth guides? Check out [ChatOps the Easy Way](https://leanpub.com/chatops-the-easy-way) on [Leanpub](https://leanpub.com/)!

# Introduction

PoshBot is a chat bot written in [PowerShell](https://msdn.microsoft.com/powershell).
It makes extensive use of classes introduced in PowerShell 5.0.
PowerShell modules are loaded into PoshBot and instantly become available as bot commands.
PoshBot currently supports connecting to Slack to provide you with awesome ChatOps goodness.

## What Can PoshBot Do?

Pretty much anything you want :) No seriously.
PoshBot executes functions or cmdlets from PowerShell modules.
Use PoshBot to connect to servers and report status, deploy code, execute runbooks, query APIs, etc.
If you can write it in PowerShell, PoshBot can execute it.

## Documentation

Detailed documentation can be found at [ReadTheDocs](http://poshbot.readthedocs.io/en/latest/).

## Building PoshBot

See [Building PoshBot](./building.md) for documentation on how to build PoshBot from source.

## Changelog

Detailed changes for each release are documented in the [release notes](https://github.com/poshbotio/poshbot/releases).

## [YouTube] PowerShell Summit 2018 - Invoke-ChatOps: Level up and change your culture with chat and PowerShell

[![Alt text](https://i.ytimg.com/vi/Mrs49IdnSHc/hqdefault.jpg?sqp=-oaymwEZCNACELwBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLDdWnuXllwxfDRO_LGa0h_h_VGPPQ)](https://youtu.be/Mrs49IdnSHc)

## Quickstart

To get started now, get a SLACK-API-TOKEN for your bot:

[https://my.slack.com/services/new/bot](https://my.slack.com/services/new/bot)

```powershell
# Install the module from PSGallery
Install-Module -Name PoshBot -Repository PSGallery

# Import the module
Import-Module -Name PoshBot

# Create a bot configuration
$botParams = @{
    Name = 'name'
    BotAdmins = @('<SLACK-CHAT-HANDLE>')
    CommandPrefix = '!'
    LogLevel = 'Info'
    BackendConfiguration = @{
        Name = 'SlackBackend'
        Token = '<SLACK-API-TOKEN>'
    }
    AlternateCommandPrefixes = 'bender', 'hal'
}

$myBotConfig = New-PoshBotConfiguration @botParams

# Start a new instance of PoshBot interactively or in a job.
Start-PoshBot -Configuration $myBotConfig #-AsJob
```

Basic usage:

```powershell
# Create a Slack backend
$backendConfig = @{Name = 'SlackBackend'; Token = '<SLACK-API-TOKEN>'}
$backend = New-PoshBotSlackBackend -Configuration $backendConfig

# Create a PoshBot configuration
$pbc = New-PoshBotConfiguration -BotAdmins @('<MY-SLACK-HANDLE>') -BackendConfiguration $backendConfig

# Save configuration
Save-PoshBotConfiguration -InputObject $pbc -Path .\PoshBotConfig.psd1

# Load configuration
$pbc = Get-PoshBotConfiguration -Path .\PoshBotConfig.psd1

# Create an instance of the bot
$bot = New-PoshBotInstance -Configuration $pbc -Backend $backend

# Start the bot
$bot.Start()

# Available commands
Get-Command -Module PoshBot

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-PoshBot                                        0.12.0     poshbot
Function        Get-PoshBotConfiguration                           0.12.0     poshbot
Function        Get-PoshBotStatefulData                            0.12.0     poshbot
Function        New-PoshBotCardResponse                            0.12.0     poshbot
Function        New-PoshBotConfiguration                           0.12.0     poshbot
Function        New-PoshBotDiscordBackend                          0.12.0     poshbot
Function        New-PoshBotFileUpload                              0.12.0     poshbot
Function        New-PoshBotInstance                                0.12.0     poshbot
Function        New-PoshBotMiddlewareHook                          0.12.0     poshbot
Function        New-PoshBotScheduledTask                           0.12.0     poshbot
Function        New-PoshBotSlackBackend                            0.12.0     poshbot
Function        New-PoshBotTeamsBackend                            0.12.0     poshbot
Function        New-PoshBotTextResponse                            0.12.0     poshbot
Function        Remove-PoshBotStatefulData                         0.12.0     poshbot
Function        Save-PoshBotConfiguration                          0.12.0     poshbot
Function        Set-PoshBotStatefulData                            0.12.0     poshbot
Function        Start-PoshBot                                      0.12.0     poshbot
Function        Stop-Poshbot                                       0.12.0     poshbot
```

[azure-pipeline-badge]: https://dev.azure.com/devblackops/PoshBot/_apis/build/status/PoshBot-CI
[azure-pipeline-build]: https://dev.azure.com/devblackops/PoshBot/_build/latest?definitionId=3
[github-actions-badge]: https://github.com/poshbotio/PoshBot/workflows/CI/badge.svg
[github-actions-build]: https://github.com/poshbotio/PoshBot/actions
[docs-badge]: https://readthedocs.org/projects/poshbot/badge/?version=latest
[docs]: http://poshbot.readthedocs.io/en/latest/
[psgallery-badge]: https://img.shields.io/powershellgallery/dt/poshbot.svg
[psgallery]: https://www.powershellgallery.com/packages/poshbot
[license-badge]: https://img.shields.io/github/license/poshbotio/poshbot.svg
[license]: https://www.powershellgallery.com/packages/poshbot


text:
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.autofill;

import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.ui.WindowAndroid;

/**
 * Java-side AutofillDialog and AutofillDialogFactory interfaces, and
 * JNI glue for C++ AutofillDialogControllerAndroid.
 */
@JNINamespace("autofill")
public class AutofillDialogControllerAndroid {
    private static AutofillDialogFactory mDialogFactory;

    private int mNativeDelegate;  // could be 0 after onDestroy().
    private AutofillDialog mDialog;

    /**
     * An interface to the two possible continuations for the dialog.
     * The dialog is expected to be dismissed when either of the calls is made.
     */
    public interface AutofillDialogDelegate {
        /**
         * Cancels the requestAutocomplete.
         */
        void dialogCancel();

        /**
         * Submits the data to the web-page and persists the last account/card/address choices.
         * @param fullWallet Resulting billing/shipping information obtained from the user
         * @param lastUsedChoiceIsAutofill Whether the last selected data source is Autofill
         * @param lastUsedAccountName The last selected account name, or null
         * @param guidLastUsedBilling GUID of the last selected Autofill billing address, or null
         * @param guidLastUsedShipping GUID of the last selected Autofill shipping address, or null
         * @param guidLastUsedCard GUID of the last selected Autofill credit card, or null
         */
        void dialogContinue(
                AutofillDialogResult.ResultWallet fullWallet,
                boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
                String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
    }

    /**
     * An interface that exposes the necessary functionality for an Autofill dialog.
     * Note that all information necessary to construct the dialog is passed to the factory.
     */
    public interface AutofillDialog {
        /**
         * Notifies the dialog that the C++ side is gone.
         * The dialog needs to clear its reference to the no longer valid AutofillDialogDelegate.
         */
        void onDestroy();
    }

    /**
     * An interface to the factory that creates Autofill dialogs.
     */
    public interface AutofillDialogFactory {
        /**
         * Creates the dialog.
         * Reasonable attempts should be made to respect "initial choices",
         * Initial choices don't have to be self-consistent or valid.
         *
         * @param delegate Continuations for the dialog
         * @param windowAndroid Context in which the dialog should be shown
         * @param requestFullBillingAddress Whether the full billing address is required
         * @param requestShippingAddress Whether the shipping address is required
         * @param requestPhoneNumbers Whether the phone numbers are required in addresses
         * @param incognitoMode True if the dialog started from an incognito tab
         * @param initialChoiceIsAutofill Whether the selected data source should be Autofill
         * @param initialAccountName Account to be selected, or null
         * @param initialBillingGuid GUID of the initial billing address selection in Autofill
         * @param initialShippingGuid GUID of the initial shipping address selection in Autofill
         * @param initialCardGuid GUID of the initial credit card selection in Autofill
         * @param merchantDomain Scheme+origin for the originating web page, or null
         * @return The Autofill dialog that would later call into the delegate, or null
         */
        AutofillDialog createDialog(
                final AutofillDialogDelegate delegate,
                final WindowAndroid windowAndroid,
                final boolean requestFullBillingAddress, final boolean requestShippingAddress,
                final boolean requestPhoneNumbers,
                final boolean incognitoMode,
                final boolean initialChoiceIsAutofill, final String initialAccountName,
                final String initialBillingGuid, final String initialShippingGuid,
                final String initialCardGuid,
                final String merchantDomain);
    }

    /**
     * Sets the factory to be used.
     * @param factory An instance of the AutofillDialogFactory that will handle requests.
     */
    public static void setDialogFactory(AutofillDialogFactory factory) {
        mDialogFactory = factory;
    }

    private AutofillDialogControllerAndroid(
            final int nativeAutofillDialogControllerAndroid,
            final WindowAndroid windowAndroid,
            final boolean requestFullBillingAddress, final boolean requestShippingAddress,
            final boolean requestPhoneNumbers,
            final boolean incognitoMode,
            final boolean initialChoiceIsAutofill, final String initialWalletAccountName,
            final String initialBillingGuid, final String initialShippingGuid,
            final String initialCardGuid,
            final String merchantDomain) {
        mNativeDelegate = nativeAutofillDialogControllerAndroid;

        if (mDialogFactory == null) {
            nativeDialogCancel(mNativeDelegate);
            return;
        }

        AutofillDialogDelegate delegate = new AutofillDialogDelegate() {
            @Override
            public void dialogCancel() {
                nativeDialogCancel(mNativeDelegate);
            }

            @Override
            public void dialogContinue(
                    AutofillDialogResult.ResultWallet fullWallet,
                    boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
                    String guidLastUsedBilling, String guidLastUsedShipping,
                    String guidLastUsedCard) {
                nativeDialogContinue(mNativeDelegate, fullWallet,
                        lastUsedChoiceIsAutofill, lastUsedAccountName,
                        guidLastUsedBilling, guidLastUsedShipping, guidLastUsedCard);
            }
        };

        mDialog = mDialogFactory.createDialog(
                delegate,
                windowAndroid,
                requestFullBillingAddress, requestShippingAddress,
                requestPhoneNumbers,
                incognitoMode,
                initialChoiceIsAutofill, initialWalletAccountName,
                initialBillingGuid, initialShippingGuid, initialCardGuid,
                merchantDomain);
        if (mDialog == null) {
            nativeDialogCancel(mNativeDelegate);
            return;
        }
    }

    @CalledByNative
    private static AutofillDialogControllerAndroid create(
            final int nativeAutofillDialogControllerAndroid,
            final WindowAndroid windowAndroid,
            final boolean requestFullBillingAddress, final boolean requestShippingAddress,
            final boolean requestPhoneNumbers,
            final boolean incognitoMode,
            final boolean initialChoiceIsAutofill, final String initialWalletAccountName,
            final String initialBillingGuid, final String initialShippingGuid,
            final String initialCreditCardGuid,
            final String merchantDomain) {
        return new AutofillDialogControllerAndroid(
                nativeAutofillDialogControllerAndroid, windowAndroid,
                requestFullBillingAddress, requestShippingAddress, requestPhoneNumbers,
                incognitoMode,
                initialChoiceIsAutofill, initialWalletAccountName,
                initialBillingGuid, initialShippingGuid,
                initialCreditCardGuid,
                merchantDomain);
    }

    @CalledByNative
    private void onDestroy() {
        if (mNativeDelegate == 0) return;

        if (mDialog != null) mDialog.onDestroy();

        mDialog = null;
        mNativeDelegate = 0;
    }

    // Calls from Java to C++ AutofillDialogControllerAndroid:

    private native void nativeDialogCancel(int nativeAutofillDialogControllerAndroid);
    private native void nativeDialogContinue(int nativeAutofillDialogControllerAndroid,
            Object fullWallet,
            boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
            String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
}


text:
#ifndef FUSION_VECTOR_11052014_1625
#define FUSION_VECTOR_11052014_1625

#include <boost/config.hpp>
#include <boost/fusion/support/config.hpp>
#include <boost/fusion/container/vector/vector_fwd.hpp>

///////////////////////////////////////////////////////////////////////////////
// Without variadics, we will use the PP version
///////////////////////////////////////////////////////////////////////////////
# include <boost/fusion/container/vector/detail/cpp03/vector.hpp>

#endif


text:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/selection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


text:
using EFCoreExtend;
using EFCoreExtend.Commons;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace EFCoreExtend.Test
{
    public class TestSqlConfigSection
    {
        static DbContext db = new MSSqlDBContext();
        //static DbContext db = new SqlieteDBContext();
        //static DbContext db = new MysqlDBContext();
        //static DbContext db = new PostgreSqlDBContext();

        PersonSectionBLL p = new PersonSectionBLL(db);

        static TestSqlConfigSection()
        {
            EFHelper.Services.SqlConfigMgr.Config.LoadDirectory(Directory.GetCurrentDirectory() + "/Datas/Section");
            //sql日志记录策略执行器
            EFHelper.Services.SqlConfigMgr.PolicyMgr.SetSqlConfigExecuteLogPolicyExecutor(
                (tableName, sqlName, sql, sqlparam) =>
                {
                    Assert.NotEmpty(tableName);
                    Assert.NotEmpty(sqlName);
                    Assert.NotEmpty(sql);
                    Console.WriteLine(sql + ";      params:" + (sqlparam?.Count > 0 ? sqlparam.JoinToString(",", l => l.ParameterName  + "="+ l.Value) : "") + "\r\n");
                }, true, false);
        }

        [Fact]
        public void Test()
        {
            Assert.True(p.AddPersonSection() > 0);
            Assert.True(p.GetListSection()?.Count > 0);
            Assert.True(p.UpdatePersonSection() > 0);
            Assert.True(p.DeletePersonSection() > 0);
        }

    }
}


text:
package com.thoughtworks.go.util.pool;

import org.apache.commons.codec.digest.MessageDigestAlgorithms;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestObjectPools {

    public static final String SHA_256 = MessageDigestAlgorithms.SHA_256;
    public static final String MD5 = MessageDigestAlgorithms.MD5;
    public static final String SHA_512_256 = MessageDigestAlgorithms.SHA_512_256;
    private static final ThreadLocal<MessageDigest> sha256DigestLocal = new ThreadLocal<>();
    private static final ThreadLocal<MessageDigest> sha512DigestLocal = new ThreadLocal<>();
    private static final ThreadLocal<MessageDigest> md5DigestLocal = new ThreadLocal<>();
    private final CreateDigest createDigest;

    public DigestObjectPools() {
        this(new SimpleCreateDigest());
    }

    public DigestObjectPools(CreateDigest createDigest) {
        this.createDigest = createDigest;
    }

    public String computeDigest(String algorithm, DigestOperation operation) {
        if (!SHA_512_256.equals(algorithm) && !SHA_256.equals(algorithm) && !MD5.equals(algorithm)) {
            throw new IllegalArgumentException("Algorithm not supported");
        }
        try {
            MessageDigest digest = getDigest(algorithm);
            String result = operation.perform(digest);
            digest.reset(); //test passes even without this, but can't see sun impl's source, so playing safe
            return result;
        } catch (Exception e) {
            throw new RuntimeException("Failed to compute the digest.", e);
        }
    }

    private MessageDigest getDigest(String algorithm) throws Exception {
        ThreadLocal<MessageDigest> messageDigestThreadLocal = get(algorithm);
        MessageDigest digest = messageDigestThreadLocal.get();
        if (digest == null) {
            digest = createDigest.create(algorithm);
            messageDigestThreadLocal.set(digest);
        }
        return digest;
    }

    private ThreadLocal<MessageDigest> get(String algorithm) {
        switch (algorithm) {
            case SHA_512_256:
                return sha512DigestLocal;
            case SHA_256:
                return sha256DigestLocal;
            case MD5:
                return md5DigestLocal;
            default:
                throw new IllegalArgumentException("Algorithm not supported");
        }
    }

    public interface DigestOperation {
        String perform(MessageDigest digest) throws IOException;
    }

    public interface CreateDigest {
        MessageDigest create(String algorithm) throws NoSuchAlgorithmException;
    }

    private static class SimpleCreateDigest implements CreateDigest {

        @Override
        public MessageDigest create(String algorithm) throws NoSuchAlgorithmException {
            return MessageDigest.getInstance(algorithm);
        }
    }

    /**
     * @deprecated Used only in tests
     */
    void clearThreadLocals() {
        sha256DigestLocal.set(null);
        sha512DigestLocal.set(null);
        md5DigestLocal.set(null);
    }
}


text:
package git4idea.update;

import javax.annotation.Nonnull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import static com.intellij.openapi.util.text.StringUtil.join;
import static com.intellij.openapi.util.text.StringUtil.pluralize;

/**
 * @author Kirill Likhodedov
 */
public final class GitFetchResult {

  private final Type myType;
  private Collection<Exception> myErrors = new ArrayList<Exception>();
  private Collection<String> myPrunedRefs = new ArrayList<String>();

  public enum Type {
    SUCCESS,
    CANCELLED,
    NOT_AUTHORIZED,
    ERROR
  }

  public GitFetchResult(@Nonnull Type type) {
    myType = type;
  }

  @Nonnull
  public static GitFetchResult success() {
    return new GitFetchResult(Type.SUCCESS);
  }

  @Nonnull
  public static GitFetchResult cancel() {
    return new GitFetchResult(Type.CANCELLED);
  }

  @Nonnull
  public static GitFetchResult error(Collection<Exception> errors) {
    GitFetchResult result = new GitFetchResult(Type.ERROR);
    result.myErrors = errors;
    return result;
  }

  @Nonnull
  public static GitFetchResult error(Exception error) {
    return error(Collections.singletonList(error));
  }

  @Nonnull
  public static GitFetchResult error(@Nonnull String errorMessage) {
    return error(new Exception(errorMessage));
  }

  public boolean isSuccess() {
    return myType == Type.SUCCESS;
  }

  public boolean isCancelled() {
    return myType == Type.CANCELLED;
  }

  public boolean isNotAuthorized() {
    return myType == Type.NOT_AUTHORIZED;
  }

  public boolean isError() {
    return myType == Type.ERROR;
  }

  @Nonnull
  public Collection<? extends Exception> getErrors() {
    return myErrors;
  }

  public void addPruneInfo(@Nonnull Collection<String> prunedRefs) {
    myPrunedRefs.addAll(prunedRefs);
  }

  @Nonnull
  public Collection<String> getPrunedRefs() {
    return myPrunedRefs;
  }

  @Nonnull
  public String getAdditionalInfo() {
    if (!myPrunedRefs.isEmpty()) {
      return "Pruned obsolete remote " + pluralize("reference", myPrunedRefs.size()) + ": " + join(myPrunedRefs, ", ");
    }
    return "";
  }

}


text:
using System.Collections.Generic;

namespace DynThings.WebAPIClient.Models
{
    public class APIThing
    {
        #region :: Public Properties ::

        public long ID { get; set; }

        public string Title { get; set; }

        public int UTC_Diff { get; set; }

        #region Master Types
        public APIThingsType ThingsType { get; set; }

        #endregion

        #region Parents
        public List<APILocation> Locations { get; set; }

        #endregion

        #region Childs

        public List<APIThingEnd> ThingEnds { get; set; }
        public int ThingEndsCount { get; set; }

        #endregion

        #endregion

        #region :: Constructor ::
        public APIThing()
        {
            this.ID = 0;
            this.Title = "";
            this.ThingsType = new APIThingsType();
            this.UTC_Diff = 0;

            //this.EndPoints = new List<APIEndPoint>();
        }
        #endregion
    }

}


text:
package jp.bizreach.twitter.app.dto;

import java.io.Serializable;

import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;

@Component(instance = InstanceType.SESSION)
public class MemberDto implements Serializable {

    private static final long serialVersionUID = 1L;
    public Integer memberId = null;
    public String memberName = null;
    public String memberAccount = null;

    public boolean isLogin() {
        if (memberId == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public String toString() {
        return "{" + memberId + "}";
    }
}


text:
module Spec
  module Matchers
    class Matcher
      include Spec::Matchers::InstanceExec
      include Spec::Matchers::Pretty
      include Spec::Matchers

      attr_reader :expected, :actual

      def initialize(name, *expected, &declarations)
        @name     = name
        @expected = expected
        @actual   = nil
        @diffable = false
        @error_to_watch_for = nil
        @messages = {
          :description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
          :failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
          :failure_message_for_should_not => lambda {|actual| "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"}
        }
        making_declared_methods_public do
          instance_exec(*@expected, &declarations)
        end
      end

      def matches?(actual)
        if @error_to_watch_for
          begin
            instance_exec(@actual = actual, &@match_block)
            true
          rescue @error_to_watch_for
            false
          end
        else
          instance_exec(@actual = actual, &@match_block)
        end
      rescue Spec::Expectations::ExpectationNotMetError
        false
      end

      def description(&block)
        cache_or_call_cached(:description, &block)
      end

      def failure_message_for_should(&block)
        cache_or_call_cached(:failure_message_for_should, actual, &block)
      end

      def failure_message_for_should_not(&block)
        cache_or_call_cached(:failure_message_for_should_not, actual, &block)
      end

      def match(&block)
        @match_block = block
      end

      def match_unless_raises(error=StandardError, &block)
        @error_to_watch_for = error
        match(&block)
      end

      def diffable?
        @diffable
      end

      def diffable
        @diffable = true
      end

    private

      def making_declared_methods_public # :nodoc:
        # Our home-grown instance_exec in ruby 1.8.6 results in any methods
        # declared in the block eval'd by instance_exec in the block to which we
        # are yielding here are scoped private. This is NOT the case for Ruby
        # 1.8.7 or 1.9.
        #
        # Also, due some crazy scoping that I don't understand, these methods
        # are actually available in the specs (something about the matcher being
        # defined in the scope of Spec::Matchers or within an example), so not
        # doing the following will not cause specs to fail, but they *will*
        # cause features to fail and that will make users unhappy. So don't.
        orig_private_methods = private_methods
        yield
        st = (class << self; self; end)
        (private_methods - orig_private_methods).each {|m| st.__send__ :public, m}
      end

      def cache_or_call_cached(key, actual=nil, &block)
        block ? @messages[key] = block : @messages[key].call(actual)
      end

      def name_to_sentence
        split_words(@name)
      end

      def expected_to_sentence
        to_sentence(@expected)
      end

    end
  end
end


text:
import numpy
import kernel_tuner
from collections import OrderedDict

def tune():

    with open('stencil.cu', 'r') as f:
        kernel_string = f.read()

    problem_size = (4096, 2048)
    size = numpy.prod(problem_size)

    x_old = numpy.random.randn(size).astype(numpy.float32)
    x_new = numpy.copy(x_old)
    args = [x_new, x_old]

    tune_params = OrderedDict()
    tune_params["block_size_x"] = [32*i for i in range(1,9)]
    tune_params["block_size_y"] = [2**i for i in range(6)]

    grid_div_x = ["block_size_x"]
    grid_div_y = ["block_size_y"]

    return kernel_tuner.tune_kernel("stencil_kernel", kernel_string, problem_size,
        args, tune_params, grid_div_x=grid_div_x, grid_div_y=grid_div_y,
        verbose = True)

if __name__ == "__main__":
    tune()


text:
  Craintilleux est
      une ville située dans le département de Loire en Rhône-Alpes. Elle totalisait 1 082 habitants en 2008.

À proximité de Craintilleux sont positionnées géographiquement les villes de
Boisset-lès-Montrond située à 4 km, 1 006 habitants,
Andrézieux-Bouthéon à 6 km, 9 508 habitants,
Précieux localisée à 6 km, 969 habitants,
Montrond-les-Bains à 6 km, 4 608 habitants,
Chamboeuf située à 6 km, 1 516 habitants,
Bonson située à 6 km, 3 710 habitants,
 entre autres. De plus, Craintilleux est située à seulement 19 km de Saint-Étienne.

Le nombre de logements, à Craintilleux, était réparti en 2011 en 15 appartements et 428 maisons soit
 un marché plutôt équilibré.

À Craintilleux, la valorisation moyenne à l'achat d'un appartement se situe à 2 177 € du m² en vente. la valorisation moyenne d'une maison à l'achat se situe à 1 986 € du m². À la location la valeur moyenne se situe à 0 € du m² mensuel.

La commune compte de multiples  équipements sportifs, elle propose entre autres un terrain de tennis, un centre d'équitation, un terrain de golf et un terrain de sport.


text:
\documentclass[12pt]{article}
\usepackage{bm}

\newcounter{problem}
\newenvironment{problem}{%
  \subsubsection*{\refstepcounter{problem}Problem~\theproblem:}}{}
\renewcommand{\vector}[1]{\bm{#1}}
\newcommand{\tensor}[1]{\bm{\mathsf{#1}}}
\newcommand{\ihat}{\vector{i}}
\newcommand{\jhat}{\vector{j}}
\newcommand{\khat}{\vector{k}}

\begin{document}\sloppy\sloppypar\thispagestyle{empty}

\section*{PHYS-UA 120 Dynamics Problem Set 2}

\noindent
\emph{Due in the ``Dynamics'' hand-in box before noon on 2014 September 25.}

\begin{problem}
Kibble \& Berkshire, Ch 3, problem 1
\end{problem}

\begin{problem}
Kibble \& Berkshire, Ch 3, problem 14
\end{problem}

\begin{problem}
Kibble \& Berkshire, Ch 3, problem 15
\end{problem}

\begin{problem}
The equation (3.16) in Kibble \& Berkshire does \emph{not} apply to
the motion of a macroscopic object through the atmosphere.  Cross that
part out of your books!  What applies is actually ram-pressure force.
Also cross out the solution (3.17), which applies to no macroscopic
problem.

Imagine that the projectile launched in Kibble \& Berkshire, Ch~3,
problem~6 was roughly a sphere of mass $M$ and radius $R$.
Roughly---and I mean order of magnitude only---what conditions would
you need to set on the relationship between $M$ and $R$ such that the
assumption (atmospheric drag is negligible) would hold?  Your answer
will have a ``$\gg$'' or a ``$\ll$'' sign in it.  To get concrete, use
for the atmospheric drag the ram-pressure force, which depends only on
the density of the air (which you can estimate).

Once you have that answered, now imagine that the projectile is made
of solid iron.  Your result (above) can be made into a limit on the
mass, which is what?

Finally, what (formally) would have to be the size (radius) of the
iron projectile such that equation (3.16) would \emph{actually} hold?
That is, at what size would the viscous drag force be much stronger than
the ram-pressure force?  To find out, look up the viscosity of air and
Stokes Law.  Again, your answer will have a ``$\gg$'' or a ``$\ll$''
sign in it.  In that case, would the atmospheric drag be negligible?
\end{problem}

\end{document}


text:
package edu.cs4460.msd.backend.utilities;

import processing.core.PApplet;
import processing.core.PFont;
import controlP5.ControlFont;

public class FontHelper {
	private PApplet parent;

	public FontHelper(PApplet p) {
		this.parent = p;
	}

	public ControlFont tabFont() {
		PFont pfont = parent.createFont("Arial", 20, true);
		return  new ControlFont(pfont, 20);
	}

	public ControlFont accordionHeadFont() {
		PFont pfont = parent.createFont("Arial", 16, true);
		return  new ControlFont(pfont, 16);
	}

	public ControlFont accordionSubFont() {
		PFont pfont = parent.createFont("Arial", 12, true);
		return  new ControlFont(pfont, 12);
	}

}


text:
try:
   from osgeo import ogr # Comes first, for fiona's sake (generally this
                         # import is not needed, since we don't ref ogr).
   from osgeo import gdal
   from osgeo import osr
   from osgeo.gdalconst import GA_ReadOnly
except ImportError:
   import ogr
   import gdal
   import osr
   from gdalconst import GA_ReadOnly

import os
import struct
import sys

import conf
import g

from grax.access_infer import Access_Infer
from grax.access_level import Access_Level
from gwis.exception.gwis_error import GWIS_Error
from item import geofeature
from item import item_base
from item import item_versioned
from item import permissions_free
from item.feat import node_byway
from item.grac import group
from item.util.item_type import Item_Type
from util_ import geometry
from util_ import misc

__all__ = ['One', 'Many',]

log = g.log.getLogger('node_endpoint')

# ***

class One(permissions_free.One):

   # Base class overrides

   # NOTE: Before NODE_ENDPOINT, the item type/table was BYWAY_NODE.
   item_type_id = Item_Type.NODE_ENDPOINT
   item_type_table = 'node_endpoint'
   item_gwis_abbrev = 'ndpt'
   child_item_types = None

   #
   item_version_is_branch_specific = True

   # SYNC_ME: See the node_endpoint script, which makes this table.
   local_defns = [
      # py/psql name, deft, send?, pkey?, pytyp, reqv, abbrev, precision
      # MAYBE: move item_versioned columns to node_endpoint table
      # *** Calculated values.
      # The is the node endpoint's x,y geometry. It's stored in the
      # node_endpt_xy table, because an endpoint's geometry never changes.
      # The vertex is stored losslessly (i.e., %.6f) as opposed to node_byway
      # which stores just, e.g., a decimeter's worth.
      # NOTE: This is really an EWKT, i.e., endpoint_ewkt.
      ('endpoint_wkt',        None,  False,   None,    str,  None),
      # This is the number of byways that use this endpoint.
      ('reference_n',         None,  False,  False,    int,  None),
      # This is a list of stack IDs of the byways that use this endpoint.
      # MAYBE: referencers is not implemented.
      ('referencers',         None,  False,  False,    str,  None),
      # *** User-editable values.
      #
      # BUG nnnn: How do we get this sent to the client?
      # This is the elevations in meters from the Digital Elevation Model.
      ('elevation_m',          0.0,  False,  False,  float, 0, None, 1),
      #
      # A dangle is an endpoint that's all alone.
      ('dangle_okay',         None,  False,  False,   bool,     0),
      # "A duex rues" means "Has two streets." It's an endpoint that connects
      # just two byways. (And we might be able to join the two byways.)
      ('a_duex_rues',         None,  False,  False,   bool,     0),
      ]
   attr_defns = permissions_free.One.attr_defns + local_defns
   psql_defns = permissions_free.One.psql_defns + local_defns
   gwis_defns = item_base.One.attr_defns_reduce_for_gwis(attr_defns)

   __slots__ = [
      ] + [attr_defn[0] for attr_defn in local_defns]

   #
   def __init__(self, qb=None, row=None, req=None, copy_from=None):
      permissions_free.One.__init__(self, qb, row, req, copy_from)

   #
   def __str__(self):
      return (
            ('%s | reference_n: %s / dangle_okay: %s / a_duex_rues: %s')
         % (permissions_free.One.__str__(self),
            self.reference_n,
            self.dangle_okay,
            self.a_duex_rues,
            ))

   #
   def from_gml(self, qb, elem):
      permissions_free.One.from_gml(self, qb, elem)

   # ***

   #
   # MAYBE: If we make another class like node_endpoint (which is always
   #        public so the user doesn't specify GIA records), this fcn.
   #        should be moved to a new base class.
   def validize(self, qb, is_new_item, dirty_reason, ref_item):
      # At one time, in the beginning, we made Public GIA records for each
      # node_endpoint. But then it was decided that all endpoints are Public.
      g.assurt(self.groups_access is None)
      if is_new_item:
         g.assurt(ref_item is None)
      else:
         g.assurt(id(self) == id(ref_item))
      permissions_free.One.validize(self, qb, is_new_item, dirty_reason,
                                              ref_item)

   #
   def save_core(self, qb):
      permissions_free.One.save_core(self, qb)
      self.save_insert(qb, One.item_type_table, One.psql_defns)

   #
   def save_update(self, qb):
      # This is used for updating a newly-created node_endpoint that was saved
      # but we want to change it before committing the new revision.
      permissions_free.One.save_update(self, qb)
      # NOTE: Not updating, but clobbering and re-inserting.
      self.save_insert(qb, One.item_type_table, One.psql_defns,
                       do_update=True)

   #
   def save_related_maybe(self, qb, rid):
      permissions_free.One.save_related_maybe(self, qb, rid)
      # If this is a new node_endpoint, we may have to create the node_endpt_xy
      # entry.
      if self.version == 1:
         g.assurt(self.endpoint_wkt)
         # For the public basemap, a new node_endpoint means a new
         # node_endpt_xy. But for branches, a new node_endpoint does not
         # necessarily mean a new node_endpt_xy: if another branch uses the
         # same node_endpoint, the node_endpt_xy should already exist.
         endpt_sql = (
            """
            SELECT
               ST_AsText(endpoint_xy) AS existing_xy_wkt
            FROM
               node_endpt_xy
            WHERE
               node_stack_id = %d
            """
            % (self.stack_id,))
         rows = qb.db.sql(endpt_sql)
         if not rows:
            # This endpoint truly is new.
            sql_endpt_xy = (
               """
               INSERT INTO node_endpt_xy
                  (node_stack_id, endpoint_xy)
               VALUES
                  (%d, '%s')
               """ % (self.stack_id,
                      self.endpoint_wkt,))
            qb.db.sql(sql_endpt_xy)
         else:
            # The endpoint is being used or has been previously used by another
            # branch. Just see if the geometry is the same or not.
            g.assurt(len(rows) == 1)
            existing_xy = geometry.wkt_point_to_xy(rows[0]['existing_xy_wkt'],
                                                precision=conf.node_precision)
            proposed_xy = geometry.wkt_point_to_xy(self.endpoint_wkt,
                                                precision=conf.node_precision)
            if existing_xy != proposed_xy:
               log.warning(
                  'save_related_maybe: endpt_xy unequal: theirs: %s / ours: %s'
                  % (existing_xy, proposed_xy,))

   # ***

   n_node_id_matches = {}

# BUG_FALL_2013: BUG nnnn: This probably doesn't decrement on byway delete?
   #
   def save_new_connection(self, qb, for_byway, rid_new, old_node_id=None):

      g.assurt(rid_new > 0)
      g.assurt((old_node_id is None) or (old_node_id > 0))
      g.assurt(self.stack_id > 0)

      # We expect that node_byway is up to date. Though we don't use it to make
      # the reference_n, since we can figure that out easily enough. Enable
      # this if you'd like to double-check the logic.
      # FIXME: Disable this.
      debug_curious = False
      debug_curious = True

      reference_n = self.reference_n

      # The reference count should only not exist for new nodes.
      if self.version == 0:
         # When the stack ID was assigned, fresh was set.
         g.assurt(self.fresh)
         #
         g.assurt(rid_new != self.valid_start_rid)
         g.assurt(self.valid_start_rid is None)
         # Permissions-free items have no grac records.
         self.groups_access = {}
         self.latest_infer_id = None
         self.latest_infer_username = None
         # A byway this is deleted or reverted should not save new nodes.
         g.assurt((for_byway is None)
                  or (not (for_byway.deleted or for_byway.reverted)))
         # If we're called from node_cache_maker, the reference count is set,
         # otherwise, this is a commit of a new node, so the count is just one.
         if reference_n is None:
            reference_n = 1
         else:
            # The count is 0 for orphans, but it's usually at least one, or
            # hopefully at least 2.
            g.assurt(reference_n >= 0)
         if debug_curious:
            # Just lie so we don't warn. We believe the caller.
            expected_n = reference_n
         #log.debug('save_new_connection: new node: %s' % (self,))
      else:
         # The node was loaded from the database so its reference count should
         # be nonnegative.
         g.assurt(reference_n >= 0)
         if debug_curious:
            expected_n = node_byway.Many.search_get_stats(
               qb, self.stack_id, internals_ok=False)
         # The node had an existing stack ID, so it's not considered fresh.
         g.assurt(not self.fresh)
         #log.debug('save_new_connection: existing: %s' % (self,))

         # Since we loaded reference_n from the database, only bump the
         # reference count if this byway is coming or going.
         if for_byway is not None:
            if for_byway.fresh:
               reference_n += 1
            if for_byway.deleted:
               reference_n -= 1

      # If we've moved from an old node, decrement its reference_n.
      if old_node_id is not None:
         # We expect that we're saving a new version of an existing byway.
         g.assurt((for_byway is not None) and (for_byway.version > 1))
         # Update the old node endpoint, from whence for_byway is being
         # moved. Finalizes the old version, decrements reference_n, and
         # creates a new version.
         old_ndpt = Many.node_endpoint_get(qb, old_node_id, pt_xy=None,
                                               skip_cache=False)
         #log.debug('save_new_connection: decrementing old: %s' % (old_ndpt,))
         g.assurt(old_ndpt.version > 0)

         # save_new_connection is also called without a for_byway or
         # old_node_id from node_cache_maker.py, so cheat and decrement the
         # reference_n here. The save_new_connection fcn. queries the database
         # for the reference_n, and it complains if the object's value differs.
         old_ndpt.reference_n -= 1
         old_ndpt.save_new_connection(qb, for_byway=None,
                                          rid_new=rid_new,
                                          old_node_id=None)
         # We shouldn't be called unless the geometry changes, which doesn't
         # technically happen when deleting or reverting (it does
         # conceptually, i.e., the next time you checkout items).
         g.assurt(not (for_byway.deleted or for_byway.reverted))
         # BUG nnnn: Remove node_endpoints that no longer have users?
         #           Search for cmnts re changing from int stack id to GUID.
         #           But note: When splitting a byway, you might temporarily
         #           orphan endpoints, so you'd want to cleanup node IDs as
         #           a separate operation, i.e., after committing.

         # Increment or decrement the count, depending.
         if for_byway is not None:
            reference_n += (
               +1 if (not (for_byway.deleted or for_byway.reverted)) else -1)
            #log.debug('save_new_connection: incremented old: %s' % (self,))
         else:
            # The byway's node endpoint is moving to a new node, so decrement.
            reference_n -= 1
            #log.debug('save_new_connection: decremented old: %s' % (self,))

      g.assurt(self.endpoint_wkt)
      g.assurt(reference_n >= 0)
      g.assurt(not self.referencers) # Not currently used.
      g.assurt(self.elevation_m is not None)
      #? g.assurt(self.dangle_okay is not None)
      #? g.assurt(self.a_duex_rues is not None)

   # FIXME: If you edit the same node_endpoint via two different byways --
   # because node_endpoints piggy-back on byways -- each byway will have a
   # different state of node_endpoint. You may need a node_endpoint cache
   # while working in pyserver. Definitely in flashclient you'll need
   # to use a cache. But in pyserver, you might just have to know when
   # node_endpoints referenced by a byway become stale... it almost seems
   # easier to do like the attribute and tag cache and just keep 'em in
   # memory...
   # Here's an older comment:
      #
      # FIXME: Let user edit elevation, etc., of node_endpoint. Also update
      # node_endpoint when byway changing. But two byways connecting to same
      # node would load different node_endpoint objects! So we need to use a
      # node_endpoint cache. If the user is editing the node_endpoint, commit
      # will have prepared the item. If the user is just editing byways, the
      # node_endpoint has not been prepared.  Argh, this seems so tedious.....

      g.assurt(self.branch_id == qb.branch_hier[0][0])

      # MAYBE: debug_curious should always be True, maybe? Or maybe not...
      if debug_curious:
         #log.debug('save_new_connection: saved: %s' % (str(self),))
         #
         # [lb] can't quite remember but this just counts how 'off' our code
         # is... ideally, the difference should be zero, eh.
         stat_count_diff = (expected_n - reference_n)
         misc.dict_count_inc(One.n_node_id_matches, stat_count_diff)
         #
         if reference_n != expected_n:
            if not qb.cp_maint_lock_owner:
               # 2014.09.16: Reducing from warning to just info until fixed.
               # FIXME/BUG nnnn: The node_endpoint.reference_n value is not
               #                 correct.
               #  E.g.,
               #     select * from _nby where nd_stk_id = 1320559
               #        and brn_id = 2500677;
               #  returns four rows, and
               #     select * from (select distinct on (stk_id) stk_id, *
               #        from _by where brn_id = 2500677 and (beg_nd = 1320559
               #        or fin_nd = 1320559) order by stk_id desc) as foo
               #        where d is false and until = 'inf';
               #  also returns four rows, but
               #     select ref_n, start, until from _nde
               #        where stk_id = 1320559 and brn_id = 2500677 limit 1;
               #  says
               #      ref_n | start | until
               #     -------+-------+-------
               #          5 | 22626 | inf
               # So, yeah, fix the problem and run node_cache_maker.py again.
               # But this is low priority: reference_n is destined for use to
               # help find dangles and fix map problems, but it's only so far
               # used as output in the Shapefiles. But the new p3 route planner
               # instead uses networkx to find roads that are not connected to
               # the main road network. The reference_n value only helps with
               # dangles and cannot identify disconnected sub-trees of byways.
               # tl;dr The node_endpoint.reference_n value is not always
               # maintained properly (possibly due to deleted byways, such as
               # when splitting byways) but it's doesn't really matter.
               # One solution: after committing the new revision, get a new
               # db cursor and, using a list of byways just saved, recompute
               # reference_n and maybe check the node_byway rows, too.
               #
               #log.warning(
               log.info(
                  'unexpected ref_n: nd: %d / by: %s / ref_n: %d / calcd: %d'
                  % (self.stack_id,
                     'n/a' if for_byway is None else for_byway.stack_id,
                     reference_n,
                     expected_n,))
            # else, this is a bulk operation, so not to worry...? =)
            # 2012.07.30: The old node_cache_maker calculated reference_n
            # incorrectly; the one we calculated is the one to believe.
            reference_n = expected_n
         #else:
         #   log.debug(
         #      'peachy ref_n: stack_id: %9s / ref_n: %d / expected_n: %d'
         #      % ('      n/a' if for_byway is None else for_byway.stack_id,
         #         reference_n, expected_n,))

      self.reference_n = reference_n

      if rid_new != self.valid_start_rid:
         # We're creating a new version (and maybe finalizing the old version,
         # or we're creating a new node_endpoint).
         if self.valid_start_rid is None:
            g.assurt(self.system_id is None)
            g.assurt(self.valid_until_rid is None)
         else:
            g.assurt(self.system_id > 0) # We loaded it from the db.
            # Finalize the last version's revisions.
            item_versioned.One.finalize_last_version_at_revision(
                        self, qb, rid_new, same_version=False)
            self.system_id = None
            g.assurt(self.valid_start_rid < rid_new)
            g.assurt(self.valid_until_rid == conf.rid_inf)
         # Since we're not calling version_finalize_and_increment().
         self.valid_start_rid = rid_new
         self.valid_until_rid = conf.rid_inf
         self.version += 1
         self.acl_grouping = 1
         # We haven't called save() yet, and this class doesn't use validize().
         g.assurt(not self.is_dirty())
         self.dirty_reason_add(item_base.One.dirty_reason_item_auto)
         # Since we're not using validize().
         g.assurt(not self.valid)
         self.valid = True
         #
         self.save(qb, rid_new)
      else:
         g.assurt(self.system_id > 0)
         # This node_endpoint was just saved during the making of a
         # revision, so just update what we haven't committed yet.
         g.assurt(self.valid_start_rid == rid_new)
         g.assurt(self.valid_until_rid == conf.rid_inf)
         #
         self.save_update(qb)

   # ***

class Many(permissions_free.Many):

   one_class = One

   __slots__ = ()

   # *** SQL clauseses

   sql_clauses_cols_all = permissions_free.Many.sql_clauses_cols_all.clone()

   # NOTE: Using ST_AsEWKT instead of ST_AsText, e.g.,
   #         ST_AsText: 'POINT(473657.6 4977266)'
   #         ST_AsEWKT: 'SRID=26915;POINT(473657.6 4977266)'
   #       So SRID is included (since that's what geometry.py's
   #       xy_to_ewkt_point_restrict does).
   sql_clauses_cols_all.inner.select += (
      """
         , item.reference_n
         , item.referencers
         , item.dangle_okay
         , item.a_duex_rues
         , item.elevation_m
         , ST_AsEWKT(ptxy.endpoint_xy) AS endpoint_wkt
      """)

   sql_clauses_cols_all.inner.from_table = (
      """
      FROM
         node_endpoint AS item
      JOIN
         node_endpt_xy AS ptxy
         ON (ptxy.node_stack_id = item.stack_id)
      """)

   # *** Constructor

   def __init__(self):
      permissions_free.Many.__init__(self)

   # *** Finding Node Endpoints

   #
   @staticmethod
   def node_endpoints_get(qb, bway,
                          preferred_beg_node_id,
                          preferred_fin_node_id,
                          skip_cache=False):
      g.assurt((not bway.beg_node_id) and (not bway.fin_node_id))
      (beg_ndpt, fin_ndpt,) = (None, None,)
      (beg_pt, fin_pt,) = Many.node_endpts_get(bway)
      if (beg_pt is not None) and (fin_pt is not None):
         # Either node ID might be None if we need to make a new endpoint.
         log.verbose1('node_endpoints_get: looking for byway nodes: %s and %s'
                      % (bway.beg_node_id, bway.fin_node_id,))
         beg_ndpt = Many.node_endpoint_get(qb, bway.beg_node_id,
                        beg_pt, preferred_beg_node_id, skip_cache)
         fin_ndpt = Many.node_endpoint_get(qb, bway.fin_node_id,
                        fin_pt, preferred_fin_node_id, skip_cache)
      else:
         g.assurt((beg_pt is None) and (fin_pt is None))
      return (beg_ndpt, fin_ndpt,)

   #
   @staticmethod
   def node_endpts_get(bway):
      if bway.geometry_wkt:
         locs = geometry.wkt_line_to_xy(bway.geometry_wkt)
      elif bway.geometry_svg:
         # EXPLAIN: I [lb] always forget: Who uses SVG?
         locs = geometry.svg_line_to_xy(bway.geometry_svg)
      elif bway.geometry:
         # This is for an item loaded from the database. E.g., a split-from
         # byway being copied to a branch so it can be marked deleted: we just
         # use the raw geometry of the item we loaded from the db.
         log.warning('node_endpts_get: use not qb.filters.skip_geometry_wkt')
         g.assurt(False)
      else:
         # Programmer error. This should never happen.
         g.assurt(False)
         locs = (None, None,)
         log.error('elevation_update: no link segment?: %s' % (bway,))
      return (locs[0], locs[-1],)

   #
   @staticmethod
   def node_endpoint_get(qb, stack_id,
                             pt_xy,
                             preferred_node_id=None,
                             skip_cache=False):

      log.verbose1('node_endpoint_get: looking for: %s' % (stack_id,))

      node_endpt = None

      # The stack_id may or may not be set, but it should never be a client id.
      # If it's nonzero, the node exists in the database. If it's not set, the
      # byway doesn't know if the pt_xy has a node yet or not.
      g.assurt((not stack_id) or (stack_id > 0))
      g.assurt(stack_id or pt_xy)

      if (not preferred_node_id) and stack_id:
         preferred_node_id = stack_id

      # 2012.07.10: Thoughts re cache: Each byway has its own reference to a
      #             node in memory, so right now we need the cache like we need
      #             one for tags and attributes.
      #             FIXME: In byway.py, (see call to save_new_connection),
      #                    can we hydrate nodes on demand? probably...
      #             FIXME: Is memory usage a problem? For either routed or
      #                    the connectivity scripts, or import?
      if stack_id and (not skip_cache):
         try:
            g.assurt(qb.item_mgr is not None)
            node_endpt = qb.item_mgr.cache_node_endpoints[stack_id]
         except KeyError:
            pass

      if stack_id and (node_endpt is None):
         node_endpoints = Many()
         node_endpoints.search_by_stack_id(stack_id, qb)
         if len(node_endpoints) == 1:
            node_endpt = node_endpoints[0]
            log.verbose1('node_endpt_get: found by ID: %s' % (node_endpt,))
         else:
            g.assurt(len(node_endpoints) == 0)
            # This never happens, I assume.
            g.assurt(False)

      if node_endpt is None:

# BUG nnnn/FIXME_2013_06_11:
# FIXME: What about node_endpt_xy???????????
#        Why bother w/ node_byway????
# search node_endpt_xy and use same stack_id but make node_endpoint version=1
# or maybe client will find parent branch node and use its stack ID...
# still, we should make sure we're not making a new node_endpt_xy or
# creating a new stack ID for a node whose xy is already in the system!
# also double check that node_endpt_xy is otherwise maintained correctly.

         node_byways = node_byway.Many()
         # This search is exact, down to conf.node_tolerance. And we don't care
         # if there are multiple byways but they don't all use the same
         # node_endpoint: that's for the audit script to complain about.
         g.assurt(pt_xy is not None)
         node_byways.search_by_endpoint_xy(
            qb, pt_xy, internals_ok=False, limit_one=False)

         if node_byways:
            if len(node_byways) == 1:
               stack_id = node_byways[0].node_stack_id
            else:
               ref_counter = {}
               for node_bway in node_byways:
                  stack_id = node_bway.node_stack_id
                  misc.dict_count_inc(ref_counter, stack_id)
                  if stack_id == preferred_node_id:
                     break
               if ((stack_id != preferred_node_id)
                   and (len(ref_counter) > 1)):
                  # Use the node with the most byways.
                  largest = -1
                  for sid, count in ref_counter.iteritems():
                     if count > largest:
                        largest = count
                        stack_id = sid
                  log.debug('%s sid: %d (ref_n: %d of %d) / e.g. nds[0]: %s'
                     % ('Found two+ node endpts: not confident:',
                        stack_id, largest, len(node_byways), node_byways[0],))
            g.assurt(stack_id > 0)
            node_endpoints = Many()
            node_endpoints.search_by_stack_id(stack_id, qb)
            if len(node_endpoints) == 1:
               node_endpt = node_endpoints[0]
               log.verbose1('node_endpt_get: found by xy: %s' % (node_endpt,))
            else:
# FIXME_2013_06_11:
# FIXME: 2013.06.14: What about a leafy branch: won't search_by_endpoint_xy
#                    find the stack_id and then
#                    node_endpoints.search_by_stack_id fails because != brid ?
#   though [lb] notes that node_byway searches by branch_id, as does
#   node_endpoint... but maybe that's the best way...
#
               g.assurt(len(node_endpoints) == 0)
               log.warning('node_endpt_get: missing stack ID: %d'
                           % (stack_id,))

      if node_endpt is None:

         # NOTE: ST_GeomFromEWKT is implicit on INSERT.
         # E.g., "SRID=%s;POINT(%.6f %.6f)"
         endpoint_wkt = geometry.xy_to_ewkt_point_restrict(pt_xy)

         elevation_m = Many.elevation_get_for_pt(pt_xy)

         # We want a permanent ID right away, since we're being managed by a
         # new byway (so there's no need for a client ID, but we still get one
         # for a moment just to be proper, i.e., instead of calling item_mgr's
         # seq_id_next_stack_id directly).
         g.assurt(not stack_id)
         client_id = qb.item_mgr.get_next_client_id()
         permanent_id = qb.item_mgr.stack_id_translate(qb, client_id)
         stack_id = permanent_id

         # I.e., node_endpoint.One(). See also node_cache_maker.
         node_endpt = One(
            qb=qb,
            row={
               # From item_versioned
               'system_id'    : None, # assigned later,
               'branch_id'    : qb.branch_hier[0][0],
               'stack_id'     : stack_id,
               'version'      : 0,
               # From node_endpoint
               # SYNC_ME: See node_cache_maker, which makes this table.
               # Calculated values.
               'endpoint_wkt' : endpoint_wkt,
               'reference_n'  : 1,
               # MAYBE: 'referencers'  : '%d' % (stack_id,),
               # User-editable values.
               'elevation_m'  : elevation_m,
               'dangle_okay'  : None,
               'a_duex_rues'  : None,
               })

         # Fake that we're fresh (the base class only sets it if stack_id less
         # than 0, but we skipped that step and made our own permanent ID).
         node_endpt.fresh = True

         # 2013.10.07: Does this make sense?
         qb.item_mgr.item_cache_add(node_endpt, client_id)

         log.verbose1('node_endpoint_get: created: %s' % (str(node_endpt),))

         # NOTE: It's up to the caller to save this new node_endpoint.

      if not skip_cache:
         try:
            existing = qb.item_mgr.cache_node_endpoints[stack_id]
            g.assurt(existing == node_endpt)
            # Don't save the new node; this happens on delete when
            # we're double-checking that an x,y matches the expected
            # endpoint.
            node_endpt = existing
         except KeyError:
            qb.item_mgr.cache_node_endpoints[stack_id] = node_endpt

      g.assurt(node_endpt.endpoint_wkt)
      g.assurt(node_endpt.reference_n >= 0)
      g.assurt(not node_endpt.referencers) # Not currently used.
      g.assurt(node_endpt.elevation_m is not None)

      return node_endpt

   # *** Table management

   indexed_cols = ('branch_id',
                   'stack_id',
                   'reference_n',
                   #'referencers',
                   #'elevation_m',
                   #'dangle_okay',
                   #'a_duex_rues',
                   )

   #
   @staticmethod
   def drop_indices(db): # ..................//.mind the gap.//..........
      # NOTE: If we call ALTER TABLE ... DROP CONSTRAINT ourselves and the
      # constraint does not exist, the db handle becomes invalid (and you get
      # the "psycopg2.InternalError: current transaction is aborted, commands
      # ignored until end of transaction block" error if you try to use it).
      # I'm not sure how to test if a constraint exists, so that leaves two
      # options: create a new db handle here, or use the convenience function
      # we wrote in db_load_add_constraints.sql.
      db.sql(
         """
         SELECT
            cp_constraint_drop_safe('node_endpoint', 'enforce_valid_geometry')
         """)
      db.sql("DROP INDEX IF EXISTS node_endpoint_endpoint_xy")
      # From the route table.
      # MAYBE: See comments in make_indices.
      #db.sql("SELECT cp_constraint_drop_safe('route', 'route_beg_nid_fkey')")
      #db.sql("SELECT cp_constraint_drop_safe('route', 'route_fin_nid_fkey')")
      # FIXME: Missing any other tables?

      for col_name in Many.indexed_cols:
         # E.g., "DROP INDEX IF EXISTS node_endpoint_branch_id"
         db.sql("DROP INDEX IF EXISTS node_endpoint_%s" % (col_name,))

      # MAYBE: Move to its own file, i.e., item/feat/node_endpt_xy.py ?
      db.sql(
         """
         SELECT
            cp_constraint_drop_safe('node_endpt_xy', 'enforce_valid_geometry')
         """)
      db.sql("DROP INDEX IF EXISTS node_endpt_xy_endpoint_xy")

   #
   @staticmethod
   def make_indices(db):
      # Drop the indices, lest you have to catch: psycopg2.ProgrammingError:
      # constraint "enforce_valid_geometry" for relation "node_endpoint"
      # already exists.
      Many.drop_indices(db)
      # To the route table.
      # MAYBE: The node ID in route is a stack ID, but node_endpoint is keyed
      # by the system ID. So we don't need (or can't use?) a foreign key
      # constraint.
      #db.sql(
      #   """
      #   ALTER TABLE route
      #      ADD CONSTRAINT route_beg_nid_fkey
      #         FOREIGN KEY (beg_nid) REFERENCES node_endpoint (stack_id)
      #            DEFERRABLE
      #   """)
      #db.sql(
      #   """
      #   ALTER TABLE route
      #      ADD CONSTRAINT route_fin_nid_fkey
      #         FOREIGN KEY (fin_nid) REFERENCES node_endpoint (stack_id)
      #            DEFERRABLE
      #   """)

      for col_name in Many.indexed_cols:
         # E.g.,
         #  "CREATE INDEX node_endpoint_branch_id ON node_endpoint(branch_id)"
         db.sql("CREATE INDEX node_endpoint_%s ON node_endpoint(%s)"
                % (col_name, col_name,))

      # MAYBE: Move to its own file, i.e., item/feat/node_endpt_xy.py ?
      sql_add_constraint = (
         """
         ALTER TABLE node_endpt_xy
            ADD CONSTRAINT enforce_valid_geometry
               CHECK (IsValid(endpoint_xy))
         """)
      db.sql(sql_add_constraint)
      #
      # This is the PostGIS 1.x way: USING GIST (... GIST_GEOMETRY_OPS)
      sql_add_index = (
         """
         CREATE INDEX node_endpt_xy_endpoint_xy ON node_endpt_xy
            USING GIST (endpoint_xy)
         """)
      db.sql(sql_add_index)

   # *** Find Nearby Nodes

   # Says PostGIS,
   #    Prior to 1.3, ST_Expand was commonly used in conjunction with && and
   #    ST_Distance to achieve the same effect and in pre-1.3.4 this function
   #    was basically short-hand for that construct. From 1.3.4, ST_DWithin
   #    uses a more short-circuit distance function which should make it more
   #    efficient than prior versions for larger buffer regions.
   # 2012.01.11: We let the programmer choose which method to always use;
   #             see dbg_find_nbor.

   #
   @staticmethod
   def find_nearby(qb, xy_pt, radius, dbg_find_nbor='st_dwithin'):
      if dbg_find_nbor == 'st_expand':
         node_ids, nid_details = Many.find_nearby_v1(qb, xy_pt, radius)
      else:
         g.assurt(dbg_find_nbor == 'st_dwithin')
         node_ids, nid_details = Many.find_nearby_v2(qb, xy_pt, radius)
      return node_ids, nid_details

   #
   @staticmethod
   def find_nearby_v1(qb, xy_pt, radius):

      # E.g., "ST_GeomFromEWKT('SRID=%d;POINT(%.6f %.6f)')"
      point_sql = geometry.xy_to_raw_point_lossless(xy_pt)

      radius = ('%%.%df' % conf.geom_precision) % radius

      results = qb.db.sql(
         """
         SELECT
              ndpt.stack_id
            , ndpt.reference_n
            , ptxy.endpoint_xy
            , ST_Distance(ptxy.endpoint_xy, %s) AS pt_dist
         FROM
            node_endpoint AS ndpt
         JOIN
            item_versioned AS neiv
            ON (neiv.system_id = ndpt.system_id)
         JOIN
            node_endpt_xy AS ptxy
            ON (ptxy.node_stack_id = ndpt.stack_id)
         WHERE
            ndpt.branch_id = %d
            AND neiv.valid_until_rid = %d
            AND ptxy.endpoint_xy && ST_EXPAND(%s, %s)
         ORDER BY
            ST_Distance(ptxy.endpoint_xy, %s) ASC
         """ % (point_sql,
                qb.branch_hier[0][0],
                conf.rid_inf,
                point_sql,
                radius,
                point_sql,))
      # BUG nnnn: "Groveland Rec Center" has this problem: very big
      # intersection, but the six byways that connect use two different
      # node IDs (so there's logically two intersections where there should
      # just be one). Which means the SQL above returns more than one row.
      node_ids = []
      nid_details = {}
      for row in results:
         log.verbose(
            'find_nearby: xy_pt: %s / rad: %s | nid: %d / xy: %s / dist: %f'
            % (xy_pt, radius, row['stack_id'], row['endpoint_xy'],
               row['pt_dist']))
         node_ids.append(row['stack_id'])
         g.assurt(row['stack_id'] not in nid_details)
         nid_details[row['stack_id']] = (row['endpoint_xy'], row['pt_dist'],)
      return node_ids, nid_details

# MAYBE: Assign confidence to what's found nearby?

   #
   @staticmethod
   def find_nearby_v2(qb, xy_pt, radius):

      # "[D]o an ST_DWithin search to utilize indexes to limit [the] search
      #  list that the non-indexable ST_Distance needs to process."

      # E.g., "ST_GeomFromEWKT('SRID=%d;POINT(%.6f %.6f)')"
      point_sql = geometry.xy_to_raw_point_lossless(xy_pt)

      radius = ('%%.%df' % conf.geom_precision) % radius

      # The node_endpoint table doesn't do branch_hier. It's a
      # flattened-branch, not a stacked-branch, table.

      # MAYBE: Use a revision? node_endpoint is wiki'ed nowadays...
      #        but this is just used for route-finding at Current
      #        (route_analysis does not need this for Historic route
      #        finding).

      results = qb.db.sql(
         """
         SELECT
              stack_id
            , reference_n
            , endpoint_xy
            , pt_dist
         FROM (
            SELECT
                 ndpt.stack_id
               , ndpt.reference_n
               , ptxy.endpoint_xy
               , ST_Distance(ptxy.endpoint_xy, %s) AS pt_dist
            FROM
               node_endpoint AS ndpt
            JOIN
               item_versioned AS neiv
               ON (neiv.system_id = ndpt.system_id)
            JOIN
               node_endpt_xy AS ptxy
               ON (ptxy.node_stack_id = ndpt.stack_id)
            WHERE
               ndpt.branch_id = %d
               AND neiv.valid_until_rid = %d
               AND ST_DWithin(ptxy.endpoint_xy, %s, %s)
         ) AS foo_nde_1
         ORDER BY
            pt_dist ASC
         """ % (point_sql,
                qb.branch_hier[0][0],
                conf.rid_inf,
                point_sql,
                radius,))

      # BUG nnnn: "Groveland Rec Center" has this problem: very big
      # intersection, but the six byways that connect use two different
      # node IDs (so there's logically two intersections where there should
      # just be one). Which means the SQL above returns more than one row.

      node_ids = []
      nid_details = {}
      for row in results:
         log.verbose(
               'find_nearby: xy_pt: %s / rad: %s | nid: %d / xy: %s / dist: %f'
            % (xy_pt, radius, row['stack_id'], row['endpoint_xy'],
               row['pt_dist']))
         node_ids.append(row['stack_id'])
         g.assurt(row['stack_id'] not in nid_details)
         nid_details[row['stack_id']] = (row['endpoint_xy'], row['pt_dist'],)

      return node_ids, nid_details

   # *** Global DEM handle.

   # Since we're using a C module, elevdata must be global, as raster is simply
   # a pointer to elevdata. If elevdata is not global, it can cause a segfault.
   # FIXME: [lb] wants to not always load DEM for all pyserver calls, since
   # they don't use them. So is class-scope okay?

   elevdata = None
   raster = None
   xform_coord = None
   xform_pixel = None

   #
   @staticmethod
   def node_endpoints_cache_ensure():
      # Set up the elevation data file for the elevation module.
      if (Many.elevdata is None) and (conf.elevation_tiff):
         log.debug('Opening DEM...')
         Many.elevdata = gdal.Open(conf.elevation_tiff, GA_ReadOnly)
         if Many.elevdata is None:
            log.error('Error reading elevation source file: %s.'
                      % (conf.elevation_tiff,))
            raise GWIS_Error('Error reading elevation source file.')
         log.debug(' opened DEM.')
         proj = Many.elevdata.GetProjection()
         dest_srs = geometry.spatial_reference_from_wkt(proj)
         src_srs = geometry.spatial_reference_from_srid(conf.default_srid)
         Many.xform_coord = osr.CoordinateTransformation(src_srs, dest_srs)
         Many.xform_pixel = geometry.Geo_Transform(
                        Many.elevdata.GetGeoTransform())
         Many.raster = Many.elevdata.GetRasterBand(1)
         # NOTE: The dest_srs might be lon/lat or it might be bbox_x/_y.
         #       I.e., we'll transform points to, e.g., UTM 15, or to latlon.
         #  Many.raster.ComputeRasterMinMax(1): (646.0, 2011.0)

   # ***

   feet_to_meters = 0.3048

   #
   @staticmethod
   def elevation_get_for_pt(pt_xy):
      Many.node_endpoints_cache_ensure()
      if Many.raster is not None:
         elevation_m = Many.elevation_get_for_pt_(pt_xy)
      else:
         elevation_m = conf.elevation_mean
      return elevation_m

   #
   @staticmethod
   def elevation_get_for_pt_(pt_xy):
      '''Take a point in the default srs and return its elevation in meters.'''
      pt_x, pt_y = float(pt_xy[0]), float(pt_xy[1])
      (lon_or_bbox_x, lat_or_bbox_y, pt_z
         ) = Many.xform_coord.TransformPoint(pt_x, pt_y)
      (pixel_x, pixel_y
         ) = Many.xform_pixel.transform_point(lon_or_bbox_x, lat_or_bbox_y)
      try:
         # Check the size of the raster. If the lat/lon or bbox_x/_y is
         # elsewhere (e.g., Wisconsin), OGR prints to stderr:
         #   ERROR 5: Access window out of range in RasterIO(). Requested
         #            (15675,6926) of size 1x1 on raster of 15638x11844.
         if ((pixel_x < Many.elevdata.RasterXSize)
             and (pixel_y < Many.elevdata.RasterYSize)):
            elevation_struct = Many.raster.ReadRaster(pixel_x, pixel_y, 1, 1)
            try:
               if len(elevation_struct) == 4:
                  # A four-byte number.
                  elevation_m = struct.unpack('f'*1, elevation_struct)[0]
                  g.assurt(isinstance(elevation_m, float))
               elif len(elevation_struct) == 2:
                  # A two-byte number... no wonder the state DEM
                  # is only 100 MB larger than the metro DEM.
                  elevation_m = struct.unpack('h', elevation_struct)[0]
                  g.assurt(isinstance(elevation_m, int))
                  elevation_m = float(elevation_m)
               else:
                  log.error('Unexpected elevation packed length: %s (%s)'
                            % (elevation_struct, len(elevation_struct),))
                  g.assurt(False)
               if conf.elevation_units == 'feet':
                  elevation_m *= Many.feet_to_meters
            except struct.error, e:
               g.assurt(False) # Should be caught by RasterX/YSize check.
               pass
         else:
            # BUG nnnn: Better DEM coverage (maybe multiple dems, with a bbox
            # lookup to figure out which one to use?).
            # 2012.02.10: There are dozens of these pts in Ccp. I [lb] sampled
            #             two of them and they were both River Falls, 'sconi.
            # MAYBE: Using %s on tuple means lots of precision, e.g.,
            #        530160.95999999996. Make a wrapper for printing tuple-pts.
            log.warning(
               'nd_elev_ins: outside DEM: ptxy %s,%s / bbox %s,%s / pxel %s,%s'
               % (pt_x, pt_y, lon_or_bbox_x, lat_or_bbox_y, pixel_x, pixel_y,))
            # BUG nnnn: The profile in the client is going to be inaccurate.
            elevation_m = conf.elevation_mean
      except TypeError:
         if elevation_m is None:
            # This is probably outside MN, right?
            log.warning('node_elevation_insert: no elev for pt: %s' % (pt_xy,))
            # BUG nnnn: The profile in the client is going to be inaccurate.
            elevation_m = conf.elevation_mean
      g.assurt(elevation_m is not None)
      elevation_m = round(float(elevation_m), conf.elev_precision + 1)
      return elevation_m

   # ***

# ***


text:
On its own, Salesforce has a lot of built in power to get an organization going in a short amount of time. Beyond that are tools like Apex, Visualforce and Triggers that vastly add to the flexibility. What happens when you need to do something that expands past what is possible with Salesforce alone? For that you need to use a connected app. Luckily this is very easy and in a few minutes you will have done it.

##What is a "Connected app?"
For specialized tasks or ones that break the limits of Salesforce, you will need some code outside of Salesforce, a way to authenticate a user and a way to leverage your current data in Salesforce. As it turns out, Salesforce has all of these! Lets start with the foundation

##Where will this run?
You are going to need someplace to run your code. In reality this could be run almost anywhere, from a phone, to a server, and everywhere in between. For this quick-start, there is an application set up on Heroku just waiting for you to connect to it. In the real world, you are not going to be giving your connection information out to anyone but applications you trust to a very high degree. If you are still wary, good for you, that security mindedness will help. Anyway, if you are wary about going through an app you don't control, feel free to clone this git repository, sign up for Heroku, and deploy there.

##How will this authenticate
For business, security is paramount. Salesforce provides a robust suite of features for access control and with connected apps, we don't lose any of it. Using s system called OAuth, you can control what permissions a connected app has on top of the permissions of the individual users.

###Callback Url
How this flow works is that the external application will open a new page in Salesforce. This will pass along two tokens you will get when you are finished configuring the connected app and the Callback Url you configured. If you are not logged in, you will be asked to do that now. If you are already logged in, or just did so, Salesforce will ask if you want to let this app connect. After you grant the app access, Salesforce will call the Callback Url with an access token that will grant your external application access to Salesforce.

###Selected OAuth Scopes
This jumbled mess of terms is what lets you define some additional permissions for what your external app can do. Suffice to say going over every one of them is outside the scope of this quick start. We will just add the api scope, allowing us access to some basic data management.

##Setting up the Connected app
###What You'll Do
+ Configure your connected app in Salesforce.
+ Use the pre-made Node.js app to connect to Salesforce and list the accounts.

###Step 1: Create the connected app
1. Log in to your Developer Edition (DE) org.
2. Click the Setup link (upper right corner).
![Setup menu](https://cloud.githubusercontent.com/assets/1520229/9043995/87449258-39e7-11e5-81a8-c8acf6661e95.png)
3. In the left navigation, select Build > Create > Apps.
![App menu](https://cloud.githubusercontent.com/assets/1520229/9043992/873ff220-39e7-11e5-81b5-a972ea3030ba.png)
4. Click the New button on the connected app list
5. Fill in the form as follows
	+ Connected App Name: Connected Quickstart
	+ API Name: Connected_Quickstart
	+ Contact Email: YOUR_EMAIL@PROVIDER
	+ Enable OAuth Settings: Checked
	+ Callback URL: https://fathomless-reef-5467.herokuapp.com/accounts.
	+ Selected OAuth Scopes: Access and manage your data (api)
	![Sections to fill in](https://cloud.githubusercontent.com/assets/1520229/9043994/87446f30-39e7-11e5-85d3-ff4f2e3bded1.png)
6. Click Save and then Continue
7. Take a break, it can take some time for the configuration to take hold

###Step 2: Connect the external app
1. Navigate to https://fathomless-reef-5467.herokuapp.com/ in a new tab or window. You will need to leave the connected apps detail screen open to get some info off of it.
![Data in Salesforce](https://cloud.githubusercontent.com/assets/1520229/9043993/8741abd8-39e7-11e5-81ce-4fb7f58e7c91.png)
2. Fill in the form as follows
	+ In the first input box, copy the Consumer Key from the details page
	+ On the details page, click the link next to the Consumer Secret to reveal it, then copy that value into the second box
	![Inputs in Heroku](https://cloud.githubusercontent.com/assets/1520229/9044353/183d1a8a-39ea-11e5-9631-db548902a128.png)

3. Click Submit
4. Next
	+ If you are not logged in, you will be asked to do so
	+ If you are logged in, or just did, you will be asked to allow the external app
5. If everything went well, you will see the first 10 accounts in your org.


text:
{% extends "base.html" %}

{% block title %}{{ block.super }} – In Print – Game answers{% endblock title %}

{% block content %}

Game answers

{% if object_list %}
    {% for answer in object_list %}

{{ answer.type }}

    {% endfor %}
{% else %}

For some reason, there aren't any answers to display here.

{% endif %}
{% endblock content %}