text:
package org.elasticsearch.river.twitter;

import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.indices.IndexAlreadyExistsException;
import org.elasticsearch.river.AbstractRiverComponent;
import org.elasticsearch.river.River;
import org.elasticsearch.river.RiverName;
import org.elasticsearch.river.RiverSettings;
import org.elasticsearch.threadpool.ThreadPool;
import twitter4j.*;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class TwitterRiver extends AbstractRiverComponent implements River {

    private final ThreadPool threadPool;

    private final Client client;

    private final String oauthConsumerKey;
    private final String oauthConsumerSecret;
    private final String oauthAccessToken;
    private final String oauthAccessTokenSecret;

    private final TimeValue retryAfter;

    private final String proxyHost;
    private final String proxyPort;
    private final String proxyUser;
    private final String proxyPassword;

    private final boolean raw;
    private final boolean ignoreRetweet;
    private final boolean geoAsArray;

    private final String indexName;

    private final String typeName;

    private final int bulkSize;
    private final int maxConcurrentBulk;
    private final TimeValue bulkFlushInterval;

    private final FilterQuery filterQuery;

    private final String streamType;

    private RiverStatus riverStatus;

    private volatile TwitterStream stream;

    private volatile BulkProcessor bulkProcessor;

    @SuppressWarnings({"unchecked"})
    @Inject
    public TwitterRiver(RiverName riverName, RiverSettings riverSettings, Client client, ThreadPool threadPool, Settings settings) {
        super(riverName, riverSettings);
        this.riverStatus = RiverStatus.UNKNOWN;
        this.client = client;
        this.threadPool = threadPool;

        String riverStreamType;

        if (riverSettings.settings().containsKey("twitter")) {
            Map<String, Object> twitterSettings = (Map<String, Object>) riverSettings.settings().get("twitter");

            raw = XContentMapValues.nodeBooleanValue(twitterSettings.get("raw"), false);
            ignoreRetweet = XContentMapValues.nodeBooleanValue(twitterSettings.get("ignore_retweet"), false);
            geoAsArray = XContentMapValues.nodeBooleanValue(twitterSettings.get("geo_as_array"), false);

            if (twitterSettings.containsKey("oauth")) {
                Map<String, Object> oauth = (Map<String, Object>) twitterSettings.get("oauth");
                if (oauth.containsKey("consumer_key")) {
                    oauthConsumerKey = XContentMapValues.nodeStringValue(oauth.get("consumer_key"), null);
                } else {
                    oauthConsumerKey = settings.get("river.twitter.oauth.consumer_key");
                }
                if (oauth.containsKey("consumer_secret")) {
                    oauthConsumerSecret = XContentMapValues.nodeStringValue(oauth.get("consumer_secret"), null);
                } else {
                    oauthConsumerSecret = settings.get("river.twitter.oauth.consumer_secret");
                }
                if (oauth.containsKey("access_token")) {
                    oauthAccessToken = XContentMapValues.nodeStringValue(oauth.get("access_token"), null);
                } else {
                    oauthAccessToken = settings.get("river.twitter.oauth.access_token");
                }
                if (oauth.containsKey("access_token_secret")) {
                    oauthAccessTokenSecret = XContentMapValues.nodeStringValue(oauth.get("access_token_secret"), null);
                } else {
                    oauthAccessTokenSecret = settings.get("river.twitter.oauth.access_token_secret");
                }
            } else {
                oauthConsumerKey = settings.get("river.twitter.oauth.consumer_key");
                oauthConsumerSecret = settings.get("river.twitter.oauth.consumer_secret");
                oauthAccessToken = settings.get("river.twitter.oauth.access_token");
                oauthAccessTokenSecret = settings.get("river.twitter.oauth.access_token_secret");
            }

            if (twitterSettings.containsKey("retry_after")) {
                retryAfter = XContentMapValues.nodeTimeValue(twitterSettings.get("retry_after"), TimeValue.timeValueSeconds(10));
            } else {
                retryAfter = XContentMapValues.nodeTimeValue(settings.get("river.twitter.retry_after"), TimeValue.timeValueSeconds(10));
            }

            if (twitterSettings.containsKey("proxy")) {
                Map<String, Object> proxy = (Map<String, Object>) twitterSettings.get("proxy");
                proxyHost = XContentMapValues.nodeStringValue(proxy.get("host"), null);
                proxyPort = XContentMapValues.nodeStringValue(proxy.get("port"), null);
                proxyUser = XContentMapValues.nodeStringValue(proxy.get("user"), null);
                proxyPassword = XContentMapValues.nodeStringValue(proxy.get("password"), null);
            } else {
                // Let's see if we have that in node settings
                proxyHost = settings.get("river.twitter.proxy.host");
                proxyPort = settings.get("river.twitter.proxy.port");
                proxyUser = settings.get("river.twitter.proxy.user");
                proxyPassword = settings.get("river.twitter.proxy.password");
            }

            riverStreamType = XContentMapValues.nodeStringValue(twitterSettings.get("type"), "sample");
            Map<String, Object> filterSettings = (Map<String, Object>) twitterSettings.get("filter");

            if (riverStreamType.equals("filter") && filterSettings == null) {
                filterQuery = null;
                stream = null;
                streamType = null;
                indexName = null;
                typeName = "status";
                bulkSize = 100;
                this.maxConcurrentBulk = 1;
                this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
                logger.warn("no filter defined for type filter. Disabling river...");
                return;
            }

            if (filterSettings != null) {
                riverStreamType = "filter";
                filterQuery = new FilterQuery();
                filterQuery.count(XContentMapValues.nodeIntegerValue(filterSettings.get("count"), 0));
                Object tracks = filterSettings.get("tracks");
                boolean filterSet = false;
                if (tracks != null) {
                    if (tracks instanceof List) {
                        List<String> lTracks = (List<String>) tracks;
                        filterQuery.track(lTracks.toArray(new String[lTracks.size()]));
                    } else {
                        filterQuery.track(Strings.commaDelimitedListToStringArray(tracks.toString()));
                    }
                    filterSet = true;
                }
                Object follow = filterSettings.get("follow");
                if (follow != null) {
                    if (follow instanceof List) {
                        List lFollow = (List) follow;
                        long[] followIds = new long[lFollow.size()];
                        for (int i = 0; i < lFollow.size(); i++) {
                            Object o = lFollow.get(i);
                            if (o instanceof Number) {
                                followIds[i] = ((Number) o).intValue();
                            } else {
                                followIds[i] = Long.parseLong(o.toString());
                            }
                        }
                        filterQuery.follow(followIds);
                    } else {
                        String[] ids = Strings.commaDelimitedListToStringArray(follow.toString());
                        long[] followIds = new long[ids.length];
                        for (int i = 0; i < ids.length; i++) {
                            followIds[i] = Long.parseLong(ids[i]);
                        }
                        filterQuery.follow(followIds);
                    }
                    filterSet = true;
                }
                Object locations = filterSettings.get("locations");
                if (locations != null) {
                    if (locations instanceof List) {
                        List lLocations = (List) locations;
                        double[][] dLocations = new double[lLocations.size()][];
                        for (int i = 0; i < lLocations.size(); i++) {
                            Object loc = lLocations.get(i);
                            double lat;
                            double lon;
                            if (loc instanceof List) {
                                List lLoc = (List) loc;
                                if (lLoc.get(0) instanceof Number) {
                                    lon = ((Number) lLoc.get(0)).doubleValue();
                                } else {
                                    lon = Double.parseDouble(lLoc.get(0).toString());
                                }
                                if (lLoc.get(1) instanceof Number) {
                                    lat = ((Number) lLoc.get(1)).doubleValue();
                                } else {
                                    lat = Double.parseDouble(lLoc.get(1).toString());
                                }
                            } else {
                                String[] sLoc = Strings.commaDelimitedListToStringArray(loc.toString());
                                lon = Double.parseDouble(sLoc[0]);
                                lat = Double.parseDouble(sLoc[1]);
                            }
                            dLocations[i] = new double[]{lon, lat};
                        }
                        filterQuery.locations(dLocations);
                    } else {
                        String[] sLocations = Strings.commaDelimitedListToStringArray(locations.toString());
                        double[][] dLocations = new double[sLocations.length / 2][];
                        int dCounter = 0;
                        for (int i = 0; i < sLocations.length; i++) {
                            double lon = Double.parseDouble(sLocations[i]);
                            double lat = Double.parseDouble(sLocations[++i]);
                            dLocations[dCounter++] = new double[]{lon, lat};
                        }
                        filterQuery.locations(dLocations);
                    }
                    filterSet = true;
                }
                Object userLists = filterSettings.get("user_lists");
                if (userLists != null) {
                    if (userLists instanceof List) {
                        List<String> lUserlists = (List<String>) userLists;
                        String[] tUserlists = lUserlists.toArray(new String[lUserlists.size()]);
                        filterQuery.follow(getUsersListMembers(tUserlists));
                    } else {
                        String[] tUserlists = Strings.commaDelimitedListToStringArray(userLists.toString());
                        filterQuery.follow(getUsersListMembers(tUserlists));
                    }
                    filterSet = true;
                }

                // We should have something to filter
                if (!filterSet) {
                    streamType = null;
                    indexName = null;
                    typeName = "status";
                    bulkSize = 100;
                    this.maxConcurrentBulk = 1;
                    this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
                    logger.warn("can not set language filter without tracks, follow, locations or user_lists. Disabling river.");
                    return;
                }

                Object language = filterSettings.get("language");
                if (language != null) {
                    if (language instanceof List) {
                        List<String> lLanguage = (List<String>) language;
                        filterQuery.language(lLanguage.toArray(new String[lLanguage.size()]));
                    } else {
                        filterQuery.language(Strings.commaDelimitedListToStringArray(language.toString()));
                    }
                }
            } else {
                filterQuery = null;
            }
        } else {
            // No specific settings. We need to use some defaults
            riverStreamType = "sample";
            raw = false;
            ignoreRetweet = false;
            geoAsArray = false;
            oauthConsumerKey = settings.get("river.twitter.oauth.consumer_key");
            oauthConsumerSecret = settings.get("river.twitter.oauth.consumer_secret");
            oauthAccessToken = settings.get("river.twitter.oauth.access_token");
            oauthAccessTokenSecret = settings.get("river.twitter.oauth.access_token_secret");
            retryAfter = XContentMapValues.nodeTimeValue(settings.get("river.twitter.retry_after"), TimeValue.timeValueSeconds(10));
            filterQuery = null;
            proxyHost = null;
            proxyPort = null;
            proxyUser = null;
            proxyPassword =null;
        }

        if (oauthAccessToken == null || oauthConsumerKey == null || oauthConsumerSecret == null || oauthAccessTokenSecret == null) {
            stream = null;
            streamType = null;
            indexName = null;
            typeName = "status";
            bulkSize = 100;
            this.maxConcurrentBulk = 1;
            this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
            logger.warn("no oauth specified, disabling river...");
            return;
        }

        if (riverSettings.settings().containsKey("index")) {
            Map<String, Object> indexSettings = (Map<String, Object>) riverSettings.settings().get("index");
            indexName = XContentMapValues.nodeStringValue(indexSettings.get("index"), riverName.name());
            typeName = XContentMapValues.nodeStringValue(indexSettings.get("type"), "status");
            this.bulkSize = XContentMapValues.nodeIntegerValue(indexSettings.get("bulk_size"), 100);
            this.bulkFlushInterval = TimeValue.parseTimeValue(XContentMapValues.nodeStringValue(
                    indexSettings.get("flush_interval"), "5s"), TimeValue.timeValueSeconds(5));
            this.maxConcurrentBulk = XContentMapValues.nodeIntegerValue(indexSettings.get("max_concurrent_bulk"), 1);
        } else {
            indexName = riverName.name();
            typeName = "status";
            bulkSize = 100;
            this.maxConcurrentBulk = 1;
            this.bulkFlushInterval = TimeValue.timeValueSeconds(5);
        }

        logger.info("creating twitter stream river");
        if (raw && logger.isDebugEnabled()) {
            logger.debug("will index twitter raw content...");
        }

        streamType = riverStreamType;
        this.riverStatus = RiverStatus.INITIALIZED;
    }

    /**
     * Get users id of each list to stream them.
     * @param tUserlists List of user list. Should be a public list.
     * @return
     */
    private long[] getUsersListMembers(String[] tUserlists) {
        logger.debug("Fetching user id of given lists");
        List<Long> listUserIdToFollow = new ArrayList<Long>();
        Configuration cb = buildTwitterConfiguration();
        Twitter twitterImpl = new TwitterFactory(cb).getInstance();

        //For each list given in parameter
        for (String listId : tUserlists) {
            logger.debug("Adding users of list {} ",listId);
            String[] splitListId = listId.split("/");
            try {
                long cursor = -1;
                PagableResponseList<User> itUserListMembers;
                do {
                    itUserListMembers = twitterImpl.getUserListMembers(splitListId[0], splitListId[1], cursor);
                    for (User member : itUserListMembers) {
                        long userId = member.getId();
                        listUserIdToFollow.add(userId);
                    }
                } while ((cursor = itUserListMembers.getNextCursor()) != 0);

            } catch (TwitterException te) {
                logger.error("Failed to get list members for : {}", listId, te);
            }
        }

        //Just casting from Long to long
        long ret[] = new long[listUserIdToFollow.size()];
        int pos = 0;
        for (Long userId : listUserIdToFollow) {
            ret[pos] = userId;
            pos++;
        }
        return ret;
    }

    /**
     * Build configuration object with credentials and proxy settings
     * @return
     */
    private Configuration buildTwitterConfiguration() {
        logger.debug("creating twitter configuration");
        ConfigurationBuilder cb = new ConfigurationBuilder();

        cb.setOAuthConsumerKey(oauthConsumerKey)
                .setOAuthConsumerSecret(oauthConsumerSecret)
                .setOAuthAccessToken(oauthAccessToken)
                .setOAuthAccessTokenSecret(oauthAccessTokenSecret);

        if (proxyHost != null) cb.setHttpProxyHost(proxyHost);
        if (proxyPort != null) cb.setHttpProxyPort(Integer.parseInt(proxyPort));
        if (proxyUser != null) cb.setHttpProxyUser(proxyUser);
        if (proxyPassword != null) cb.setHttpProxyPassword(proxyPassword);
        if (raw) cb.setJSONStoreEnabled(true);
        logger.debug("twitter configuration created");
        return cb.build();
    }

    /**
     * Start twitter stream
     */
    private void startTwitterStream() {
        logger.info("starting {} twitter stream", streamType);

        if (stream == null) {
            logger.debug("creating twitter stream");

            stream = new TwitterStreamFactory(buildTwitterConfiguration()).getInstance();
            if (streamType.equals("user")) {
                stream.addListener(new UserStreamHandler());
            } else {
                stream.addListener(new StatusHandler());
            }

            logger.debug("twitter stream created");
        }

        if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
            if (streamType.equals("filter") || filterQuery != null) {
                stream.filter(filterQuery);
            } else if (streamType.equals("firehose")) {
                stream.firehose(0);
            } else if (streamType.equals("user")) {
                stream.user();
            } else {
                stream.sample();
            }
        }
        logger.debug("{} twitter stream started!", streamType);
    }

    @Override
    public void start() {
        this.riverStatus = RiverStatus.STARTING;
        // Let's start this in another thread so we won't stop the start process
        threadPool.generic().execute(new Runnable() {
            @Override
            public void run() {
                if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
                    // We are first waiting for a yellow state at least
                    logger.debug("waiting for yellow status");
                    client.admin().cluster().prepareHealth("_river").setWaitForYellowStatus().get();
                    logger.debug("yellow or green status received");
                }

                if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
                    // We push ES mapping only if raw is false
                    if (!raw) {
                        try {
                            logger.debug("Trying to create index [{}]", indexName);
                            client.admin().indices().prepareCreate(indexName).execute().actionGet();
                            logger.debug("index created [{}]", indexName);
                        } catch (Exception e) {
                            if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
                                // that's fine
                                logger.debug("Index [{}] already exists, skipping...", indexName);
                            } else if (ExceptionsHelper.unwrapCause(e) instanceof ClusterBlockException) {
                                // ok, not recovered yet..., lets start indexing and hope we recover by the first bulk
                                // TODO: a smarter logic can be to register for cluster event listener here, and only start sampling when the block is removed...
                                logger.debug("Cluster is blocked for now. Index [{}] can not be created, skipping...", indexName);
                            } else {
                                logger.warn("failed to create index [{}], disabling river...", e, indexName);
                                riverStatus = RiverStatus.STOPPED;
                                return;
                            }
                        }

                        if (client.admin().indices().prepareGetMappings(indexName).setTypes(typeName).get().getMappings().isEmpty()) {
                            try {
                                String mapping = XContentFactory.jsonBuilder().startObject().startObject(typeName).startObject("properties")
                                        .startObject("location").field("type", "geo_point").endObject()
                                        .startObject("language").field("type", "string").field("index", "not_analyzed").endObject()
                                        .startObject("user").startObject("properties").startObject("screen_name").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()
                                        .startObject("mention").startObject("properties").startObject("screen_name").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()
                                        .startObject("in_reply").startObject("properties").startObject("user_screen_name").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()
                                        .startObject("retweet").startObject("properties").startObject("user_screen_name").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()
                                        .endObject().endObject().endObject().string();
                                logger.debug("Applying default mapping for [{}]/[{}]: {}", indexName, typeName, mapping);
                                client.admin().indices().preparePutMapping(indexName).setType(typeName).setSource(mapping).execute().actionGet();
                            } catch (Exception e) {
                                logger.warn("failed to apply default mapping [{}]/[{}], disabling river...", e, indexName, typeName);
                                return;
                            }
                        } else {
                            logger.debug("Mapping already exists for [{}]/[{}], skipping...", indexName, typeName);
                        }
                    }
                }

                // Creating bulk processor
                logger.debug("creating bulk processor [{}]", indexName);
                bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {
                    @Override
                    public void beforeBulk(long executionId, BulkRequest request) {
                        logger.debug("Going to execute new bulk composed of {} actions", request.numberOfActions());
                    }

                    @Override
                    public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
                        logger.debug("Executed bulk composed of {} actions", request.numberOfActions());
                        if (response.hasFailures()) {
                            logger.warn("There was failures while executing bulk", response.buildFailureMessage());
                            if (logger.isDebugEnabled()) {
                                for (BulkItemResponse item : response.getItems()) {
                                    if (item.isFailed()) {
                                        logger.debug("Error for {}/{}/{} for {} operation: {}", item.getIndex(),
                                                item.getType(), item.getId(), item.getOpType(), item.getFailureMessage());
                                    }
                                }
                            }
                        }
                    }

                    @Override
                    public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
                        logger.warn("Error executing bulk", failure);
                    }
                })
                        .setBulkActions(bulkSize)
                        .setConcurrentRequests(maxConcurrentBulk)
                        .setFlushInterval(bulkFlushInterval)
                    .build();

                logger.debug("Bulk processor created with bulkSize [{}], bulkFlushInterval [{}]", bulkSize, bulkFlushInterval);
                if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
                    startTwitterStream();
                    riverStatus = RiverStatus.RUNNING;
                }
            }
        });
    }

    private void reconnect() {
        if (riverStatus == RiverStatus.STOPPING || riverStatus == RiverStatus.STOPPED ) {
            logger.debug("can not reconnect twitter on a closed river");
            return;
        }

        riverStatus = RiverStatus.STARTING;

        if (stream != null) {
            try {
                logger.debug("cleanup stream");
                stream.cleanUp();
            } catch (Exception e) {
                logger.debug("failed to cleanup after failure", e);
            }
            try {
                logger.debug("shutdown stream");
                stream.shutdown();
            } catch (Exception e) {
                logger.debug("failed to shutdown after failure", e);
            }
        }

        if (riverStatus == RiverStatus.STOPPING || riverStatus == RiverStatus.STOPPED ) {
            logger.debug("can not reconnect twitter on a closed river");
            return;
        }

        try {
            startTwitterStream();
            riverStatus = RiverStatus.RUNNING;
        } catch (Exception e) {
            if (riverStatus == RiverStatus.STOPPING || riverStatus == RiverStatus.STOPPED ) {
                logger.debug("river is closing. we won't reconnect.");
                close();
                return;
            }
            // TODO, we can update the status of the river to RECONNECT
            logger.warn("failed to connect after failure, throttling", e);
            threadPool.schedule(retryAfter, ThreadPool.Names.GENERIC, new Runnable() {
                @Override
                public void run() {
                    reconnect();
                }
            });
        }
    }

    @Override
    public void close() {
        riverStatus = RiverStatus.STOPPING;

        logger.info("closing twitter stream river");

        if (bulkProcessor != null) {
            bulkProcessor.close();
        }

        if (stream != null) {
            // No need to call stream.cleanUp():
            // - since it is done by the implementation of shutdown()
            // - it will lead to a thread leak (see TwitterStreamImpl.cleanUp() and TwitterStreamImpl.shutdown() )
            stream.shutdown();
        }

        riverStatus = RiverStatus.STOPPED;
    }

    private class StatusHandler extends StatusAdapter {

        @Override
        public void onStatus(Status status) {
            if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
                try {
                    // #24: We want to ignore retweets (default to false) https://github.com/elasticsearch/elasticsearch-river-twitter/issues/24
                    if (status.isRetweet() && ignoreRetweet) {
                        if (logger.isTraceEnabled()) {
                            logger.trace("ignoring status cause retweet {} : {}", status.getUser().getName(), status.getText());
                        }
                    } else {
                        if (logger.isTraceEnabled()) {
                            logger.trace("status {} : {}", status.getUser().getName(), status.getText());
                        }

                        // If we want to index tweets as is, we don't need to convert it to JSon doc
                        if (raw) {
                            String rawJSON = TwitterObjectFactory.getRawJSON(status);
                            if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
                                bulkProcessor.add(Requests.indexRequest(indexName).type(typeName).id(Long.toString(status.getId())).source(rawJSON));
                            }
                        } else {
                            XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
                            builder.field("text", status.getText());
                            builder.field("created_at", status.getCreatedAt());
                            builder.field("source", status.getSource());
                            builder.field("truncated", status.isTruncated());
                            builder.field("language", status.getLang());

                            if (status.getUserMentionEntities() != null) {
                                builder.startArray("mention");
                                for (UserMentionEntity user : status.getUserMentionEntities()) {
                                    builder.startObject();
                                    builder.field("id", user.getId());
                                    builder.field("name", user.getName());
                                    builder.field("screen_name", user.getScreenName());
                                    builder.field("start", user.getStart());
                                    builder.field("end", user.getEnd());
                                    builder.endObject();
                                }
                                builder.endArray();
                            }

                            if (status.getRetweetCount() != -1) {
                                builder.field("retweet_count", status.getRetweetCount());
                            }

                            if (status.isRetweet() && status.getRetweetedStatus() != null) {
                                builder.startObject("retweet");
                                builder.field("id", status.getRetweetedStatus().getId());
                                if (status.getRetweetedStatus().getUser() != null) {
                                    builder.field("user_id", status.getRetweetedStatus().getUser().getId());
                                    builder.field("user_screen_name", status.getRetweetedStatus().getUser().getScreenName());
                                    if (status.getRetweetedStatus().getRetweetCount() != -1) {
                                        builder.field("retweet_count", status.getRetweetedStatus().getRetweetCount());
                                    }
                                }
                                builder.endObject();
                            }

                            if (status.getInReplyToStatusId() != -1) {
                                builder.startObject("in_reply");
                                builder.field("status", status.getInReplyToStatusId());
                                if (status.getInReplyToUserId() != -1) {
                                    builder.field("user_id", status.getInReplyToUserId());
                                    builder.field("user_screen_name", status.getInReplyToScreenName());
                                }
                                builder.endObject();
                            }

                            if (status.getHashtagEntities() != null) {
                                builder.startArray("hashtag");
                                for (HashtagEntity hashtag : status.getHashtagEntities()) {
                                    builder.startObject();
                                    builder.field("text", hashtag.getText());
                                    builder.field("start", hashtag.getStart());
                                    builder.field("end", hashtag.getEnd());
                                    builder.endObject();
                                }
                                builder.endArray();
                            }
                            if (status.getContributors() != null && status.getContributors().length > 0) {
                                builder.array("contributor", status.getContributors());
                            }
                            if (status.getGeoLocation() != null) {
                                if (geoAsArray) {
                                    builder.startArray("location");
                                    builder.value(status.getGeoLocation().getLongitude());
                                    builder.value(status.getGeoLocation().getLatitude());
                                    builder.endArray();
                                } else {
                                    builder.startObject("location");
                                    builder.field("lat", status.getGeoLocation().getLatitude());
                                    builder.field("lon", status.getGeoLocation().getLongitude());
                                    builder.endObject();
                                }
                            }
                            if (status.getPlace() != null) {
                                builder.startObject("place");
                                builder.field("id", status.getPlace().getId());
                                builder.field("name", status.getPlace().getName());
                                builder.field("type", status.getPlace().getPlaceType());
                                builder.field("full_name", status.getPlace().getFullName());
                                builder.field("street_address", status.getPlace().getStreetAddress());
                                builder.field("country", status.getPlace().getCountry());
                                builder.field("country_code", status.getPlace().getCountryCode());
                                builder.field("url", status.getPlace().getURL());
                                builder.endObject();
                            }
                            if (status.getURLEntities() != null) {
                                builder.startArray("link");
                                for (URLEntity url : status.getURLEntities()) {
                                    if (url != null) {
                                        builder.startObject();
                                        if (url.getURL() != null) {
                                            builder.field("url", url.getURL());
                                        }
                                        if (url.getDisplayURL() != null) {
                                            builder.field("display_url", url.getDisplayURL());
                                        }
                                        if (url.getExpandedURL() != null) {
                                            builder.field("expand_url", url.getExpandedURL());
                                        }
                                        builder.field("start", url.getStart());
                                        builder.field("end", url.getEnd());
                                        builder.endObject();
                                    }
                                }
                                builder.endArray();
                            }

                            builder.startObject("user");
                            builder.field("id", status.getUser().getId());
                            builder.field("name", status.getUser().getName());
                            builder.field("screen_name", status.getUser().getScreenName());
                            builder.field("location", status.getUser().getLocation());
                            builder.field("description", status.getUser().getDescription());
                            builder.field("profile_image_url", status.getUser().getProfileImageURL());
                            builder.field("profile_image_url_https", status.getUser().getProfileImageURLHttps());

                            builder.endObject();

                            builder.endObject();
                            if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
                                bulkProcessor.add(Requests.indexRequest(indexName).type(typeName).id(Long.toString(status.getId())).source(builder));
                            }
                        }
                    }

                } catch (Exception e) {
                    logger.warn("failed to construct index request", e);
                }
            } else {
                logger.debug("river is closing. ignoring tweet [{}]", status.getId());
            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            if (riverStatus != RiverStatus.STOPPED && riverStatus != RiverStatus.STOPPING) {
                if (statusDeletionNotice.getStatusId() != -1) {
                    bulkProcessor.add(Requests.deleteRequest(indexName).type(typeName).id(Long.toString(statusDeletionNotice.getStatusId())));
                }
            } else {
                logger.debug("river is closing. ignoring deletion of tweet [{}]", statusDeletionNotice.getStatusId());
            }
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            logger.info("received track limitation notice, number_of_limited_statuses {}", numberOfLimitedStatuses);
        }

        @Override
        public void onException(Exception ex) {
            logger.warn("stream failure, restarting stream...", ex);
            threadPool.generic().execute(new Runnable() {
                @Override
                public void run() {
                    reconnect();
                }
            });
        }
    }

    private class UserStreamHandler extends UserStreamAdapter {

    private final StatusHandler statusHandler = new StatusHandler();

        @Override
        public void onException(Exception ex) {
            statusHandler.onException(ex);
        }

        @Override
        public void onStatus(Status status) {
            statusHandler.onStatus(status);
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            statusHandler.onDeletionNotice(statusDeletionNotice);
        }
    }

    public enum RiverStatus {
        UNKNOWN,
        INITIALIZED,
        STARTING,
        RUNNING,
        STOPPING,
        STOPPED;
    }
}


text:
package util;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 * Created by Guilherme on 13/03/2016.
 */
public class AlertBox {

    public void display(String title, String message){
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(title);
        window.setMinWidth(250);

        Label label = new Label(message);
        Button closeBtn = new Button("Close");
        closeBtn.setOnAction(e -> window.close());

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20));
        layout.getChildren().addAll(label, closeBtn);
        layout.setAlignment(Pos.CENTER);

        window.setScene(new Scene(layout));
        window.showAndWait();
    }
}

redpajama_set_name: RedPajamaGithub


text:
import React from 'react';
import { render } from 'react-dom';
import { Provider, connect } from 'react-redux';
import { Route, Router } from 'react-router-dom';
import { GlobalStyles } from 'netlify-cms-ui-default';
import { I18n } from 'react-polyglot';

import { store } from './redux';
import { history } from './routing/history';
import { loadConfig } from './actions/config';
import { authenticateUser } from './actions/auth';
import { getPhrases } from './lib/phrases';
import { selectLocale } from './reducers/config';
import { ErrorBoundary } from './components/UI';
import App from './components/App/App';
import './components/EditorWidgets';
import './mediaLibrary';
import 'what-input';

const ROOT_ID = 'nc-root';

function TranslatedApp({ locale, config }) {
  return (
    <I18n locale={locale} messages={getPhrases(locale)}>
      <ErrorBoundary showBackup config={config}>
        <Router history={history}>
          <Route component={App} />
        </Router>
      </ErrorBoundary>
    </I18n>
  );
}

function mapDispatchToProps(state) {
  return { locale: selectLocale(state.config), config: state.config };
}

const ConnectedTranslatedApp = connect(mapDispatchToProps)(TranslatedApp);

function bootstrap(opts = {}) {
  const { config } = opts;

  /**
   * Log the version number.
   */
  if (typeof NETLIFY_CMS_CORE_VERSION === 'string') {
    console.log(`netlify-cms-core ${NETLIFY_CMS_CORE_VERSION}`);
  }

  /**
   * Get DOM element where app will mount.
   */
  function getRoot() {
    /**
     * Return existing root if found.
     */
    const existingRoot = document.getElementById(ROOT_ID);
    if (existingRoot) {
      return existingRoot;
    }

    /**
     * If no existing root, create and return a new root.
     */
    const newRoot = document.createElement('div');
    newRoot.id = ROOT_ID;
    document.body.appendChild(newRoot);
    return newRoot;
  }

  /**
   * Dispatch config to store if received. This config will be merged into
   * config.yml if it exists, and any portion that produces a conflict will be
   * overwritten.
   */
  store.dispatch(
    loadConfig(config, function onLoad() {
      store.dispatch(authenticateUser());
    }),
  );

  /**
   * Create connected root component.
   */
  function Root() {
    return (
      <>
        <GlobalStyles />
        <Provider store={store}>
          <ConnectedTranslatedApp />
        </Provider>
      </>
    );
  }

  /**
   * Render application root.
   */
  render(<Root />, getRoot());
}

export default bootstrap;


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

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

import org.activiti.engine.ActivitiException;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.idm.api.Group;
import org.activiti.idm.api.GroupQuery;
import org.activiti.idm.api.NativeGroupQuery;
import org.activiti.idm.api.NativeUserQuery;
import org.activiti.idm.api.User;
import org.activiti.idm.api.UserQuery;
import org.activiti.idm.engine.impl.IdmIdentityServiceImpl;
import org.activiti.ldap.impl.LDAPGroupQueryImpl;
import org.activiti.ldap.impl.LDAPUserQueryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LDAPIdentityServiceImpl extends IdmIdentityServiceImpl {

  private static Logger logger = LoggerFactory.getLogger(LDAPIdentityServiceImpl.class);

  protected ProcessEngineConfigurationImpl processEngineConfiguration;
  protected LDAPConfigurator ldapConfigurator;
  protected LDAPGroupCache ldapGroupCache;

  public LDAPIdentityServiceImpl(ProcessEngineConfigurationImpl processEngineConfiguration,
      LDAPConfigurator ldapConfigurator, LDAPGroupCache ldapGroupCache) {

    this.processEngineConfiguration = processEngineConfiguration;
    this.ldapConfigurator = ldapConfigurator;
    this.ldapGroupCache = ldapGroupCache;
  }

  @Override
  public UserQuery createUserQuery() {
    return new LDAPUserQueryImpl(ldapConfigurator);
  }

  @Override
  public GroupQuery createGroupQuery() {
    return new LDAPGroupQueryImpl(ldapConfigurator, ldapGroupCache);
  }

  @Override
  public boolean checkPassword(String userId, String password) {
    return executeCheckPassword(userId, password);
  }

  @Override
  public User newUser(String userId) {
    throw new ActivitiException("LDAP identity service doesn't support creating a new user");
  }

  @Override
  public void saveUser(User user) {
    throw new ActivitiException("LDAP identity service doesn't support saving an user");
  }

  @Override
  public NativeUserQuery createNativeUserQuery() {
    throw new ActivitiException("LDAP identity service doesn't support native querying");
  }

  @Override
  public void deleteUser(String userId) {
    throw new ActivitiException("LDAP identity service doesn't support deleting an user");
  }

  @Override
  public Group newGroup(String groupId) {
    throw new ActivitiException("LDAP identity service doesn't support creating a new group");
  }

  @Override
  public NativeGroupQuery createNativeGroupQuery() {
    throw new ActivitiException("LDAP identity service doesn't support native querying");
  }

  @Override
  public void saveGroup(Group group) {
    throw new ActivitiException("LDAP identity service doesn't support saving a group");
  }

  @Override
  public void deleteGroup(String groupId) {
    throw new ActivitiException("LDAP identity service doesn't support deleting a group");
  }

  protected boolean executeCheckPassword(final String userId, final String password) {
    // Extra password check, see http://forums.activiti.org/comment/22312
    if (password == null || password.length() == 0) {
      throw new ActivitiException("Null or empty passwords are not allowed!");
    }

    try {
      LDAPTemplate ldapTemplate = new LDAPTemplate(ldapConfigurator);
      return ldapTemplate.execute(new LDAPCallBack<Boolean>() {

        public Boolean executeInContext(InitialDirContext initialDirContext) {

          if (initialDirContext == null) {
            return false;
          }

          // Do the actual search for the user
          String userDn = null;
          try {

            String searchExpression = ldapConfigurator.getLdapQueryBuilder().buildQueryByUserId(ldapConfigurator, userId);
            String baseDn = ldapConfigurator.getUserBaseDn() != null ? ldapConfigurator.getUserBaseDn() : ldapConfigurator.getBaseDn();
            NamingEnumeration<?> namingEnum = initialDirContext.search(baseDn, searchExpression, createSearchControls());

            while (namingEnum.hasMore()) { // Should be only one
              SearchResult result = (SearchResult) namingEnum.next();
              userDn = result.getNameInNamespace();
            }
            namingEnum.close();

          } catch (NamingException ne) {
            logger.info("Could not authenticate user " + userId + " : " + ne.getMessage(), ne);
            return false;
          }

          // Now we have the user DN, we can need to create a connection it ('bind' in ldap lingo) to check if the user is valid
          if (userDn != null) {
            InitialDirContext verificationContext = null;
            try {
              verificationContext = LDAPConnectionUtil.createDirectoryContext(ldapConfigurator, userDn, password);
            } catch (ActivitiException e) {
              // Do nothing, an exception will be thrown if the login fails
            }

            if (verificationContext != null) {
              LDAPConnectionUtil.closeDirectoryContext(verificationContext);
              return true;
            }
          }

          return false;

        }
      });

    } catch (ActivitiException e) {
      logger.info("Could not authenticate user : " + e);
      return false;
    }
  }

  protected SearchControls createSearchControls() {
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
    return searchControls;
  }

  public LDAPGroupCache getLdapGroupCache() {
    return ldapGroupCache;
  }

  public void setLdapGroupCache(LDAPGroupCache ldapGroupCache) {
    this.ldapGroupCache = ldapGroupCache;
  }
}


text:
package io.github.giovibal.mqtt;

import io.vertx.core.json.JsonObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by giova_000 on 23/02/2015.
 */
public class ConfigParser {

    private int port;
    private boolean wsEnabled;
    private String wsSubProtocols;
    private boolean retainSupport;
    private String authenticatorAddress;
    private boolean securityEnabled;
    private String tlsKeyPath;
    private String tlsCertPath;
    private int socketIdleTimeout;

    private static final int IDLE_TIMEOUT_SECONDS = 120;

    public ConfigParser() { }

    public ConfigParser(JsonObject conf) {
        parse(conf);
    }
    public void parse(JsonObject conf) {
        port = conf.getInteger("tcp_port", 1883);
        wsEnabled = conf.getBoolean("websocket_enabled", false);
        wsSubProtocols = conf.getString("websocket_subprotocols", "mqtt,mqttv3.1");
        retainSupport = conf.getBoolean("retain_support", true);

        authenticatorAddress = conf.getString("authenticator_address");
        securityEnabled = authenticatorAddress!=null && authenticatorAddress.trim().length()>0;

        JsonObject tls = conf.getJsonObject("tls", new JsonObject());
        tlsKeyPath = tls.getString("keyPath");
        tlsCertPath = tls.getString("certPath");
        socketIdleTimeout = conf.getInteger("socket_idle_timeout", IDLE_TIMEOUT_SECONDS);
    }

    public int getPort() {
        return port;
    }

    public boolean isWsEnabled() {
        return wsEnabled;
    }

    public String getWsSubProtocols() {
        return wsSubProtocols;
    }

    public String getAuthenticatorAddress() {
        return authenticatorAddress;
    }

    public boolean isSecurityEnabled() {
        return securityEnabled;
    }

    public String getTlsKeyPath() {
        return tlsKeyPath;
    }

    public String getTlsCertPath() {
        return tlsCertPath;
    }

    public boolean isTlsEnabled() {
        String keyPath = getTlsKeyPath();
        String certPath = getTlsCertPath();
        boolean ret = keyPath!=null && keyPath.trim().length()>0 && certPath!=null && certPath.trim().length()>0;
        return ret;
    }

    public boolean isRetainSupport() {
        return retainSupport;
    }

    public List<String> getFeatures() {
        List<String> ret = new ArrayList<>();
        if(wsEnabled)
            ret.add("websocket");
        if(securityEnabled)
            ret.add("security");
        if(!retainSupport)
            ret.add("retain_disabled");
        return ret;
    }
    public String getFeatursInfo() {
        StringBuilder ret = new StringBuilder();
        List<String> features = getFeatures();
        for(String f : features) {
            ret.append(f).append(",");
        }
        String s = ret.toString();
        int idx = s.lastIndexOf(',');
        if(idx>=0)
            s = s.substring(0, idx);
        return s;
    }

    public int getSocketIdleTimeout() {
        return socketIdleTimeout;
    }
}


text:
package org.dartlang.vm.service.consumer;

// This is a generated file.

import org.dartlang.vm.service.element.InstanceSet;

@SuppressWarnings({"WeakerAccess", "unused"})
public interface InstanceSetConsumer extends Consumer {

  void received(InstanceSet response);
}


text:
{-
Copyright James d'Arcy 2011

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of James d'Arcy nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-}

module Hastur.Image
  ( createWxImage,
    isRenderable
  ) where

import qualified Data.ByteString as B
import Data.Int
import qualified Data.Set as Set
import Data.Word
import Graphics.UI.WX
import Graphics.UI.WXCore

import Hastur.Types

import Data.Dicom
import Data.Dicom.Accessor
import Data.Dicom.Tag
import Data.Dicom.UID

type UidSet = Set.Set UID

--
createWxImage :: Int32 -> DicomObject -> Maybe (IO (Image ()))
createWxImage frameIdx dcm =
  getColumns dcm >>= \nX ->
  getRows dcm >>= \nY ->
  getPixelData dcm >>= \pixelData ->
  Just (wxImageFromPixels frameIdx nX nY pixelData)

--
isRenderable :: DicomObject -> Bool
isRenderable dcm = do
  let maybeUid = getSopClassUid dcm
  case maybeUid of
    Nothing  -> False
    Just uid -> Set.member uid renderableSopClassUidSet

--
renderableSopClassUidSet :: UidSet
renderableSopClassUidSet =
  Set.insert cT_IMAGE_STORAGE .
  Set.insert mR_IMAGE_STORAGE .
  Set.insert eNHANCED_MR_IMAGE_STORAGE .
  Set.insert pOSITRON_EMISSION_TOMOGRAPHY_IMAGE_STORAGE .
  Set.insert uLTRASOUND_IMAGE_STORAGE
  $ Set.empty

--
wxImageFromPixels :: Int32 -> Word16 -> Word16 -> B.ByteString -> IO (Image ())
wxImageFromPixels frame nX nY pixels = do
  let intNx = fromIntegral nX
  let intNy = fromIntegral nY
  let rgbImage = map (\x -> rgb x x x) (extractInt16s (fromIntegral frame) (intNx*intNy*2) pixels)
  imageCreateFromPixels (sz intNx intNy) rgbImage


text:
using System;
using System.Collections.Generic;
using System.Linq;
using Bytes2you.DataAccess.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bytes2you.DataAccess.UnitTests.Testing.Mocks
{
    public class UnitOfWorkMock<TDataEntity, TId> : IUnitOfWork<TDataEntity, TId>
        where TDataEntity : class, IDataEntity<TId>
    {
        private readonly List<TId> getByIdCalls;
        private readonly List<TDataEntity> getByIdReturnValues;
        private readonly List<TDataEntity[]> getAllReturnValues;
        private readonly List<int> getCountReturnValues;
        private readonly List<TDataEntity> registerNewCalls;
        private readonly List<TDataEntity> registerDirtyCalls;
        private readonly List<TDataEntity> registerRemovedCalls;

        public UnitOfWorkMock()
        {
            this.getByIdCalls = new List<TId>();
            this.getByIdReturnValues = new List<TDataEntity>();
            this.getAllReturnValues = new List<TDataEntity[]>();
            this.getCountReturnValues = new List<int>();
            this.registerNewCalls = new List<TDataEntity>();
            this.registerDirtyCalls = new List<TDataEntity>();
            this.registerRemovedCalls = new List<TDataEntity>();
        }

        public IEnumerable<TDataEntity[]> GetAllReturnValues
        {
            get
            {
                return this.getAllReturnValues;
            }
        }

        public IEnumerable<TDataEntity> RegisterNewCalls
        {
            get
            {
                return this.registerNewCalls;
            }
        }

        public IEnumerable<TDataEntity> RegisterDirtyCalls
        {
            get
            {
                return this.registerDirtyCalls;
            }
        }

        public IEnumerable<TDataEntity> RegisterRemovedCalls
        {
            get
            {
                return this.registerRemovedCalls;
            }
        }

        public TDataEntity GetById(TId id)
        {
            TDataEntity result = (TDataEntity)Activator.CreateInstance(typeof(TDataEntity));

            this.getByIdCalls.Add(id);
            this.getByIdReturnValues.Add(result);

            return result;
        }

        public TDataEntity[] GetAll()
        {
            TDataEntity[] result = new TDataEntity[5];

            for (int i = 0; i < result.Length; i++)
            {
                TDataEntity entity = (TDataEntity)Activator.CreateInstance(typeof(TDataEntity));
                result[i] = entity;
            }

            this.getAllReturnValues.Add(result);

            return result;
        }

        public int GetCount()
        {
            int result = DateTime.Now.Second;

            this.getCountReturnValues.Add(result);

            return result;
        }

        public void RegisterNew(TDataEntity entity)
        {
            this.registerNewCalls.Add(entity);
        }

        public void RegisterDirty(TDataEntity entity)
        {
            this.registerDirtyCalls.Add(entity);
        }

        public void RegisterRemoved(TDataEntity entity)
        {
            this.registerRemovedCalls.Add(entity);
        }

        public void Commit()
        {
            this.OnCommitting();
        }

        public void Rollback()
        {
            throw new NotImplementedException();
        }

        public void AssertGetByIdCalls(params TId[] calls)
        {
            CollectionAssert.AreEqual(this.getByIdCalls, calls);
        }

        public void AssertGetByIdReturnValues(params TDataEntity[] returnValues)
        {
            CollectionAssert.AreEqual(this.getByIdReturnValues, returnValues);
        }

        public void AssertGetAllReturnValues(params IEnumerable<TDataEntity>[] returnValues)
        {
            CollectionAssert.AreEqual(this.getAllReturnValues, returnValues);
        }

        public void AssertGetCountReturnValues(params int[] returnValues)
        {
            CollectionAssert.AreEqual(this.getCountReturnValues, returnValues);
        }

        public void AssertRegisterNewCalls(params TDataEntity[] calls)
        {
            CollectionAssert.AreEqual(this.registerNewCalls, calls);
        }

        public void AssertRegisterDirtyCalls(params TDataEntity[] calls)
        {
            CollectionAssert.AreEqual(this.registerDirtyCalls, calls);
        }

        public void AssertRegisterRemovedCalls(params TDataEntity[] calls)
        {
            CollectionAssert.AreEqual(this.registerRemovedCalls, calls);
        }

        public event EventHandler Committing;
        protected virtual void OnCommitting()
        {
            if (this.Committing != null)
            {
                this.Committing(this, EventArgs.Empty);
            }
        }
    }
}


text:
/* globals module */
/**
 * @module notificationsSubscriptionsAnonymousBatchClient
 * @description  Notifications Subscriptions Anonymous Batch Client provides an easy way to consume  Notifications REST API end-points. In order to obtain needed routes `notificationsSubscriptionsAnonymousBatchClient` uses `notificationsSubscriptionsAnonymousBatchRoute`.
 */

import { injectable, inject } from "inversify";
import { IQueryModel, IOptions } from '../../../common/contracts';;
import { ApiClient, IHttpResponse, httpTYPES } from '../../../httpApi';
import { NotificationsSubscriptionsAnonymousBatchRoute, TYPES as notificationsTypes } from './';
import { IAnonymousSubscription } from './contracts';

@injectable()
export class NotificationsSubscriptionsAnonymousBatchClient {

    get routeDefinition(): NotificationsSubscriptionsAnonymousBatchRoute {
        return this.notificationsSubscriptionsAnonymousBatchRoute;
    }

    constructor(
        @inject(notificationsTypes.NotificationsSubscriptionsAnonymousBatchRoute) protected notificationsSubscriptionsAnonymousBatchRoute: NotificationsSubscriptionsAnonymousBatchRoute,
        @inject(httpTYPES.ApiClient) protected apiClient: ApiClient
    ) { }

    /**
     * Returns a promise that is resolved once the create anonymous subscription action has been performed; this action creates new anonymous subscription resources.
     * @method
     * @param data AnonymousSubscription objects that need to be inserted into the system.
     * @returns A promise that is resolved once the create anonymous subscription action has been performed.
     * @example notificationsSubscriptionsAnonymousBatchClient.create([{
                    channel: '<channel-name>',
                    registrationId: '<registration-id>' ž
                }])
                .then(function (data) {
                    // perform success action here
                },
                 function (response, status, headers, config) {
                     // perform error handling here
                });
     */
    create(data: IAnonymousSubscription[]): PromiseLike<IHttpResponse<IAnonymousSubscription[]>> {
        return this.apiClient.post(this.routeDefinition.create(), this.routeDefinition.createParams(data));
    }

    /**
     * Returns a promise that is resolved once the remove action has been performed. This action will remove anonymous subscription resources from the system if successfully completed.
     * @method
     * @param ids The subscription ids which uniquely identify AnonymousSubscription resources that need to be deleted.
     * @returns a promise that is resolved once the remove action has been performed.
     * @example notificationsSubscriptionsAnonymousBatchClient.remove(subscriptionIds)
                   .then(function (data) {
                       // perform success action here
                   },
                    function (response, status, headers, config) {
                        // perform error handling here
                   });
    */
    remove(ids: string[]): PromiseLike<IHttpResponse<void>> {
        return this.apiClient.delete<void>(this.notificationsSubscriptionsAnonymousBatchRoute.delete(), undefined, ids);
    }

    /**
     * Returns a promise that is resolved once the update anonymous subscriptions action has been performed; this action updates specified anonymous subscription resources.
     * @method
     * @param data AnonymousSubscription objects used to update specified AnonymousSubscription resources.
     * @returns A promise that is resolved once the update anonymous subscriptions action has been performed.
     * @example notificationsSubscriptionsAnonymousBatchClient.update(subscriptions)
                    .then(function (data) {
                        // perform success action here
                    },
                     function (response, status, headers, config) {
                         // perform error handling here
                    });
     */
    update(data: IAnonymousSubscription[]): PromiseLike<IHttpResponse<void>> {
        return this.apiClient.put<void>(this.routeDefinition.update(), this.routeDefinition.updateParams(data));
    }
}

/**
 * @overview
 ***Notes:**
 - Refer to the [REST API documentation](https://github.com/Baasic/baasic-rest-api/wiki) for detailed information about available Baasic REST API end-points.
 - All end-point objects are transformed by the associated route service.
 */


text:
<?php

namespace GW2Treasures\GW2Api\V2\Localization;

use GW2Treasures\GW2Api\V2\IEndpoint;

interface ILocalizedEndpoint extends IEndpoint {
    /**
     * Change the language of this endpoint.
     *
     * @param string $language
     * @return $this
     */
    public function lang( $language );

    /**
     * Get the current language.
     *
     * @return string
     */
    public function getLang();
}


text:
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef NS_SMILNULLTYPE_H_
#define NS_SMILNULLTYPE_H_

#include "nsISMILType.h"

class nsSMILNullType : public nsISMILType
{
public:
  // Singleton for nsSMILValue objects to hold onto.
  static nsSMILNullType sSingleton;

protected:
  // nsISMILType Methods
  // -------------------
  virtual void Init(nsSMILValue& aValue) const {}
  virtual void Destroy(nsSMILValue& aValue) const {}
  virtual nsresult Assign(nsSMILValue& aDest, const nsSMILValue& aSrc) const;

  // The remaining methods should never be called, so although they're very
  // simple they don't need to be inline.
  virtual bool     IsEqual(const nsSMILValue& aLeft,
                           const nsSMILValue& aRight) const;
  virtual nsresult Add(nsSMILValue& aDest, const nsSMILValue& aValueToAdd,
                       uint32_t aCount) const;
  virtual nsresult ComputeDistance(const nsSMILValue& aFrom,
                                   const nsSMILValue& aTo,
                                   double& aDistance) const;
  virtual nsresult Interpolate(const nsSMILValue& aStartVal,
                               const nsSMILValue& aEndVal,
                               double aUnitDistance,
                               nsSMILValue& aResult) const;

private:
  // Private constructor & destructor: prevent instances beyond my singleton,
  // and prevent others from deleting my singleton.
  nsSMILNullType()  {}
  ~nsSMILNullType() {}
};

#endif // NS_SMILNULLTYPE_H_


text:
$(document).foundation();

var _gaq = _gaq || [];
_gaq.push(
  ['_setAccount', 'UA-2195009-2'],
  ['_trackPageview'],
  ['b._setAccount', 'UA-2195009-27'],
  ['b._trackPageview']
);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
var _kmq = _kmq || [];
!function() {

var _kmq = _kmq || [];
var _kmk = _kmk || "d945f04ff5e68057c85f5323b46f185efb3826b3";
function _kms(u){
  setTimeout(function(){
    var d = document, f = d.getElementsByTagName('script')[0],
    s = d.createElement('script');
    s.type = 'text/javascript'; s.async = true; s.src = u;
    f.parentNode.insertBefore(s, f);
  }, 1);
}
_kms('//i.kissmetrics.com/i.js');
_kms('//doug1izaerwt3.cloudfront.net/' + _kmk + '.1.js');

}();

var mySVGsToInject = document.querySelectorAll('img.inject-me');

var likes = new window.Likes();
likes.populateLikesInPage();
likes.setupLike();

SVGInjector(mySVGsToInject);
var $searchInput = $('input[type="search"]')
var setupFilterable = function($current, $links, updateMethod) {
  $links.on('click', function(e) {
    e.preventDefault();
    var $el = $(e.currentTarget);
    var type = $el.data().type;
    $current.text($el.text());
    updateMethod(type);
    $links.each(function() {
      var $link = $(this);
      if (typeof($link.data().hideActive) === 'undefined') {
        if ($link.data().type === type) {
          $link.addClass('is-active');
        } else {
          $link.removeClass('is-active');
        }
      } else {
        if ($link.data().type === type) {
          $link.addClass('hide');
        } else {
          $link.removeClass('hide');
        }
      }
    });
  });
}
var params = getParams();
if($searchInput.is('*')) {
  if(params.q) {
    $('#bb-search-bar').removeClass('is-hidden').show();
    $searchInput.focus();
  }
  window.search = new Search({
    input: $('input[type="search"]'),
    searchContainer: $('#search-results-container .card-container'),
    kitContainer: $('#search-results-container .kit-container'),
    initialQuery: params.q,
    onSearch: function(term, filter, sort, results, kitResults) {
      if(term.length > 0 || filter !== 'all' || sort !== 'newest') {
        $('#main-results-container').hide();
        $('#search-results-container').show();
        $('#search-results-container').foundation();
        $('#result-count').text(results.length);
      } else {
        $('#main-results-container').show();
        $('#search-results-container').hide();
        $('#result-count').text(results.length);
      }
      likes.populateLikesInPage();
    }
  });
  var $currentSort = $('[data-sort-current]');
  var $sortLinks = $('[data-sort]');
  setupFilterable($currentSort, $sortLinks, window.search.setSort.bind(window.search));

  var $currentFilter = $('[data-filter-current]');
  var $filterLinks = $('[data-filter]');
  setupFilterable($currentFilter, $filterLinks, window.search.setFilter.bind(window.search));
  $('#bb-search-bar').on('close.zf.trigger', function() {
    $searchInput.val('');
    window.search.updateSearch();
  }).on('toggle.zf.trigger', function() {
    setTimeout( () => { $searchInput.focus();window.search.updateSearch();}, 1)
  });
}

function getParams() {
  var search = location.search.substring(1);
  return search?JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}',
                   function(key, value) { return key===""?value:decodeURIComponent(value) }):{}
}

function toggleCode() {
  $('#codeBoxSCSS').toggleClass('is-active');
  $('#scssToggle').toggleClass('is-active');
  $('#codeBoxCSS').toggleClass('is-active');
  $('#cssToggle').toggleClass('is-active');
}

$('#scssToggle').click(function(){
  if ($('#cssToggle').hasClass('is-active')) {
    toggleCode();
  }
});

$('#cssToggle').click(function(){
  if ($('#scssToggle').hasClass('is-active')) {
    toggleCode();
  }
});

var toggleHTML = $('[data-toggle-HTML]');
var toggleSCSS = $('[data-toggle-SCSS]');
var toggleJS   = $('[data-toggle-JS]');

// toggles trigger for the code boxes
toggleHTML.click(function(e) {
  if( toggleSCSS.hasClass('is-active') || toggleJS.hasClass('.is-active') ) {
    $(this).toggleClass('is-active');
    $('#codeBoxHTML').toggleClass('is-active');
  }
  e.preventDefault();
});

toggleSCSS.click(function(e) {
  if( toggleHTML.hasClass('is-active') || toggleJS.hasClass('.is-active') ) {
    $(this).toggleClass('is-active');
    $('#codeBoxSCSS').toggleClass('is-active');
  }
  e.preventDefault();
});

toggleSCSS.click(function(e) {
  if( toggleSCSS.hasClass('is-active') || toggleJS.hasClass('.is-active') ) {
    $(this).toggleClass('is-active');
    $('#codeBoxJS').toggleClass('is-active');
  }
  e.preventDefault();
});

var socialName = $('meta[name="og:title"]').prop('content');
var socialImage = $('meta[name="og:image"]').prop('content');
$('#bb-social').jsSocials({
  showLabel: false,
  showCount: false,
  text: "Download this " + socialName + " from Foundation Building Blocks",
  shares: [ { share: "twitter", via: "ZURBFoundation" }, "facebook", {share: "pinterest", media: socialImage, text: socialName + " from Foundation Building Blocks"}]
});

var setCalloutPosition = function(moveOver) {
  localStorage.setItem('course-callout-shrunk', '' + moveOver);
  $('.course-callout-alert').toggleClass('is-moved-over', moveOver);
}

$('[data-toggle-callout]').click(function() {
  setCalloutPosition(!$('.course-callout-alert').hasClass('is-moved-over'));
});

if(localStorage.getItem('course-callout-shrunk') === 'true') {
  $('.course-callout-alert').addClass('is-moved-over no-animate');
  setTimeout(function() {
    $('.course-callout-alert').removeClass('no-animate');
  }, 1);
}

var $footer = $('#footer');
var $window = $(window);
var $callout = $('.course-callout-alert');

if($callout.is('*')) {
  $(window).on("load scroll", function() {
      var footerOffset = $footer.offset().top;
      var myScrollPosition = $(this).scrollTop();
      var windowHeight = $window.height();
      var footerHeight = $footer.outerHeight();

      if ((myScrollPosition + windowHeight - footerHeight) > footerOffset) {
        $callout.addClass('absolute');
      } else {
        $callout.removeClass('absolute');
      }
  });
}


text:
require 'rubygems'

# Pre-declare Base class, so we can load derived classes for class factory
module PackageManager
  class Base; end
end

require 'package_manager/apt'
require 'package_manager/dpkg'
require 'package_manager/fink'
require 'package_manager/port'
require 'package_manager/rpm'
require 'package_manager/yum'

module PackageManager

  class Base

    attr_reader :name

    KNOWN_PACKAGE_MANAGERS = { 'apt'  => Apt,
                               'dpkg' => Dpkg,
                               'fink' => Fink,
                               'port' => Port,
                               'rpm'  => Rpm,
                               'yum'  => Yum }

    def self.load( package_manager = guess_package_manager )
      raise 'Unknown package manager' if KNOWN_PACKAGE_MANAGERS[ package_manager ].nil?
      KNOWN_PACKAGE_MANAGERS[ package_manager ].new
    end

    # Returns an array of matches
    def find_available( package_match )
      run_command( find_available_command, [ package_match ] ).split( "\n" )
    end

    # Returns an array of matches
    def find_installed( package_match )
      run_command( find_installed_command, [ package_match ] ).split( "\n" ).uniq
    end

    # Returns an array of file names
    def list_contents( package )
      run_command( list_contents_command, [ package ] ).split( "\n" )
    end

    # Returns true if the package installed successfully
    def install( package )
      exec_command( 'sudo ' + install_command, [ package ], { :echo_output => true } )
    end

    # Returns true if the package installed successfully
    def install_file( file )
      raise "The file '#{ file }' does not exist" if ! File.exist?( file )
      exec_command( 'sudo ' + install_file_command, [ file ], { :echo_output => true } )
    end

    # Returns true if the package uninstalled successfully
    def uninstall( package )
      exec_command( 'sudo ' + uninstall_command, [ package ], { :echo_output => true } )
    end

    # Returns the package name if found, or an empty string
    def provides( file )
      raise "The file '#{ file }' does not exist" if ! File.exist?( file )
      run_command( provides_command, [ file ] )
    end

    private

    def run_command( command, args, options = {} )
      full_command = "#{ command } #{ args.join( ' ' ) }"
      out = ''
      IO.popen( full_command ) do | pipe |
        pipe.each_line do | s |
          puts s if options[ :echo_output ]
          out << s
        end
      end

      out.chomp
    end

    def exec_command( command, args, options = {} )
      exec "#{ command } #{ args.join( ' ' ) }"
    end

    def self.guess_package_manager
      os = operating_system
      case os
      when 'linux'
        linux_package_manager
      when 'darwin'
        darwin_package_manager
      else
        raise "Unhandled operating system: '#{ os }'"
      end
    end

    def self.operating_system
      platforms = Gem.platforms
      raise 'Unexpected response from Gem.platforms'                       unless platforms.respond_to?( :size )
      raise 'Expected Array of 2 values'                                   unless platforms.size == 2
      raise 'Unexpected operating system object returned by Gem.platforms' unless platforms[ 1 ].respond_to?( :os )
      platforms[ 1 ].os
    end

    def self.linux_package_manager
      case
      when program_installed?( 'apt-get' )
        'apt'
      when program_installed?( 'yum' )
        'yum'
      when program_installed?( 'rpm' )
        'rpm'
      else
        raise "Unknown or missing package manager system"
      end
    end

    # TODO: Add brew
    def self.darwin_package_manager
      # Be opinionated and prefer Mac Ports if both it and Fink are installed
      case
      when program_installed?( 'port' )
        'port'
      when program_installed?( 'apt-get' )
        'fink'
      else
        raise "Unknown or missing package manager system"
      end
    end

    def self.program_installed?( program )
      `which '#{ program }'`
      $?.exitstatus == 0
    end

  end

end


text:
package gov.samhsa.consent2share.service.admin;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import gov.samhsa.consent2share.common.AuthenticatedUser;
import gov.samhsa.consent2share.common.UserContext;
import gov.samhsa.consent2share.domain.account.Users;
import gov.samhsa.consent2share.domain.account.UsersRepository;
import gov.samhsa.consent2share.domain.commondomainservices.EmailSender;
import gov.samhsa.consent2share.domain.patient.Patient;
import gov.samhsa.consent2share.domain.patient.PatientLegalRepresentativeAssociationRepository;
import gov.samhsa.consent2share.domain.patient.PatientRepository;
import gov.samhsa.consent2share.domain.staff.Staff;
import gov.samhsa.consent2share.domain.staff.StaffRepository;
import gov.samhsa.consent2share.hl7.dto.PixPatientDto;
import gov.samhsa.consent2share.infrastructure.DtoToDomainEntityMapper;
import gov.samhsa.consent2share.infrastructure.PixService;
import gov.samhsa.consent2share.infrastructure.security.AuthenticationFailedException;
import gov.samhsa.consent2share.pixclient.util.PixManagerBean;
import gov.samhsa.consent2share.service.dto.AdminProfileDto;
import gov.samhsa.consent2share.service.dto.LookupDto;
import gov.samhsa.consent2share.service.dto.PatientProfileDto;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.modelmapper.ModelMapper;
import org.springframework.security.crypto.password.PasswordEncoder;

@RunWith(MockitoJUnitRunner.class)
public class AdminServiceImplTest {

	@Mock
	private PatientRepository patientRepository;

	@Mock
	private PatientLegalRepresentativeAssociationRepository patientLegalRepresentativeAssociationRepository;

	@Mock
	private ModelMapper modelMapper;

	@Mock
	private UserContext userContext;

	@Mock
	private AdminProfileDtoToAdministratorMapper adminProfileDtoToAdministratorMapper;

	@Mock
	DtoToDomainEntityMapper<PatientProfileDto, Patient> patientProfileDtoToPatientMapper;

	@Mock
	private UsersRepository usersRepository;

	@Mock
	private PasswordEncoder passwordEncoder;

	@Mock
	private EmailSender emailSender;

	@Mock
	private StaffRepository administratorRepository;

	@Mock
	private PixService pixService;

	@InjectMocks
	private AdminServiceImpl sut;

	@Test
	public void testFindPatientProfileByUsername() {
		// Arrange
		final Staff admin = mock(Staff.class);
		when(administratorRepository.findByUsername(anyString())).thenReturn(
				admin);
		final AdminProfileDto expectedPAdminProfileDto = mock(AdminProfileDto.class);
		when(modelMapper.map(admin, AdminProfileDto.class)).thenReturn(
				expectedPAdminProfileDto);

		// Act
		AdminProfileDto result = sut.findAdminProfileByUsername(anyString());

		// Assert
		assertEquals(expectedPAdminProfileDto, result);
	}

	@Test
	public void testUpdateAdministrator_when_authentication_success()
			throws AuthenticationFailedException {
		final String username = "TheUsername";
		AuthenticatedUser authenticatedUser = mock(AuthenticatedUser.class);
		when(authenticatedUser.getUsername()).thenReturn(username);
		when(userContext.getCurrentUser()).thenReturn(authenticatedUser);

		Users user = mock(Users.class);
		when(user.getUsername()).thenReturn(username);
		when(user.getPassword()).thenReturn("hashedpassword");
		when(usersRepository.loadUserByUsername(anyString())).thenReturn(user);
		when(passwordEncoder.matches("rawpassword", "hashedpassword"))
				.thenReturn(true);

		// Arrange
		AdminProfileDto adminProfileDtoInput = mock(AdminProfileDto.class);
		Staff admin = mock(Staff.class);
		when(adminProfileDtoInput.getUsername()).thenReturn(username);
		when(adminProfileDtoInput.getPassword()).thenReturn("rawpassword");
		when(adminProfileDtoInput.getFirstName()).thenReturn("albert");
		when(adminProfileDtoInput.getLastName()).thenReturn("Smith");
		when(adminProfileDtoInput.getEmail()).thenReturn(
				"albert.smith@consent2share.com");
		when(adminProfileDtoToAdministratorMapper.map(adminProfileDtoInput))
				.thenReturn(admin);

		// Act
		sut.updateAdministrator(adminProfileDtoInput);

		// Assert
		verify(administratorRepository, times(1)).save(admin);
	}

	@Test
	public void testUpdateAdministrator_when_username_does_not_match_and_so_authentication_fails() {
		AuthenticatedUser authenticatedUser = mock(AuthenticatedUser.class);
		when(authenticatedUser.getUsername()).thenReturn("TheUsername");
		when(userContext.getCurrentUser()).thenReturn(authenticatedUser);
		Users user = mock(Users.class);
		when(user.getUsername()).thenReturn("AnotherUsername");

		// Arrange
		AdminProfileDto adminProfileDtoInput = mock(AdminProfileDto.class);
		when(adminProfileDtoInput.getUsername()).thenReturn("AnotherUsername");

		// Act
		try {
			sut.updateAdministrator(adminProfileDtoInput);
		} catch (AuthenticationFailedException e) {
			assertEquals(e.getMessage(),
					"Username does not match current active user.");
		}

		// Assert
		verify(administratorRepository, times(0)).save(any(Staff.class));
	}

	@Test
	public void testUpdateAdministrator_when_username_matches_but_password_is_wrong() {
		final String username = "TheUsername";
		AuthenticatedUser authenticatedUser = mock(AuthenticatedUser.class);
		when(authenticatedUser.getUsername()).thenReturn(username);
		when(userContext.getCurrentUser()).thenReturn(authenticatedUser);

		Users user = mock(Users.class);
		when(user.getUsername()).thenReturn(username);
		when(user.getPassword()).thenReturn("hashedpassword");
		when(usersRepository.loadUserByUsername(anyString())).thenReturn(user);
		when(passwordEncoder.matches("rawpassword", "hashedpassword"))
				.thenReturn(false);

		// Arrange
		AdminProfileDto adminProfileDtoInput = mock(AdminProfileDto.class);
		Staff admin = mock(Staff.class);
		when(adminProfileDtoInput.getUsername()).thenReturn(username);
		when(adminProfileDtoInput.getPassword()).thenReturn("rawpassword");
		when(adminProfileDtoInput.getFirstName()).thenReturn("albert");
		when(adminProfileDtoInput.getLastName()).thenReturn("Smith");
		when(adminProfileDtoInput.getEmail()).thenReturn(
				"albert.smith@consent2share.com");
		when(adminProfileDtoToAdministratorMapper.map(adminProfileDtoInput))
				.thenReturn(admin);

		// Act
		try {
			sut.updateAdministrator(adminProfileDtoInput);
		} catch (AuthenticationFailedException e) {
			assertEquals(e.getMessage(), "Password is incorrect.");
		}
		// Assert
		verify(administratorRepository, times(0)).save(admin);
	}

	@Test
	public void testUpdatePatient() {
		// Arrange
		final long id = 1L;
		final String username = "username";
		final String mrn = "mrn";
		final String eid = "eid";
		Patient patientCurrent = mock(Patient.class);
		Patient patientNew = mock(Patient.class);
		PatientProfileDto patientDto = new PatientProfileDto();
		patientDto.setId(id);
		patientDto.setBirthDate(new Date());
		LookupDto code = new LookupDto();
		code.setCode("M");
		patientDto.setMaritalStatusCode(code);
		when(patientProfileDtoToPatientMapper.map(patientDto)).thenReturn(
				patientNew);
		when(patientRepository.findOne(id)).thenReturn(patientCurrent);
		when(patientCurrent.getUsername()).thenReturn(username);
		when(patientCurrent.getMedicalRecordNumber()).thenReturn(mrn);
		PixManagerBean pixManagerBean = new PixManagerBean();
		pixManagerBean.setSuccess(true);
		when(pixService.updatePatient(any(PixPatientDto.class))).thenReturn(
				pixManagerBean);
		when(pixService.getEid(mrn)).thenReturn(eid);

		// Act
		sut.updatePatient(patientDto);

		// Assert
		verify(patientRepository, times(1)).save(patientNew);
		verify(pixService, times(1)).updatePatient(any(PixPatientDto.class));
	}
}


text: Go to the [WIKI](http://github.com/petermr/ContentMine/wiki/Home) describing the project


text:
dt_source _idx_common.tcl
dt_source _html.tcl

# ### ### ### ######### ######### #########
## API implementation

rename idx_postprocess {}
rename fmt_postprocess idx_postprocess

proc fmt_plain_text {text} {return {}}

proc fmt_index_begin {l t} {
    global la ti
    set la $l
    set ti $t
    return {}
}

proc fmt_key {text} {
    global key lk ch
    set lk $text
    set key($lk) {}
    set ch([F $lk]) .
    return {}
}

proc fmt_manpage {f l} {Ref [dt_fmap $f] $l}
proc fmt_url     {u l} {Ref $u           $l}

proc fmt_index_end {} {
    LoadKwid
    set lines {}
    if {![Get raw]} {
	BeginHeader ; Meta ; EndHeader
	BeginBody
    }
    BodyHeader ; Title ; Navbar
    BeginIndex ; Keys ; EndIndex
    if {![Get raw]} {
	EndBody
    }
    return [join $lines \n]
}

# ### ### ### ######### ######### #########
## Helper commands

proc Ref {r l} {
    global  key  lk
    lappend key($lk) $r $l
    return {}
}

proc F {text} {
    # Keep only alphanumeric, take first, uppercase
    # Returns nothing if input has no alphanumeric characters.
    return [string toupper [string index [regsub -all {[^a-zA-Z0-9]} $text {}] 0]]
}

proc LoadKwid {} {
    global kwid
    # Engine parameter - load predefined keyword anchors.
    set             ki [Get kwid]
    if {![llength  $ki]} return
    array set kwid $ki
    return
}

proc BeginHeader {} {
    global la ti
    upvar 1 lines lines
    lappend lines [markup {<!DOCTYPE html>}]
    lappend lines [markup ]
    lappend lines [ht_comment [c_provenance]]
    lappend lines [ht_comment "$la"]
    lappend lines [markup ]
    lappend lines "[markup <title>] $la [markup </title>]"
    return
}

proc Meta {} {
    # Engine parameter - insert 'meta'
    set meta [Get meta]
    if {$meta == {}} return
    upvar 1 lines lines
    lappend lines [markup $meta]
    return
}

proc EndHeader {} {
    upvar 1 lines lines
    lappend lines [markup ]
    return
}

proc BeginBody {} {
    upvar 1 lines lines
    lappend lines [markup ]
    return
}

proc BodyHeader {} {
    upvar 1 lines lines
    # Engine parameter - insert 'header'
    set header [Get header]
    if {$header == {}} return
    lappend map @TITLE@ [TheTitle]
    set header [string map $map $header]
    lappend lines [markup $header]
    return
}

proc TheTitle {} {
    global la ti
    set title ???
    if {($la != {}) && ($ti != {})} {
	set title "$la -- $ti"
    } elseif {$la != {}} {
	set title $la
    } elseif {$ti != {}} {
	set title $ti
    }
    return $title
}

proc Title {} {
    upvar 1 lines lines
    lappend lines "[markup
] [TheTitle] [markup
]"
    return
}

proc Navbar {} {
    global ch cnt dot
    upvar 1 lines lines

    set nav {}
    foreach c [lsort -dict [array names ch]] {
	set ref c[F $c];#[incr cnt]
	set ch($c) $ref
	lappend nav [ALink $ref $c]
    }

    lappend lines [markup "

"]
    lappend lines [join $nav $dot]
    lappend lines [markup
]
    return
}

proc BeginIndex {} {
    upvar 1 lines lines
    lappend lines [markup "

"]
    return
}

proc Keys {} {
    global key
    upvar 1 lines lines
    set lc {}
    set kwlist {}

    # For a good display we sort keywords in dictionary order.
    # We ignore their leading non-alphanumeric characters.
    set kwlist {}
    foreach kw [array names key] {
       set kwx [string trim [regsub -all {^[^a-zA-Z0-9]+} $kw {}]]
       lappend kwlist [list $kwx $kw]
    }
    foreach item [lsort -index 0 -dict $kwlist] {
       foreach {_ k} $item break
	set c [F $k] ; if {$lc != $c} { Section $c ; set lc $c }
	BeginKey   $k
	References $k
	EndKey
    }
    return
}

proc Section {c} {
    global ch
    upvar 1 lines lines
    lappend lines [markup {
}]
    lappend lines [markup "Keywords: $c"]
    lappend lines [markup
]
    return
}

proc BeginKey {k} {
    upvar 1 lines lines
    lappend lines [markup "
"]
    lappend lines [BeginColLeft][SetAnchor $k][markup 	]
    lappend lines [BeginColRight]
    return
}

proc EndKey {} {
    upvar 1 lines lines
    lappend lines [markup
]
    return
}

proc References {k} {
    global key dot
    upvar 1 lines lines
    set refs {}
    foreach {ref label} $key($k) {
	lappend refs [markup " $label "]
    }
    lappend lines [join $refs $dot]
    return
}

proc EndIndex {} {
    upvar 1 lines lines
    lappend lines [markup
]
    # Engine parameter - insert 'footer'
    set  footer [Get footer]
    if {$footer == {}} return
    lappend lines [markup "
"]
    lappend lines [markup $footer]
    return
}

proc EndBody {} {
    upvar 1 lines lines
    lappend lines [markup ""]
    return
}

proc ALink {dst label} {
    markup " $label "
}

proc BeginColLeft {} {
    return [markup {}]
}

proc BeginColRight {} {
    return [markup {}]
}

proc SetAnchor {text} {
    return [markup " $text "]
}

proc A {text} {
    set anchor [regsub -all {[^a-zA-Z0-9]} [string tolower $text] {_}]
    set anchor [regsub -all {__+} $anchor _]
    return $anchor
}

proc Anchor {text} {
    global kwid
    if {[info exists kwid($text)]} {
	return "\"$kwid($text)\""
    }
    return "\"[A $text]\""
}

proc Row {} {
    global even
    set res [expr {$even
    ? "\#doctools_idxeven"
    : "\#doctools_idxodd"}]
    Flip
    return $res
}

proc Flip {} {
    global even
    set    even [expr {1-$even}]
    return
}

# ### ### ### ######### ######### #########
## Engine state

# key  : string -> dict(ref -> label) "key formatting"
# ch   : string -> '.'                "key starting characters"
# lk   : string                       "last key"
# la   : string "index label"
# ti   : string "index title"
# cnt  : int
# kwid : string -> ...
# even : bool

global key  ; array set key  {}
global ch   ; array set ch   {}
global lk   ; set       lk   {}
global la   ; set       la   {}
global ti   ; set       ti   {}
global cnt  ; set       cnt  0
global kwid ; array set kwid {}
global even ; set       even 1
global dot  ; set dot   [markup { · }]

# ### ### ### ######### ######### #########
## Engine parameters

global    __var
array set __var {
    meta   {}
    header {}
    footer {}
    kwid   {}
    raw    0
}
proc Get               {varname}      {global __var ; return $__var($varname)}
proc idx_listvariables {}             {global __var ; return [array names __var]}
proc idx_varset        {varname text} {
    global __var
    if {![info exists __var($varname)]} {return -code error "Unknown engine variable \"$varname\""}
    set __var($varname) $text
    return
}

##
# ### ### ### ######### ######### #########