<commit_before>/* global Source, User, Group, SourceMetric, SourceTypes*/
// Importer.js - in api/services

"use strict";

const xlsx = require('xlsx');
const _ = require('lodash');
const fs = require('fs');
const request = require('request-promise');
const loadJsonFile = require('load-json-file');
const moment = require('moment');

module.exports = {
    importSources,
    importPeople,
    importGroups,
    importSourceMetrics
};

async function importSources() {

    function readWorksheet(worksheet, mappingsTable, mapFn = _.identity, filterFn = _.stubTrue) {
        function readSourceRow(r) {
            const sourceData = {};
            _.forEach(mappingsTable, (col, field) => {
                const cell = col + r;
                if (!_.isUndefined(worksheet[cell]))
                    sourceData[field] = worksheet[cell]['v'];
            });
            return sourceData;
        }

        const sources = [];
        for (let r = 2; ; r++) {
            const sourceData = readSourceRow(r);
            if (_.isEmpty(sourceData))
                break;
            const mappedSourceData = mapFn(sourceData);
            if (filterFn(mappedSourceData))
                sources.push(mappedSourceData);
        }
        return sources;
    }

    const scopusSourcesFileName = 'config/init/scopus_sources.xlsx';
    let journalsAndBookSeries = [], newConferences = [], oldConferences = [];
    if (fs.existsSync(scopusSourcesFileName)) {
        const workbook = xlsx.readFile(scopusSourcesFileName);
        const sheetNameList = workbook.SheetNames;

        const journalWorksheet = workbook.Sheets[sheetNameList[0]];
        const journalMappingsTable = {
            "title": 'B',
            "scopusId": 'A',
            "issn": 'C',
            "eissn": 'D',
            "type": 'T',
            "publisher": 'Z'
        };

        const mapJournal = (s) => {
            const sourceMappingTable = {
                'Book Series': SourceTypes.BOOKSERIES,
                'Journal': SourceTypes.JOURNAL
            };
            s.type = sourceMappingTable[s.type];
            return s;
        };

        const filterJournals = (s) => s.type;

        journalsAndBookSeries = readWorksheet(journalWorksheet, journalMappingsTable, mapJournal, filterJournals);

        const newConferencesWorksheet = workbook.Sheets[sheetNameList[1]];
        const newConferencesMappingsTable = {
            "title": 'B',
            "scopusId": 'A',
            "issn": 'D'
        };
        const mapConference = s => {
            s.type = SourceTypes.CONFERENCE;
            return s;
        };
        newConferences = readWorksheet(newConferencesWorksheet, newConferencesMappingsTable, mapConference);

        const oldConferencesWorksheet = workbook.Sheets[sheetNameList[2]];
        const oldConferencesMappingsTable = newConferencesMappingsTable;
        oldConferences = readWorksheet(oldConferencesWorksheet, oldConferencesMappingsTable, mapConference);
    }

    let books = [];
    const scopusSourcesFileName2 = 'config/init/scopus_book_sources.xlsx';
    if (fs.existsSync(scopusSourcesFileName2)) {
        const workbook2 = xlsx.readFile(scopusSourcesFileName2);
        const sheetNameList2 = workbook2.SheetNames;

        const bookWorksheet = workbook2.Sheets[sheetNameList2[0]];
        const bookMappingsTable = {
            "title": 'A',
            "isbn": 'C',
            "publisher": 'D',
            "year": 'E'
        };

        const mapBook = (s) => {
            s.type = 'book';
            return s;
        };

        books = readWorksheet(bookWorksheet, bookMappingsTable, mapBook);
    }

    const sources = _.union(journalsAndBookSeries, newConferences, oldConferences, books);

    sails.log.info('Inserting ' + sources.length + ' new sources');
    await Source.create(sources);
}

async function importPeople() {
    function userShouldBeUpdated(user, values) {
        const userFieldsToUpdate = [
            'jobTitle',
            'name',
            'surname'
        ];
        const toUpdate = userFieldsToUpdate.some(f => values[f] !== user[f]);
        return toUpdate;
    }

    const usersCreationCondition = sails.config.scientilla.mainInstituteImport.usersCreationCondition;
    const groupInsertionEnabled = false;
    sails.log.info('Import started');
    const url = sails.config.scientilla.mainInstituteImport.userImportUrl;
    const reqOptions = {
        uri: url,
        json: true
    };

    const people = await request(reqOptions);
    sails.log.info(people.length + ' entries found');
    const importTime = moment.utc().format();
    let numUsersInserted = 0, numUsersUpdated = 0, numGroupsInserted = 0;
    for (let [i, p] of people.entries()) {
        //groups are loaded in memory because waterline doesn't allow case-insensitive queries with postegres
        p.username = _.toLower(p.username);
        const allGroups = await Group.find();
        const groupsToBeInserted = p.groups.filter(g => !allGroups.some(g2 => _.toLower(g2.name) == _.toLower(g)));
        const groupsToSearch = allGroups.filter(g => p.groups.some(g2 => _.toLower(g2) == _.toLower(g.name))).map(g => g.name);
        if (groupInsertionEnabled && groupsToBeInserted.length) {
            const groupObjs = groupsToBeInserted.map(g => ({name: g}));
            sails.log.info('inserting groups: ' + groupsToBeInserted.join(', '));
            const newGroups = await Group.create(groupObjs);
            numGroupsInserted++;
            const newGroupsName = newGroups.map(g => g.name);
            groupsToSearch.push(...newGroupsName);
        }
        const groupSearchCriteria = {or: groupsToSearch.map(g => ({name: g}))};
        const groups = await Group.find(groupSearchCriteria).populate('members').populate('administrators');
        const criteria = {username: p.username};
        let user = await User.findOne(criteria);
        p.lastsynch = moment().utc().format();
        p.synchronized = true;
        if (user) {
            const u = await User.update(criteria, p);
            if (userShouldBeUpdated(user, p)) {
                sails.log.info(`Updating user ${p.username}`);
                numUsersUpdated++;
            }
        }
        else {
            if (p[usersCreationCondition.attribute] === usersCreationCondition.value) {
                sails.log.info(`Inserting user ${p.username}`);
                user = await User.createCompleteUser(p);
                numUsersInserted++;
            }
        }
        if (!user)
            continue;

        for (let g of groups) {
            g.members.add(user.id);
            await g.savePromise();
            const membershipCriteria = {user: user.id, group: g.id};
            const membership = await Membership.findOne(membershipCriteria);
            if (membership) {
                membership.lastsynch = moment.utc().format();
                membership.synchronized = true;
                membership.active = p.active;
                const m = await Membership.update(membershipCriteria, membership);
            }
            if (p.pi) {
                g.administrators.add(user.id);
                await g.savePromise();
            }
        }
    }
    const membershipUpdateCriteria = {lastsynch: {'<': importTime}, synchronized: true, active: true};
    const membershipDisabled = await Membership.update(membershipUpdateCriteria, {active: false});
    const numMembershipDisabled = membershipDisabled.length;
    const userUpdateCriteria = {lastsynch: {'<': importTime}, synchronized: true, active: true};
    const usersDisabled = await User.update(userUpdateCriteria, {active: false});
    const numUsersDisabled = usersDisabled.length;
    sails.log.info('Import finished');
    sails.log.info(`${numUsersInserted} users inserted`);
    sails.log.info(`${numUsersUpdated} users updated`);
    sails.log.info(`${numUsersDisabled} users disabled`);
    sails.log.info(`${numGroupsInserted} groups inserted`);
    sails.log.info(`${numMembershipDisabled} membership disabled`);
}

async function importGroups() {
    sails.log.info('Import started');
    const groupsPath = 'data/groups.json';
    const groupNames = await loadJsonFile(groupsPath);
    sails.log.info(groupNames.length + ' entries found');
    for (let groupName of groupNames) {
        const group = await Group.findOneByName(groupName);
        if (group)
            continue;
        await Group.create({name: groupName});
    }
    sails.log.info('Import finished');
}

async function importSourceMetrics(filename) {
    sails.log.info('Source metrics import started');

    const sourceIdentifiers = SourceMetric.sourceIdentifiers;

    const originCellCoord = 'B1';
    const yearCellCoord = 'D1';

    let recordsCount = 0;

    const cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
    const filePath = 'config/init/' + filename;
    if (fs.existsSync(filePath)) {
        const workbook = xlsx.readFile(filePath);
        const sheetNameList = workbook.SheetNames;

        const workSheet = workbook.Sheets[sheetNameList[0]];

        if (!workSheet[originCellCoord] || !workSheet[yearCellCoord])
            throw 'Invalid file format';

        const origin = workSheet[originCellCoord].v;
        const year = workSheet[yearCellCoord].v;

        const columnsMapping = {};

        for (const c of cols) {
            const colNameCell = workSheet[c + '2'];
            if (!colNameCell)
                break;

            columnsMapping[colNameCell.v] = c;
        }

        const keyColumns = Object.keys(columnsMapping).filter(c => sourceIdentifiers.includes(c));
        const valueColumns = Object.keys(columnsMapping).filter(c => !sourceIdentifiers.includes(c));

        let i = 3;
        while (true) {
            const workSheetRow = {};

            for (const colName in columnsMapping) {
                const col = columnsMapping[colName];
                const cellCoord = col + i;
                if (workSheet[cellCoord])
                    workSheetRow[colName] = workSheet[cellCoord].v;
            }

            if (workSheetRow.issn) {
                workSheetRow.issn = workSheetRow.issn + '';
                workSheetRow.issn = workSheetRow.issn.replace(/-/g, '');
            }
            if (workSheetRow.eissn) {
                workSheetRow.eissn = workSheetRow.eissn + '';
                workSheetRow.eissn = workSheetRow.eissn.replace(/-/g, '');
            }

            if (_.isEmpty(workSheetRow))
                break;

            i++;

            const baseRecord = {};

            for (const kc of keyColumns)
                if (workSheetRow[kc])
                    baseRecord[kc] = workSheetRow[kc];

            if (_.isEmpty(baseRecord))
                continue;

            baseRecord.year = year;
            baseRecord.origin = origin;

            for (const vc of valueColumns) {
                if (!workSheetRow[vc])
                    continue;

                const criteria = _.clone(baseRecord);
                criteria.name = vc;
                const record = _.clone(baseRecord);
                record.name = vc;
                record.value = workSheetRow[vc];
                await SourceMetric.createOrUpdate(criteria, record);
                recordsCount++;
            }
        }
    }

    sails.log.info('imported ' + recordsCount + ' records');
    sails.log.info('Source metrics import finished');
}<commit_msg>Memberships are now disabled but users are kept active.<commit_after>/* global Source, User, Group, SourceMetric, SourceTypes*/
// Importer.js - in api/services

"use strict";

const xlsx = require('xlsx');
const _ = require('lodash');
const fs = require('fs');
const request = require('request-promise');
const loadJsonFile = require('load-json-file');
const moment = require('moment');

module.exports = {
    importSources,
    importPeople,
    importGroups,
    importSourceMetrics
};

async function importSources() {

    function readWorksheet(worksheet, mappingsTable, mapFn = _.identity, filterFn = _.stubTrue) {
        function readSourceRow(r) {
            const sourceData = {};
            _.forEach(mappingsTable, (col, field) => {
                const cell = col + r;
                if (!_.isUndefined(worksheet[cell]))
                    sourceData[field] = worksheet[cell]['v'];
            });
            return sourceData;
        }

        const sources = [];
        for (let r = 2; ; r++) {
            const sourceData = readSourceRow(r);
            if (_.isEmpty(sourceData))
                break;
            const mappedSourceData = mapFn(sourceData);
            if (filterFn(mappedSourceData))
                sources.push(mappedSourceData);
        }
        return sources;
    }

    const scopusSourcesFileName = 'config/init/scopus_sources.xlsx';
    let journalsAndBookSeries = [], newConferences = [], oldConferences = [];
    if (fs.existsSync(scopusSourcesFileName)) {
        const workbook = xlsx.readFile(scopusSourcesFileName);
        const sheetNameList = workbook.SheetNames;

        const journalWorksheet = workbook.Sheets[sheetNameList[0]];
        const journalMappingsTable = {
            "title": 'B',
            "scopusId": 'A',
            "issn": 'C',
            "eissn": 'D',
            "type": 'T',
            "publisher": 'Z'
        };

        const mapJournal = (s) => {
            const sourceMappingTable = {
                'Book Series': SourceTypes.BOOKSERIES,
                'Journal': SourceTypes.JOURNAL
            };
            s.type = sourceMappingTable[s.type];
            return s;
        };

        const filterJournals = (s) => s.type;

        journalsAndBookSeries = readWorksheet(journalWorksheet, journalMappingsTable, mapJournal, filterJournals);

        const newConferencesWorksheet = workbook.Sheets[sheetNameList[1]];
        const newConferencesMappingsTable = {
            "title": 'B',
            "scopusId": 'A',
            "issn": 'D'
        };
        const mapConference = s => {
            s.type = SourceTypes.CONFERENCE;
            return s;
        };
        newConferences = readWorksheet(newConferencesWorksheet, newConferencesMappingsTable, mapConference);

        const oldConferencesWorksheet = workbook.Sheets[sheetNameList[2]];
        const oldConferencesMappingsTable = newConferencesMappingsTable;
        oldConferences = readWorksheet(oldConferencesWorksheet, oldConferencesMappingsTable, mapConference);
    }

    let books = [];
    const scopusSourcesFileName2 = 'config/init/scopus_book_sources.xlsx';
    if (fs.existsSync(scopusSourcesFileName2)) {
        const workbook2 = xlsx.readFile(scopusSourcesFileName2);
        const sheetNameList2 = workbook2.SheetNames;

        const bookWorksheet = workbook2.Sheets[sheetNameList2[0]];
        const bookMappingsTable = {
            "title": 'A',
            "isbn": 'C',
            "publisher": 'D',
            "year": 'E'
        };

        const mapBook = (s) => {
            s.type = 'book';
            return s;
        };

        books = readWorksheet(bookWorksheet, bookMappingsTable, mapBook);
    }

    const sources = _.union(journalsAndBookSeries, newConferences, oldConferences, books);

    sails.log.info('Inserting ' + sources.length + ' new sources');
    await Source.create(sources);
}

async function importPeople() {
    function userShouldBeUpdated(user, values) {
        const userFieldsToUpdate = [
            'jobTitle',
            'name',
            'surname'
        ];
        const toUpdate = userFieldsToUpdate.some(f => values[f] !== user[f]);
        return toUpdate;
    }

    const usersCreationCondition = sails.config.scientilla.mainInstituteImport.usersCreationCondition;
    const groupInsertionEnabled = false;
    sails.log.info('Import started');
    const url = sails.config.scientilla.mainInstituteImport.userImportUrl;
    const reqOptions = {
        uri: url,
        json: true
    };

    const people = await request(reqOptions);
    sails.log.info(people.length + ' entries found');
    const importTime = moment.utc().format();
    let numUsersInserted = 0, numUsersUpdated = 0, numGroupsInserted = 0;
    for (let [i, p] of people.entries()) {
        //groups are loaded in memory because waterline doesn't allow case-insensitive queries with postegres
        p.username = _.toLower(p.username);
        const allGroups = await Group.find();
        const groupsToBeInserted = p.groups.filter(g => !allGroups.some(g2 => _.toLower(g2.name) == _.toLower(g)));
        const groupsToSearch = allGroups.filter(g => p.groups.some(g2 => _.toLower(g2) == _.toLower(g.name))).map(g => g.name);
        if (groupInsertionEnabled && groupsToBeInserted.length) {
            const groupObjs = groupsToBeInserted.map(g => ({name: g}));
            sails.log.info('inserting groups: ' + groupsToBeInserted.join(', '));
            const newGroups = await Group.create(groupObjs);
            numGroupsInserted++;
            const newGroupsName = newGroups.map(g => g.name);
            groupsToSearch.push(...newGroupsName);
        }
        const groupSearchCriteria = {or: groupsToSearch.map(g => ({name: g}))};
        const groups = await Group.find(groupSearchCriteria).populate('members').populate('administrators');
        const criteria = {username: p.username};
        let user = await User.findOne(criteria);
        p.lastsynch = moment().utc().format();
        p.synchronized = true;
        const activeMembership = p.active;
        p.active = true;
        if (user) {
            const u = await User.update(criteria, p);
            if (userShouldBeUpdated(user, p)) {
                sails.log.info(`Updating user ${p.username}`);
                numUsersUpdated++;
            }
        }
        else {
            if (p[usersCreationCondition.attribute] === usersCreationCondition.value) {
                sails.log.info(`Inserting user ${p.username}`);
                user = await User.createCompleteUser(p);
                numUsersInserted++;
            }
        }
        if (!user)
            continue;

        for (let g of groups) {
            g.members.add(user.id);
            await g.savePromise();
            const membershipCriteria = {user: user.id, group: g.id};
            const membership = await Membership.findOne(membershipCriteria);
            if (membership) {
                membership.lastsynch = moment.utc().format();
                membership.synchronized = true;
                membership.active = activeMembership;
                const m = await Membership.update(membershipCriteria, membership);
            }
            if (p.pi) {
                g.administrators.add(user.id);
                await g.savePromise();
            }
        }
    }
    const membershipUpdateCriteria = {lastsynch: {'<': importTime}, synchronized: true, active: true};
    const membershipDisabled = await Membership.update(membershipUpdateCriteria, {active: false});
    const numMembershipDisabled = membershipDisabled.length;
    const userUpdateCriteria = {lastsynch: {'<': importTime}, synchronized: true, active: true};
    const usersDisabled = await User.update(userUpdateCriteria, {active: false});
    const numUsersDisabled = usersDisabled.length;
    sails.log.info('Import finished');
    sails.log.info(`${numUsersInserted} users inserted`);
    sails.log.info(`${numUsersUpdated} users updated`);
    sails.log.info(`${numUsersDisabled} users disabled`);
    sails.log.info(`${numGroupsInserted} groups inserted`);
    sails.log.info(`${numMembershipDisabled} membership disabled`);
}

async function importGroups() {
    sails.log.info('Import started');
    const groupsPath = 'data/groups.json';
    const groupNames = await loadJsonFile(groupsPath);
    sails.log.info(groupNames.length + ' entries found');
    for (let groupName of groupNames) {
        const group = await Group.findOneByName(groupName);
        if (group)
            continue;
        await Group.create({name: groupName});
    }
    sails.log.info('Import finished');
}

async function importSourceMetrics(filename) {
    sails.log.info('Source metrics import started');

    const sourceIdentifiers = SourceMetric.sourceIdentifiers;

    const originCellCoord = 'B1';
    const yearCellCoord = 'D1';

    let recordsCount = 0;

    const cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
    const filePath = 'config/init/' + filename;
    if (fs.existsSync(filePath)) {
        const workbook = xlsx.readFile(filePath);
        const sheetNameList = workbook.SheetNames;

        const workSheet = workbook.Sheets[sheetNameList[0]];

        if (!workSheet[originCellCoord] || !workSheet[yearCellCoord])
            throw 'Invalid file format';

        const origin = workSheet[originCellCoord].v;
        const year = workSheet[yearCellCoord].v;

        const columnsMapping = {};

        for (const c of cols) {
            const colNameCell = workSheet[c + '2'];
            if (!colNameCell)
                break;

            columnsMapping[colNameCell.v] = c;
        }

        const keyColumns = Object.keys(columnsMapping).filter(c => sourceIdentifiers.includes(c));
        const valueColumns = Object.keys(columnsMapping).filter(c => !sourceIdentifiers.includes(c));

        let i = 3;
        while (true) {
            const workSheetRow = {};

            for (const colName in columnsMapping) {
                const col = columnsMapping[colName];
                const cellCoord = col + i;
                if (workSheet[cellCoord])
                    workSheetRow[colName] = workSheet[cellCoord].v;
            }

            if (workSheetRow.issn) {
                workSheetRow.issn = workSheetRow.issn + '';
                workSheetRow.issn = workSheetRow.issn.replace(/-/g, '');
            }
            if (workSheetRow.eissn) {
                workSheetRow.eissn = workSheetRow.eissn + '';
                workSheetRow.eissn = workSheetRow.eissn.replace(/-/g, '');
            }

            if (_.isEmpty(workSheetRow))
                break;

            i++;

            const baseRecord = {};

            for (const kc of keyColumns)
                if (workSheetRow[kc])
                    baseRecord[kc] = workSheetRow[kc];

            if (_.isEmpty(baseRecord))
                continue;

            baseRecord.year = year;
            baseRecord.origin = origin;

            for (const vc of valueColumns) {
                if (!workSheetRow[vc])
                    continue;

                const criteria = _.clone(baseRecord);
                criteria.name = vc;
                const record = _.clone(baseRecord);
                record.name = vc;
                record.value = workSheetRow[vc];
                await SourceMetric.createOrUpdate(criteria, record);
                recordsCount++;
            }
        }
    }

    sails.log.info('imported ' + recordsCount + ' records');
    sails.log.info('Source metrics import finished');
}<|endoftext|>


<commit_before>// setup
var express = require('express')
var app = express()
var mongoose = require('mongoose')
var morgan = require('morgan')
var bodyParser = require('body-parser')
var methodOverride = require('method-override')

// configuration
mongoose.connect('mongodb://localhost:27017/scotchio')
app.use(express.static(__dirname + '/public'))
app.use(morgan('dev'))
app.use(bodyParser.urlencoded({'extended': 'true'}))
app.use(bodyParser.json())
app.use(bodyParser.json({type: 'application/vnd.api+json'}))
app.use(methodOverride())

// define models
var toDo = mongoose.model('toDo', {
  text: String
})

// routes

// api --------------------------------

// get all todos
app.get('/api/todos', function (req, res) {
  // use mongoose to get all todos in the database
  toDo.find(function (err, todos) {
    // if there is an error retrieving, send the error. nothing after res.send(err) will execute
    if (err) {
      res.send(err)
    }

    res.json(todos) // return all todos in JSON format
  })
})

// create todo and send back all todos after creation
app.post('/api/todos', function (req, res) {
  // create a todo, information comes from AJAX request from Angular
  toDo.create({
    text: req.body.text,
    done: false
  }, function (err, todo) {
    if (err) {
      res.send(err)
    }

    // get and return all the todos after you create another
    toDo.find(function (err, todos) {
      if (err) {
        res.send(err)
      }
      res.json(todos)
    })
  })
})

// delete a todo
app.delete('/api/todos/:todo_id', function (req, res) {
  toDo.remove({
    _id: req.params.todo_id
  }, function (err, todo) {
    if (err) {
      res.send(err)
    }

    // get and return all the todos after you create another
    toDo.find(function (err, todos) {
      if (err) {
        res.send(err)
      }
      res.json(todos)
    })
  })
})

// listen
app.listen(8080)
console.log('App listening on port 8080')
<commit_msg>grammar fix<commit_after>// setup
var express = require('express')
var app = express()
var mongoose = require('mongoose')
var morgan = require('morgan')
var bodyParser = require('body-parser')
var methodOverride = require('method-override')

// configuration
mongoose.connect('mongodb://localhost:27017/scotchio')
app.use(express.static(__dirname + '/public'))
app.use(morgan('dev'))
app.use(bodyParser.urlencoded({'extended': 'true'}))
app.use(bodyParser.json())
app.use(bodyParser.json({type: 'application/vnd.api+json'}))
app.use(methodOverride())

// define models
var toDo = mongoose.model('toDo', {
  text: String
})

// routes

// api --------------------------------

// get all todos
app.get('/api/todos', function (req, res) {
  // use mongoose to get all todos in the database
  toDo.find(function (err, todos) {
    /*
    if there is an error retrieving, send the error.
    nothing after res.send(err) will execute
    */
    if (err) {
      res.send(err)
    }

    res.json(todos) // return all todos in JSON format
  })
})

// create todo and send back all todos after creation
app.post('/api/todos', function (req, res) {
  // create a todo, information comes from AJAX request from Angular
  toDo.create({
    text: req.body.text,
    done: false
  }, function (err, todo) {
    if (err) {
      res.send(err)
    }

    // get and return all the todos after you create another
    toDo.find(function (err, todos) {
      if (err) {
        res.send(err)
      }
      res.json(todos)
    })
  })
})

// delete a todo
app.delete('/api/todos/:todo_id', function (req, res) {
  toDo.remove({
    _id: req.params.todo_id
  }, function (err, todo) {
    if (err) {
      res.send(err)
    }

    // get and return all the todos after you create another
    toDo.find(function (err, todos) {
      if (err) {
        res.send(err)
      }
      res.json(todos)
    })
  })
})

// listen
app.listen(8080)
console.log('App listening on port 8080')
<|endoftext|>


<commit_before>'use strict'
//
// Environment configurations @{
var port = process.env.PORT || 3000;
var portSSL = process.env.PORT_SSL || false;
var dbName = process.env.DB_NAME || false;
var respectCORS = process.env.RESPECT_CORS ? true : false;
// @}

//
// Required libraries @{
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var https = require('https');
var http = require('http');
var fs = require('fs');
// @}

//
// Main application.
var app = express();

//
// Database.
if (dbName) {
    var mongoose = require('mongoose');
    mongoose.Promise = global.Promise;
    mongoose.connect('mongodb://localhost/' + dbName, {
        useMongoClient: true
    });
    mongoose.connection.on('error', (err) => {
        console.log(err.name + ': ' + err.message);
    });
}
// @}

//
// Basic configurations @{
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(express.static(path.join(__dirname, 'public')));
// @}

//
// Routes @{
var routeExample = require('./routes/example');
app.use('/example', routeExample);
// @}

//
// Avoid CORS validations.
if (!respectCORS) {
    var cors = require('cors');
    app.all('*', cors());
}

//
// RESTful imports.
//  @note this depends on the presence of a Mongo database.
if (dbName) {
    //
    // Required libraries.
    let methodOverride = require('method-override');
    let restify = require('express-restify-mongoose');
    //
    // Middlewares.
    app.use(methodOverride());
    //
    // RESTful API options.
    let restOptions = {
        prefix: '/rest'
    };
    //
    // Exposing models.
    restify.serve(app, require('./schemas/example'), restOptions); // URI: /rest/v1/examples
}
// @}

//
// Default redirects.
app.use(function (req, res, next) {
    if (req.headers['accept'] === 'application/json') {
        res.status(404).json({
            message: 'Not Found',
            uri: req.url
        });
    } else {
        res.sendFile(__dirname + '/public/index.html');
    }
});

//
// HTTPS options.
var options = {
    key: fs.readFileSync('secure/key.pem'),
    cert: fs.readFileSync('secure/cert.pem')
};

// Create an HTTP service.
http.createServer(app).listen(port, () => {
    console.log("+--------------------------------------------------");
    console.log("| Server running on 'http://localhost:" + port + "'");

    console.log("| \tDB: " + (dbName ? dbName : 'Not in use'));
    if (dbName) {
        console.log("| \t\tRestify available");
    }
    console.log("| \tCORS: " + (respectCORS ? 'Respects the protocol' : 'Avoiding warnings'));

    console.log("+--------------------------------------------------");
});
//
// Create an HTTPS service identical to the HTTP service.
if (portSSL) {
    https.createServer(options, app).listen(portSSL, () => {
        console.log("| Secure Server running on 'https://localhost:" + portSSL + "'");
        console.log("+--------------------------------------------------");
    });
}
<commit_msg>Server defaults<commit_after>'use strict'
//
// Environment configurations @{
var port = process.env.PORT || 3000;
var portSSL = process.env.PORT_SSL || false;
var dbName = process.env.DB_NAME || false;
var respectCORS = process.env.RESPECT_CORS ? true : false;
// @}

//
// Required libraries @{
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var https = require('https');
var http = require('http');
var fs = require('fs');
// @}

//
// Main application.
var app = express();

//
// Database.
if (dbName) {
    var mongoose = require('mongoose');
    mongoose.Promise = global.Promise;
    mongoose.connect('mongodb://localhost/' + dbName, {
        useMongoClient: true
    });
    mongoose.connection.on('error', (err) => {
        console.log(err.name + ': ' + err.message);
    });
}
// @}

//
// Basic configurations @{
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(express.static(path.join(__dirname, 'public')));
// @}

//
// Routes @{
var routeExample = require('./routes/example');
app.use('/example', routeExample);
// @}

//
// Avoid CORS validations.
if (!respectCORS) {
    var cors = require('cors');
    app.all('*', cors());
}

//
// RESTful imports.
//  @note this depends on the presence of a Mongo database.
if (dbName) {
    //
    // Required libraries.
    let methodOverride = require('method-override');
    let restify = require('express-restify-mongoose');
    //
    // Middlewares.
    app.use(methodOverride());
    //
    // RESTful API options.
    let restOptions = {
        prefix: '/rest'
    };
    //
    // Exposing models.
    restify.serve(app, require('./schemas/example'), restOptions); // URI: /rest/v1/examples
}
// @}

//
// Default redirects.
app.use(function (req, res, next) {
    if (req.xhr || req.headers['accept'] === 'application/json' || req.headers['content-type'] === 'application/json') {
        res.status(404).json({
            message: 'Not Found',
            uri: req.url,
            isAjax: req.xhr
        });
    } else {
        res.sendFile(__dirname + '/public/index.html');
    }
});

//
// HTTPS options.
var options = {
    key: fs.readFileSync('secure/key.pem'),
    cert: fs.readFileSync('secure/cert.pem')
};

// Create an HTTP service.
http.createServer(app).listen(port, () => {
    console.log("+--------------------------------------------------");
    console.log("| Server running on 'http://localhost:" + port + "'");

    console.log("| \tDB: " + (dbName ? dbName : 'Not in use'));
    if (dbName) {
        console.log("| \t\tRestify available");
    }
    console.log("| \tCORS: " + (respectCORS ? 'Respects the protocol' : 'Avoiding warnings'));

    console.log("+--------------------------------------------------");
});
//
// Create an HTTPS service identical to the HTTP service.
if (portSSL) {
    https.createServer(options, app).listen(portSSL, () => {
        console.log("| Secure Server running on 'https://localhost:" + portSSL + "'");
        console.log("+--------------------------------------------------");
    });
}
<|endoftext|>


<commit_before>import VisualizationComponent from '../core/VisualizationComponent';
import vcharts from '../external/vcharts/src';

export default class Scatter extends VisualizationComponent {
  static get options () {
    return [
      {name: 'data', type: 'table'},
      {name: 'x', type: 'string'},
      {name: 'y', type: 'string'},
      {name: 'color', type: 'string'}
    ];
  }

  constructor (el, data, options) {
    super(el);

    this.data = data;

    this.options = Object.assign({}, options);

    this.options.x = this.options.x || 'x';
    this.options.y = this.options.y || 'y';

    this.options.color = this.options.color || 'steelblue';

    this.chart = vcharts.chart('xy', {
      el,
      series: [
        {
          name: 'values'
        }
      ]
    });

    window.onresize = () => this.render();
  }

  render () {
    this.chart.update({
      series: [{
        name: 'values',
        values: this.data,
        x: this.options.x,
        y: this.options.y,
        color: this.options.color
      }],
      xAxis: {
        title: this.options.x
      },
      yAxis: {
        title: this.options.y
      },
      legend: false
    });
  }
}
<commit_msg>Use name 'default color' for "global" color setting<commit_after>import VisualizationComponent from '../core/VisualizationComponent';
import vcharts from '../external/vcharts/src';

export default class Scatter extends VisualizationComponent {
  static get options () {
    return [
      {name: 'data', type: 'table'},
      {name: 'x', type: 'string'},
      {name: 'y', type: 'string'},
      {name: 'color', type: 'string'}
    ];
  }

  constructor (el, data, options) {
    super(el);

    this.data = data;

    this.options = Object.assign({}, options);

    this.options.x = this.options.x || 'x';
    this.options.y = this.options.y || 'y';

    this.options.defaultColor = this.options.defaultColor || 'steelblue';

    this.chart = vcharts.chart('xy', {
      el,
      series: [
        {
          name: 'values'
        }
      ]
    });

    window.onresize = () => this.render();
  }

  render () {
    this.chart.update({
      series: [{
        name: 'values',
        values: this.data,
        x: this.options.x,
        y: this.options.y,
        color: this.options.color
      }],
      xAxis: {
        title: this.options.x
      },
      yAxis: {
        title: this.options.y
      },
      legend: false
    });
  }
}
<|endoftext|>


<commit_before>(function(cornerstone, cornerstoneTools) {

    'use strict';

    // This implements an Stack specific tool state management strategy.  This means
    // that tool data is shared between all imageIds in a given stack
    function newStackSpecificToolStateManager(toolTypes, oldStateManager) {
        var toolState = {};

        function saveToolState() {
            return toolState;
        }

        function restoreToolState(stackToolState) {
            toolState = stackToolState;
        }

        // here we add tool state, this is done by tools as well
        // as modules that restore saved state
        function addStackSpecificToolState(element, toolType, data) {
            // if this is a tool type to apply to the stack, do so
            if (toolTypes.indexOf(toolType) >= 0) {

                // if we don't have tool state for this type of tool, add an empty object
                if (toolState.hasOwnProperty(toolType) === false) {
                    toolState[toolType] = {
                        data: []
                    };
                }

                var toolData = toolState[toolType];

                // finally, add this new tool to the state
                toolData.data.push(data);
            } else {
                // call the imageId specific tool state manager
                return oldStateManager.add(element, toolType, data);
            }
        }

        // here you can get state - used by tools as well as modules
        // that save state persistently
        function getStackSpecificToolState(element, toolType) {
            // if this is a tool type to apply to the stack, do so
            if (toolTypes.indexOf(toolType) >= 0) {
                // if we don't have tool state for this type of tool, add an empty object
                if (toolState.hasOwnProperty(toolType) === false) {
                    toolState[toolType] = {
                        data: []
                    };
                }

                var toolData = toolState[toolType];
                return toolData;
            } else {
                // call the imageId specific tool state manager
                return oldStateManager.get(element, toolType);
            }
        }

        var stackSpecificToolStateManager = {
            get: getStackSpecificToolState,
            add: addStackSpecificToolState,
            saveToolState: saveToolState,
            restoreToolState: restoreToolState,
            toolState: toolState,
        };
        return stackSpecificToolStateManager;
    }

    var stackStateManagers = [];

    function addStackStateManager(element) {
        var oldStateManager = cornerstoneTools.getElementToolStateManager(element);
        if (!oldStateManager) {
            oldStateManager = cornerstoneTools.globalImageIdSpecificToolStateManager;
        }

        var stackTools = [ 'stack', 'stackPrefetch', 'playClip', 'volume', 'slab', 'referenceLines', 'crosshairs' ];
        var stackSpecificStateManager = cornerstoneTools.newStackSpecificToolStateManager(stackTools, oldStateManager);
        stackStateManagers.push(stackSpecificStateManager);
        cornerstoneTools.setElementToolStateManager(element, stackSpecificStateManager);
    }

    // module/private exports
    cornerstoneTools.newStackSpecificToolStateManager = newStackSpecificToolStateManager;
    cornerstoneTools.addStackStateManager = addStackStateManager;

})(cornerstone, cornerstoneTools);
<commit_msg>Concat custom tools into stack specific state manager<commit_after>(function(cornerstone, cornerstoneTools) {

    'use strict';

    // This implements an Stack specific tool state management strategy.  This means
    // that tool data is shared between all imageIds in a given stack
    function newStackSpecificToolStateManager(toolTypes, oldStateManager) {
        var toolState = {};

        function saveToolState() {
            return toolState;
        }

        function restoreToolState(stackToolState) {
            toolState = stackToolState;
        }

        // here we add tool state, this is done by tools as well
        // as modules that restore saved state
        function addStackSpecificToolState(element, toolType, data) {
            // if this is a tool type to apply to the stack, do so
            if (toolTypes.indexOf(toolType) >= 0) {

                // if we don't have tool state for this type of tool, add an empty object
                if (toolState.hasOwnProperty(toolType) === false) {
                    toolState[toolType] = {
                        data: []
                    };
                }

                var toolData = toolState[toolType];

                // finally, add this new tool to the state
                toolData.data.push(data);
            } else {
                // call the imageId specific tool state manager
                return oldStateManager.add(element, toolType, data);
            }
        }

        // here you can get state - used by tools as well as modules
        // that save state persistently
        function getStackSpecificToolState(element, toolType) {
            // if this is a tool type to apply to the stack, do so
            if (toolTypes.indexOf(toolType) >= 0) {
                // if we don't have tool state for this type of tool, add an empty object
                if (toolState.hasOwnProperty(toolType) === false) {
                    toolState[toolType] = {
                        data: []
                    };
                }

                var toolData = toolState[toolType];
                return toolData;
            } else {
                // call the imageId specific tool state manager
                return oldStateManager.get(element, toolType);
            }
        }

        var stackSpecificToolStateManager = {
            get: getStackSpecificToolState,
            add: addStackSpecificToolState,
            saveToolState: saveToolState,
            restoreToolState: restoreToolState,
            toolState: toolState,
        };
        return stackSpecificToolStateManager;
    }

    var stackStateManagers = [];

    function addStackStateManager(element, otherTools) {
        var oldStateManager = cornerstoneTools.getElementToolStateManager(element);
        if (!oldStateManager) {
            oldStateManager = cornerstoneTools.globalImageIdSpecificToolStateManager;
        }

        var stackTools = [ 'stack', 'stackPrefetch', 'playClip', 'volume', 'slab', 'referenceLines', 'crosshairs' ];
        if (otherTools) {
          stackTools = stackTools.concat(otherTools)
        }
        var stackSpecificStateManager = cornerstoneTools.newStackSpecificToolStateManager(stackTools, oldStateManager);
        stackStateManagers.push(stackSpecificStateManager);
        cornerstoneTools.setElementToolStateManager(element, stackSpecificStateManager);
    }

    // module/private exports
    cornerstoneTools.newStackSpecificToolStateManager = newStackSpecificToolStateManager;
    cornerstoneTools.addStackStateManager = addStackStateManager;

})(cornerstone, cornerstoneTools);
<|endoftext|>


<commit_before>/*globals define*/
/*jshint node:true, browser:true*/

/**
 * Generated by PluginGenerator 0.14.0 from webgme on Mon Apr 04 2016 15:12:25 GMT-0700 (PDT).
 */

define([
    'plugin/PluginConfig',
    'plugin/PluginBase',
    'gridlabd/meta',
    'q'
], function (
    PluginConfig,
    PluginBase,
    MetaTypes,
    Q) {
    'use strict';

    /**
     * Initializes a new instance of ImportGLM.
     * @class
     * @augments {PluginBase}
     * @classdesc This class represents the plugin ImportGLM.
     * @constructor
     */
    var ImportGLM = function () {
        // Call base class' constructor.
        PluginBase.call(this);

        this.metaTypes = MetaTypes;
    };

    // Prototypal inheritance from PluginBase.
    ImportGLM.prototype = Object.create(PluginBase.prototype);
    ImportGLM.prototype.constructor = ImportGLM;

    /**
     * Gets the name of the ImportGLM.
     * @returns {string} The name of the plugin.
     * @public
     */
    ImportGLM.prototype.getName = function () {
        return 'ImportGLM';
    };

    /**
     * Gets the semantic version (semver.org) of the ImportGLM.
     * @returns {string} The version of the plugin.
     * @public
     */
    ImportGLM.prototype.getVersion = function () {
        return '0.1.0';
    };

    ImportGLM.prototype.getConfigStructure = function() {
        return [
            {
                'name': 'glmFile',
                'displayName': 'Gridlab-D Model File',
                'description': 'GLM file for loading as a WebGME Power System Model.',
                'value': '',
                'valueType': 'asset',
                'readOnly': false
            }
        ];
    };

    /**
     * Main function for the plugin to execute. This will perform the execution.
     * Notes:
     * - Always log with the provided logger.[error,warning,info,debug].
     * - Do NOT put any user interaction logic UI, etc. inside this method.
     * - callback always has to be called even if error happened.
     *
     * @param {function(string, plugin.PluginResult)} callback - the result callback
     */
    ImportGLM.prototype.main = function (callback) {
        // Use self to access core, project, result, logger etc from PluginBase.
        // These are all instantiated at this point.
        var self = this,
        nodeObject;

        self.updateMETA(self.metaTypes);

        // Default fails
        self.result.success = false;

	// fill this out before creating the WebGME nodes
	self.newModel = {
	    children: [],
	    attributes: {}
	};

	var currentConfig = self.getCurrentConfig(),
	glmFileHash = currentConfig.glmFile;

        // Using the coreAPI to make changes.
        nodeObject = self.activeNode;

	self.blobClient.getMetadata(glmFileHash)
	    .then(function(glmMetaData) {
		var splitName = glmMetaData.name.split(".");
		var newName = "";
		for (var i=0;i<splitName.length-1;i++) {
		    newName += splitName[i];
		}
		self.modelName = newName;
		self.newModel.name = newName;
		self.logger.error('loaded model: ' + self.modelName);
	    })
	    .then(function() {
		return self.blobClient.getObjectAsString(glmFileHash)
	    })
	    .then(function(glmFile) {
		return self.parseObjectsFromGLM(glmFile);
	    })
	    .then(function() {
		return self.createModelArtifacts();
	    })
	    .then(function() {
		// This will save the changes. If you don't want to save;
		// exclude self.save and call callback directly from this scope.
		return self.save('ImportGLM updated model.');
	    })
	    .then(function() {
		self.result.setSuccess(true);
		callback(null, self.result);
	    })
	    .catch(function(err) {
		self.logger.error('ERROR:: '+err);
		self.result.setSuccess(false);
		callback(null, self.result);
	    });
    };

    ImportGLM.prototype.parseObjectsFromGLM = function(glmFile) {
	// fill out self.newModel
	var self = this;
	self.parseHeader(glmFile);
	self.parseObject(glmFile, self.newModel);
	//self.logger.error(JSON.stringify(self.newModel,null,2));
    };

    ImportGLM.prototype.parseHeader = function(str) {
	var self = this;
	var regex = /#(\S+)\s+(\S+)\s*=\s*([\S ]+)(?:;)?/gi;
	var matches = regex.exec(str);
	while (matches != null) {
	    var cmd = matches[1];
	    var variable = matches[2];
	    var value = matches[3].replace(/;/gi,'').replace(/'/gi,'');
	    self.newModel.attributes[variable] = value;
	    //self.logger.error('got ' + cmd + ' for variable ' + variable + ' and value ' + value);
	    matches = regex.exec(str);
	}
    };

    ImportGLM.prototype.saveHeader = function(obj) {
	var self = this;
	// save obj as node here (set the properties of the model)
    };

    ImportGLM.prototype.parseClock = function(str, obj) {
	var self = this;
	var patterns = [
		/(timestamp)\s+'([^\/\n\r\v][\w:\- \']*)';/gi,
		/(stoptime)\s+'([^\/\n\r\v][\w:\- \']*)';/gi,
		/(timezone)\s+([^\/\n\r\v][\w:\- \']*);/gi
	];

	patterns.map(function(pattern) {
	    var matches = pattern.exec(str);
	    while (matches != null) {
		var key = matches[1];
		var value = matches[2];
		obj.attributes[key] = value;
		matches = pattern.exec(str);
	    }
	});
	self.newModel.children.push(obj);
    };

    ImportGLM.prototype.saveClock = function(obj, parentNode) {
	var self = this;
	// save obj as node here (set the properties of the clock)
	var clockNode = self.core.createNode({parent: parentNode, base: self.META.clock});
	self.core.setAttribute(clockNode, 'name', 'clock');
	for (var a in obj.attributes) {
	    var val = obj.attributes[a];
	    self.core.setAttribute(clockNode, a, val);
	}
    };

    ImportGLM.prototype.parseSchedule = function(str, obj) {
	var self = this;
	var pattern = /\s(?:([\d\*\.]+[\-\.\d]*)[ \t]*)+;?/gi;
	var lines = str.split('\n');
	obj.entries = [];
	lines.map(function(line) {
	    var matches = pattern.exec(line);
	    if (matches) {
		var splits = matches[0].split(new RegExp(" |\t|\s|;",'g')).filter(function(obj) {return obj.length > 0;});
		if ( splits.length >=5 ) {
		    var entry = {
			minutes: splits[0],
			hours: splits[1],
			days: splits[2],
			months: splits[3],
			weekdays: splits[4],
			value: splits[5]
		    };
		    obj.entries.push(entry)
		}
		else {
		    self.logger.error(line);
		    self.logger.error(matches);
		    self.logger.error(splits);
		}
	    }
	});
	self.newModel.children.push(obj);
    };

    ImportGLM.prototype.saveSchedule = function(obj, parentNode) {
	var self = this;
	// save obj as node here (set the properties of the schedule)
	var schedNode = self.core.createNode({parent: parentNode, base: self.META.schedule});
	self.core.setAttribute(schedNode, 'name', obj.name);
	var i =0;
	obj.entries.map(function(entry) {
	    var entryNode = self.core.createNode({parent: schedNode, base: self.META.schedule_entry});
	    self.core.setAttribute(entryNode, 'name', 'Entry ' + i++);
	    //self.logger.error(JSON.stringify(entry, null, 2));
	    for (var a in entry) {
		var val = entry[a];
		self.core.setAttribute(entryNode, a, val);
	    }
	});
    };

    ImportGLM.prototype.parseMultiRecorder = function(str, obj) {
	var self = this;
	var splitString = /[\s;]+/gi;
	var splits;
	var lines = str.split('\n');
	lines.map(function(line) {
	    splits = line.split(splitString)
		.filter(function(obj) { return obj.length > 0; });
	    if ( splits.length > 0 && splits[0].indexOf('/') == -1 ) {
		obj.attributes[splits[0]] = splits[1];
	    }
	});
	self.newModel.children.push(obj);
    };

    ImportGLM.prototype.saveMultiRecorder = function(obj, parentNode) {
	var self = this;
	// save obj as node here (set the properties of the multiRecorder)
	var recorderNode = self.core.createNode({parent: parentNode, base: self.META.multi_recorder});
	self.core.setAttribute(recorderNode, 'name', obj.name);
	for (var a in obj.attributes) {
	    var val = obj.attributes[a];
	    self.core.setAttribute(recorderNode, a, val);
	}
    };

    ImportGLM.prototype.parseObject = function(str, parent) {
	var self = this;
	var splitString = /[\s:\{]+/gi;
	var submodel_str = '';
	var submodels = [];
	var currentObj = undefined;
	var depth = 0;
	var lines = str.split('\n');
	var splits;
	lines.map(function(line) {
	    if ( line.indexOf('{') > -1 ) {
		if ( depth == 0 ) {
		    splits = line.split(splitString).filter(function(obj) { return obj.length > 0; });
		    if ( splits.length > 0 && splits[0].indexOf('/') == -1 ) {
			var base = splits[0];
			var type = splits[1];
			var name;
			if (splits.length >= 3) {
			    name = splits[2];
			}
			currentObj = {
			    type: type,
			    base: base,
			    name: name,
			    children: [],
			    attributes: {}
			};
		    }
		}
		else {
		    submodel_str += line +'\n';
		}
		depth += 1;
	    }
	    else if ( line.indexOf('}') > -1 ) {
		depth -= 1;
		if ( depth == 0 ) {
		    if (currentObj) {
			if (currentObj.base == 'clock') {
			    currentObj.type = currentObj.base;
			    currentObj.attributes = {};
			    self.parseClock(submodel_str, currentObj);
			}
			else if (currentObj.base == 'schedule') {
			    currentObj.name = currentObj.type;
			    currentObj.type = currentObj.base;
			    currentObj.attributes = {};
			    self.parseSchedule(submodel_str, currentObj);
			}
			else if (currentObj.type == 'multi_recorder') {
			    currentObj.name = currentObj.type;
			    currentObj.type = currentObj.base;
			    currentObj.attributes = {};
			    self.parseMultiRecorder(submodel_str, currentObj);
			}
			else {
			    submodels.push({string:submodel_str, object:currentObj});
			}
			currentObj = undefined;
			submodel_str = '';
		    }
		}
		else {
		    submodel_str += line + '\n';
		}
	    }
	    else {
		if (depth >= 1) {
		    // parse property here
		    splits = line.split(splitString).filter(function(obj) { return obj.length > 0; });
		    if (splits && splits[0].indexOf('/') == -1) { // don't want comments
			if (depth == 1)
			    currentObj.attributes[splits[0]] = splits[1].replace(';','');
			submodel_str += line + '\n';
		    }
		}
	    }
	});
	submodels.map(function(subModel) {
	    self.parseObject(subModel.string, subModel.object);
	    parent.children.push(subModel.object);
	});
    };

    ImportGLM.prototype.resolveReferences = function() {
    };

    ImportGLM.prototype.createModelArtifacts = function() {
	// use self.newModel
	var self = this;
	var fcoNode = self.core.getBaseRoot(self.activeNode);
	var modelMetaNode = self.META.Model;
	var modelNode = self.core.createNode({parent: self.activeNode, base: modelMetaNode});
	self.core.setAttribute(modelNode, 'name', self.newModel.name);
	for (var ai in self.newModel.attributes) {
	    var val = self.newModel.attributes[ai];
	    self.core.setAttribute(modelNode, ai, val);
	}
	for (var oi in self.newModel.children) {
	    var obj = self.newModel.children[oi];
	    if ( obj.type == 'clock' ){
		self.saveClock(obj, modelNode);
	    }
	    else if ( obj.type == 'schedule' ){
		self.saveSchedule(obj, modelNode);
	    }
	    else if ( obj.type == 'multi_recorder' ){
		self.saveMultiRecorder(obj, modelNode);
	    }
	    else if ( obj.type ) {
		var newNode = self.core.createNode({parent: modelNode, base: self.META[obj.type]});
		if (obj.name) {
		    self.core.setAttribute(newNode, 'name', obj.name);
		}
		for (var a in obj.attributes) {
		    var val = obj.attributes[a];
		    self.core.setAttribute(newNode, a, val);
		}
	    }
	}
    };

    return ImportGLM;
});
<commit_msg>refactoring for easier readability and to prepare for handling references.<commit_after>/*globals define*/
/*jshint node:true, browser:true*/

/**
 * Generated by PluginGenerator 0.14.0 from webgme on Mon Apr 04 2016 15:12:25 GMT-0700 (PDT).
 */

define([
    'plugin/PluginConfig',
    'plugin/PluginBase',
    'gridlabd/meta',
    'q'
], function (
    PluginConfig,
    PluginBase,
    MetaTypes,
    Q) {
    'use strict';

    /**
     * Initializes a new instance of ImportGLM.
     * @class
     * @augments {PluginBase}
     * @classdesc This class represents the plugin ImportGLM.
     * @constructor
     */
    var ImportGLM = function () {
        // Call base class' constructor.
        PluginBase.call(this);

        this.metaTypes = MetaTypes;
    };

    // Prototypal inheritance from PluginBase.
    ImportGLM.prototype = Object.create(PluginBase.prototype);
    ImportGLM.prototype.constructor = ImportGLM;

    /**
     * Gets the name of the ImportGLM.
     * @returns {string} The name of the plugin.
     * @public
     */
    ImportGLM.prototype.getName = function () {
        return 'ImportGLM';
    };

    /**
     * Gets the semantic version (semver.org) of the ImportGLM.
     * @returns {string} The version of the plugin.
     * @public
     */
    ImportGLM.prototype.getVersion = function () {
        return '0.1.0';
    };

    ImportGLM.prototype.getConfigStructure = function() {
        return [
            {
                'name': 'glmFile',
                'displayName': 'Gridlab-D Model File',
                'description': 'GLM file for loading as a WebGME Power System Model.',
                'value': '',
                'valueType': 'asset',
                'readOnly': false
            }
        ];
    };

    /**
     * Main function for the plugin to execute. This will perform the execution.
     * Notes:
     * - Always log with the provided logger.[error,warning,info,debug].
     * - Do NOT put any user interaction logic UI, etc. inside this method.
     * - callback always has to be called even if error happened.
     *
     * @param {function(string, plugin.PluginResult)} callback - the result callback
     */
    ImportGLM.prototype.main = function (callback) {
        // Use self to access core, project, result, logger etc from PluginBase.
        // These are all instantiated at this point.
        var self = this,
        nodeObject;

        self.updateMETA(self.metaTypes);

        // Default fails
        self.result.success = false;

	// fill this out before creating the WebGME nodes
	self.newModel = {
	    children: [],
	    attributes: {}
	};

	var currentConfig = self.getCurrentConfig(),
	glmFileHash = currentConfig.glmFile;

        // Using the coreAPI to make changes.
        nodeObject = self.activeNode;

	self.blobClient.getMetadata(glmFileHash)
	    .then(function(glmMetaData) {
		var splitName = glmMetaData.name.split(".");
		var newName = "";
		for (var i=0;i<splitName.length-1;i++) {
		    newName += splitName[i];
		}
		self.modelName = newName;
		self.newModel.name = newName;
		self.logger.error('loaded model: ' + self.modelName);
	    })
	    .then(function() {
		return self.blobClient.getObjectAsString(glmFileHash)
	    })
	    .then(function(glmFile) {
		return self.parseObjectsFromGLM(glmFile);
	    })
	    .then(function() {
		return self.createModelArtifacts();
	    })
	    .then(function() {
		// This will save the changes. If you don't want to save;
		// exclude self.save and call callback directly from this scope.
		return self.save('ImportGLM updated model.');
	    })
	    .then(function() {
		self.result.setSuccess(true);
		callback(null, self.result);
	    })
	    .catch(function(err) {
		self.logger.error('ERROR:: '+err);
		self.result.setSuccess(false);
		callback(null, self.result);
	    });
    };

    ImportGLM.prototype.parseObjectsFromGLM = function(glmFile) {
	// fill out self.newModel
	var self = this;
	self.parseHeader(glmFile);
	self.parseObject(glmFile, self.newModel);
	//self.logger.error(JSON.stringify(self.newModel,null,2));
    };

    ImportGLM.prototype.parseHeader = function(str) {
	var self = this;
	var regex = /#(\S+)\s+(\S+)\s*=\s*([\S ]+)(?:;)?/gi;
	var matches = regex.exec(str);
	while (matches != null) {
	    var cmd = matches[1];
	    var variable = matches[2];
	    var value = matches[3].replace(/;/gi,'').replace(/'/gi,'');
	    self.newModel.attributes[variable] = value;
	    //self.logger.error('got ' + cmd + ' for variable ' + variable + ' and value ' + value);
	    matches = regex.exec(str);
	}
    };

    ImportGLM.prototype.saveHeader = function(obj) {
	var self = this;
	// save obj as node here (set the properties of the model)
    };

    ImportGLM.prototype.parseClock = function(str, obj) {
	var self = this;
	var patterns = [
		/(timestamp)\s+'([^\/\n\r\v][\w:\- \']*)';/gi,
		/(stoptime)\s+'([^\/\n\r\v][\w:\- \']*)';/gi,
		/(timezone)\s+([^\/\n\r\v][\w:\- \']*);/gi
	];

	patterns.map(function(pattern) {
	    var matches = pattern.exec(str);
	    while (matches != null) {
		var key = matches[1];
		var value = matches[2];
		obj.attributes[key] = value;
		matches = pattern.exec(str);
	    }
	});
	self.newModel.children.push(obj);
    };

    ImportGLM.prototype.saveClock = function(obj, parentNode) {
	var self = this;
	// save obj as node here (set the properties of the clock)
	var clockNode = self.core.createNode({parent: parentNode, base: self.META.clock});
	self.core.setAttribute(clockNode, 'name', 'clock');
	for (var a in obj.attributes) {
	    var val = obj.attributes[a];
	    self.core.setAttribute(clockNode, a, val);
	}
    };

    ImportGLM.prototype.parseSchedule = function(str, obj) {
	var self = this;
	var pattern = /\s(?:([\d\*\.]+[\-\.\d]*)[ \t]*)+;?/gi;
	var lines = str.split('\n');
	obj.entries = [];
	lines.map(function(line) {
	    var matches = pattern.exec(line);
	    if (matches) {
		var splits = matches[0].split(new RegExp(" |\t|\s|;",'g')).filter(function(obj) {return obj.length > 0;});
		if ( splits.length >=5 ) {
		    var entry = {
			minutes: splits[0],
			hours: splits[1],
			days: splits[2],
			months: splits[3],
			weekdays: splits[4],
			value: splits[5]
		    };
		    obj.entries.push(entry)
		}
		else {
		    self.logger.error(line);
		    self.logger.error(matches);
		    self.logger.error(splits);
		}
	    }
	});
	self.newModel.children.push(obj);
    };

    ImportGLM.prototype.saveSchedule = function(obj, parentNode) {
	var self = this;
	// save obj as node here (set the properties of the schedule)
	var schedNode = self.core.createNode({parent: parentNode, base: self.META.schedule});
	self.core.setAttribute(schedNode, 'name', obj.name);
	var i =0;
	obj.entries.map(function(entry) {
	    var entryNode = self.core.createNode({parent: schedNode, base: self.META.schedule_entry});
	    self.core.setAttribute(entryNode, 'name', 'Entry ' + i++);
	    //self.logger.error(JSON.stringify(entry, null, 2));
	    for (var a in entry) {
		var val = entry[a];
		self.core.setAttribute(entryNode, a, val);
	    }
	});
    };

    ImportGLM.prototype.parseMultiRecorder = function(str, obj) {
	var self = this;
	var splitString = /[\s;]+/gi;
	var splits;
	var lines = str.split('\n');
	lines.map(function(line) {
	    splits = line.split(splitString)
		.filter(function(obj) { return obj.length > 0; });
	    if ( splits.length > 0 && splits[0].indexOf('/') == -1 ) {
		obj.attributes[splits[0]] = splits[1];
	    }
	});
	self.newModel.children.push(obj);
    };

    ImportGLM.prototype.saveMultiRecorder = function(obj, parentNode) {
	var self = this;
	// save obj as node here (set the properties of the multiRecorder)
	var recorderNode = self.core.createNode({parent: parentNode, base: self.META.multi_recorder});
	self.core.setAttribute(recorderNode, 'name', obj.name);
	for (var a in obj.attributes) {
	    var val = obj.attributes[a];
	    self.core.setAttribute(recorderNode, a, val);
	}
    };

    ImportGLM.prototype.parseLine = function(str, obj) {
	var self = this;
    };

    ImportGLM.prototype.saveLine = function(obj, parentNode) {
	var self = this;
	// save obj as node here (set the properties of the multiRecorder)
	var lineNode = self.core.createNode({parent: parentNode, base: self.META[obj.type]});
	self.core.setAttribute(lineNode, 'name', obj.name);
	for (var a in obj.attributes) {
	    var val = obj.attributes[a];
	    self.core.setAttribute(lineNode, a, val);
	}
	for (var p in obj.pointers) {
	    var nodeRef = obj.pointers[a];

	}
	obj.node = lineNode;
    };

    ImportGLM.prototype.parseObject = function(str, parent) {
	var self = this;
	var splitString = /[\s:\{]+/gi;
	var submodel_str = '';
	var submodels = [];
	var currentObj = undefined;
	var depth = 0;
	var lines = str.split('\n');
	var splits;
	lines.map(function(line) {
	    if ( line.indexOf('{') > -1 ) {
		if ( depth == 0 ) {
		    splits = line.split(splitString).filter(function(obj) { return obj.length > 0; });
		    if ( splits.length > 0 && splits[0].indexOf('/') == -1 ) {
			var base = splits[0];
			var type = splits[1];
			var name;
			if (splits.length >= 3) {
			    name = splits[2];
			}
			currentObj = {
			    type: type,
			    base: base,
			    name: name,
			    children: [],
			    attributes: {},
			    pointers: {}
			};
		    }
		}
		else {
		    submodel_str += line +'\n';
		}
		depth += 1;
	    }
	    else if ( line.indexOf('}') > -1 ) {
		depth -= 1;
		if ( depth == 0 ) {
		    if (currentObj) {
			if (currentObj.base == 'clock') {
			    currentObj.type = currentObj.base;
			    currentObj.attributes = {};
			    self.parseClock(submodel_str, currentObj);
			}
			else if (currentObj.base == 'schedule') {
			    currentObj.name = currentObj.type;
			    currentObj.type = currentObj.base;
			    currentObj.attributes = {};
			    self.parseSchedule(submodel_str, currentObj);
			}
			else if (currentObj.type == 'multi_recorder') {
			    currentObj.name = currentObj.type;
			    currentObj.type = currentObj.base;
			    currentObj.attributes = {};
			    self.parseMultiRecorder(submodel_str, currentObj);
			}
			else {
			    submodels.push({string:submodel_str, object:currentObj});
			}
			currentObj = undefined;
			submodel_str = '';
		    }
		}
		else {
		    submodel_str += line + '\n';
		}
	    }
	    else {
		if (depth >= 1) {
		    // parse property here
		    splits = line.split(splitString).filter(function(obj) { return obj.length > 0; });
		    if (splits && splits[0].indexOf('/') == -1) { // don't want comments
			if (depth == 1)
			    currentObj.attributes[splits[0]] = splits[1].replace(';','');
			submodel_str += line + '\n';
		    }
		}
	    }
	});
	submodels.map(function(subModel) {
	    self.parseObject(subModel.string, subModel.object);
	    parent.children.push(subModel.object);
	});
    };

    ImportGLM.prototype.saveObject = function(obj, parent) {
	var self = this;
	if ( obj.type == 'clock' ){
	    self.saveClock(obj, parent);
	}
	else if ( obj.type == 'schedule' ){
	    self.saveSchedule(obj, parent);
	}
	else if ( obj.type == 'multi_recorder' ){
	    self.saveMultiRecorder(obj, parent);
	}
	else if ( obj.type ) {
	    var newNode = self.core.createNode({parent: parent, base: self.META[obj.type]});
	    if (obj.name) {
		self.core.setAttribute(newNode, 'name', obj.name);
	    }
	    for (var a in obj.attributes) {
		var val = obj.attributes[a];
		self.core.setAttribute(newNode, a, val);
	    }
	    obj.node = newNode;
	    obj.children.map(function(child) {
		self.saveObject(child, newNode);
	    });
	}
    };

    ImportGLM.prototype.resolveReferences = function(obj) {
	var self = this;
    };

    ImportGLM.prototype.createModelArtifacts = function() {
	// use self.newModel
	var self = this;
	var fcoNode = self.core.getBaseRoot(self.activeNode);
	var modelMetaNode = self.META.Model;
	var modelNode = self.core.createNode({parent: self.activeNode, base: modelMetaNode});
	self.core.setAttribute(modelNode, 'name', self.newModel.name);
	for (var ai in self.newModel.attributes) {
	    var val = self.newModel.attributes[ai];
	    self.core.setAttribute(modelNode, ai, val);
	}
	self.newModel.children.map(function(obj) {
	    self.saveObject(obj, modelNode);
	});
	self.newModel.children.map(function(obj) {
	    self.resolveReferences(obj);
	});
    };

    return ImportGLM;
});
<|endoftext|>


<commit_before>var normalize = require("./lib/normalize.js")
var error = require("./lib/error.js")

module.exports = enumDropdown

function enumDropdown(opts) {
    opts = normalize("enumDropdown", opts)

    if (!opts.options) {
        throw new Error("enumDropdown(opts): opts.options is required " +
            JSON.stringify(opts))
    }

    return [".string-dropdown.form-elem" + (opts.selector || ""), [
        ["label.label", [ opts.label ]],
        ["select.input", {
            name: opts.name,
            "data-marker": "forms." + opts.marker
        }, [
            ["option", { selected: true, value: "" }, opts.placeholder],
            { fragment: opts.options.map(function (option) {
                if (typeof option === "string") {
                    option = { text: option, value: option }
                }

                return ["option", { value: option.value }, option.text]
            }) }
        ]],
        error(opts.marker)
    ]]
}
<commit_msg>allow setting default selected<commit_after>var normalize = require("./lib/normalize.js")
var error = require("./lib/error.js")

module.exports = enumDropdown

function enumDropdown(opts) {
    opts = normalize("enumDropdown", opts)

    if (!opts.options) {
        throw new Error("enumDropdown(opts): opts.options is required " +
            JSON.stringify(opts))
    }

    return [".string-dropdown.form-elem" + (opts.selector || ""), [
        ["label.label", [ opts.label ]],
        ["select.input", {
            name: opts.name,
            "data-marker": "forms." + opts.marker
        }, [
            ["option", { selected: true, value: "" }, opts.placeholder],
            { fragment: opts.options.map(function (option) {
                if (typeof option === "string") {
                    option = { text: option, value: option }
                }

                return ["option", {
                    value: option.value,
                    selected: opts.value && option.value === opts.value
                }, option.text]
            }) }
        ]],
        error(opts.marker)
    ]]
}
<|endoftext|>


<commit_before>// Import requirements
var express = require('express')
var mongoose = require('mongoose')
var bodyParser = require('body-parser')
var morgan = require('morgan')

var clientRouter = require('./routers/client')
var representedRouter = require('./routers/represented')
var representativeRouter = require('./routers/representative')
var orderRouter = require('./routers/order')

// ============================================================
// Set express object
var app = express()

// ============================================================
// Set server port
var SERVER_PORT = 3000;

// ============================================================
// Connect to MongoDB
var db = 'mongodb://localhost/wm-representacoes'
mongoose.connect(db, function(err, res) {
  if (err) {
    console.log('Failed to connect to ' + db)
    console.log(err)
    console.log(res)
  }
  else {
    console.log('Connected to ' + db)
  }
})

// ============================================================
// User body-parser
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

// ============================================================
// Connection routers

// API
app.use('/api/client', clientRouter)
app.use('/api/represented', representedRouter)
app.use('/api/representative', representativeRouter)
app.use('/api/order', orderRouter)

// ============================================================
// Use Morgan for development
app.use(morgan('dev'))

// ============================================================
// Start express' static paging
app.use(express.static(__dirname + '/public'))

// ============================================================
// Start server
app.listen(SERVER_PORT, function(err) {
  if (err) {
    console.log('[ERROR]')
    console.log('Couldn\'t listen on port ' + SERVER_PORT)
  }
  else {
    console.log('Listening on port ' + SERVER_PORT)
  }
})

// ============================================================
// First time settings
var Model = require('./models/models')
states = ['ac', 'al', 'am', 'ap', 'ba', 'ce', 'df', 'es', 'go', 'ma', 'mg', 'ms',
          'mt', 'pa', 'pb', 'pe', 'pi', 'pr', 'rj', 'rn', 'ro', 'rr', 'rs', 'sc',
          'se', 'sp', 'to']

Model.City.findOne({}, function(err, city) {
  if (err) {
    console.log('[ERROR]')
    console.log('Couldn\'t communicate with mongodb on Model.City.findOne')
  }
  else if (city === null) {
    // No cities on database
    codigo = 0

    // Get list of cities for each state
    states.forEach(function(s) {
      state_code = states.indexOf(s)

      // Load cities for that state
      cities = require('./cities/' + s)

      // Insert cities on database
      cities.forEach(function(c) {
        ++codigo
        new_city = {
          nome: c.name,
          estado: state_code+1,
          codigo: codigo
        }

        city = Model.City(new_city)
        city.save(function(err) {
          if (err) {
            console.log('[ERROR]')
            console.log('Couldn\'t save city ' + cities[idx-1].name + ' (' + codigo-1 + ') of state ' + s)
          }
        })
      })
    })

    console.log('Cities added to database')
  }
})

Model.State.findOne({}, function(err, state) {
  if (err) {
    console.log('[ERROR]')
    console.log('Couldn\'t communicate with mongodb on Model.State.findOne')
  }
  else if (state === null) {
    // No states on database
    codigo = 0

    // Insert state on DB
    states.forEach(function(s) {
      ++codigo
      new_state = {
        uf: states[codigo-1],
        codigo: codigo
      }

      state = Model.State(new_state)
      state.save(function(err) {
        if (err) {
          console.log('[ERROR]')
          console.log('Couldn\'t save state ' + s + ' (' + codigo-1 + ')')
        }
      })
    })

    console.log('States added to database')
  }
})
<commit_msg>Minor correction on server.js<commit_after>// Import requirements
var express = require('express')
var mongoose = require('mongoose')
var bodyParser = require('body-parser')
var morgan = require('morgan')

var clientRouter = require('./routers/client')
var representedRouter = require('./routers/represented')
var representativeRouter = require('./routers/representative')
var orderRouter = require('./routers/order')

// ============================================================
// Set express object
var app = express()

// ============================================================
// Set server port
var SERVER_PORT = 3000;

// ============================================================
// Connect to MongoDB
var db = 'mongodb://localhost/wm-representacoes'
mongoose.connect(db, function(err, res) {
  if (err) {
    console.log('Failed to connect to ' + db)
    console.log(err)
    console.log(res)
  }
  else {
    console.log('Connected to ' + db)
  }
})

// ============================================================
// User body-parser
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

// ============================================================
// Connection routers

// API
app.use('/api/client', clientRouter)
app.use('/api/represented', representedRouter)
app.use('/api/representative', representativeRouter)
app.use('/api/order', orderRouter)

// ============================================================
// Use Morgan for development
app.use(morgan('dev'))

// ============================================================
// Start express' static paging
app.use(express.static(__dirname + '/public'))

// ============================================================
// Start server
app.listen(SERVER_PORT, function(err) {
  if (err) {
    console.log('[ERROR]')
    console.log('Couldn\'t listen on port ' + SERVER_PORT)
  }
  else {
    console.log('Listening on port ' + SERVER_PORT)
  }
})

// ============================================================
// First time settings
var Model = require('./models/models')
states = ['ac', 'al', 'am', 'ap', 'ba', 'ce', 'df', 'es', 'go', 'ma', 'mg', 'ms',
          'mt', 'pa', 'pb', 'pe', 'pi', 'pr', 'rj', 'rn', 'ro', 'rr', 'rs', 'sc',
          'se', 'sp', 'to']

Model.City.findOne({}, function(err, city) {
  if (err) {
    console.log('[ERROR]')
    console.log('Couldn\'t communicate with mongodb on Model.City.findOne')
  }
  else if (city === null) {
    // No cities on database
    codigo = 0

    // Get list of cities for each state
    states.forEach(function(s) {
      state_code = states.indexOf(s)

      // Load cities for that state
      cities = require('./cities/' + s)

      // Insert cities on database
      cities.forEach(function(c) {
        ++codigo
        new_city = {
          nome: c.name,
          estado: state_code+1,
          codigo: codigo
        }

        city = Model.City(new_city)
        city.save(function(err) {
          if (err) {
            console.log('[ERROR]')
            console.log('Couldn\'t save city ' + c.name + ' (' + codigo-1 + ') of state ' + s)
          }
        })
      })
    })

    console.log('Cities added to database')
  }
})

Model.State.findOne({}, function(err, state) {
  if (err) {
    console.log('[ERROR]')
    console.log('Couldn\'t communicate with mongodb on Model.State.findOne')
  }
  else if (state === null) {
    // No states on database
    codigo = 0

    // Insert state on DB
    states.forEach(function(s) {
      ++codigo
      new_state = {
        uf: states[codigo-1],
        codigo: codigo
      }

      state = Model.State(new_state)
      state.save(function(err) {
        if (err) {
          console.log('[ERROR]')
          console.log('Couldn\'t save state ' + s + ' (' + codigo-1 + ')')
        }
      })
    })

    console.log('States added to database')
  }
})
<|endoftext|>


<commit_before>/*
 The MIT License (MIT)

 Copyright ( c ) 2014 - 2015 Teem2 LLC

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/
var http = require('http');
var path = require('path');
http.globalAgent.maxSockets = 25;
var express = require('express');
var app = express();
var fs = require('fs');

var compress = require('compression')
app.use(compress());

var components = {};
var privateComponents = {};

var componentsFiles = fs.readdirSync("./components")
for (var i = 0, len = componentsFiles.length; i < len; i++) {
  var fileName = componentsFiles[i];
  var component = require("./components/" + fileName);
  components[fileName.replace('.js', '')] = component;
  console.log('Loading Component: ', fileName.replace('.js', ''));
}

var server,
  dreemroot = path.normalize(__dirname + "/" + process.env.DREEM_ROOT),
  projectsroot,
  assembler = components['assembler'],
//  apiProxy = components['apiproxy'],
  validator = components['validator'],
  watchfile = components['watchfile'],
  smokerun = components['smokerun'],
  saucerun = components['saucerun'],
  streem = components['streem'],
  info = components['info'],
  wrapper = components['wrapper'];

console.log('serving Dreem from', dreemroot);
if (process.env.DREEM_PROJECTS_ROOT) {
  projectsroot = path.normalize(__dirname + "/" + process.env.DREEM_PROJECTS_ROOT);
  console.log('serving project root from', projectsroot);
}

if (process.env.DREEM_COMPONENTS_ROOT) {
  componentsroot = path.normalize(__dirname + "/" + process.env.DREEM_COMPONENTS_ROOT);

  if (!fs.existsSync(componentsroot)) {
    console.warn('DREEM_COMPONENTS_ROOT dir not found, no components loaded.');
    return;
  }
  console.log('Loading private components from', componentsroot);

  var componentDirs = fs.readdirSync(componentsroot)
  for (var i = 0, len = componentDirs.length; i < len; i++) {
    var dirName = componentDirs[i];

    var componentDir = componentsroot + '/' + dirName;
    var componentDescriptorPath = componentDir + '/package.json'
    var componentMainPath = componentDir + '/index.js'

    if (!fs.existsSync(componentDescriptorPath)) {
      console.warn('Could not load component from directory', dirName, 'because no package.json found.');
      continue;
    }

    if (!fs.existsSync(componentMainPath)) {
      console.warn('Could not load component from directory', dirName, 'because no index.js found.');
      continue;
    }

    var component = require(componentMainPath);
    if (!component.initialize) {
      console.warn('Could not load component from directory', dirName, 'because no initialize method found.');
      continue;
    }

    var pjson = require(componentDescriptorPath);
    console.log('Loading component', pjson.name);
    privateComponents[pjson.name] = {"component":component, "component_dir":componentDir};
  }
}

// Start:Routing
app.use(function(req, res, next) {
  if (req.url.match(/^\/(css|js|img|font|api)\/.+/)) {
    res.setHeader('Cache-Control', 'public, max-age=3600');
  }
  next();
});
if (wrapper) app.get(/\.dre$/, wrapper(projectsroot, dreemroot));
if (assembler) app.all('/core/*', assembler(projectsroot, dreemroot, 'core' + path.sep));
app.use(express.static(dreemroot));

if (projectsroot) app.use('/projects', express.static(projectsroot));
if (validator) app.get(/^\/(validate).+/, validator(projectsroot, dreemroot));
if (watchfile) app.get(/^\/(watchfile).+/, watchfile(projectsroot, dreemroot));
if (smokerun) {
  app.get(/^\/smokerun.*/, smokerun.get(projectsroot, dreemroot));
  app.post(/^\/smokerun.*/, smokerun.post(projectsroot, dreemroot));
}
if (saucerun) app.get(/^\/saucerun.*/, saucerun.get(projectsroot, dreemroot));
if (info) app.get(/^\/(version|info)/, info());
// End:Routing

for (var compName in privateComponents) {
  var comp = privateComponents[compName];
  var expressComp = comp['component'];

//  init the server component
  expressComp.initialize(app);

//  Now find all of the .dre files and serve them under the /classes/ path
  var glob = require("glob");
  var dres = glob.sync(comp['component_dir'] + '/dre/' + "**/*.dre");

  for (var i=0; i<dres.length; i++) {
    var dreemFilePath = dres[i];

    var ext = '.dre';
    if (dreemFilePath.indexOf(ext, dreemFilePath.length - ext.length) == -1) continue; //skip non-dre files

    var dreemFileName = dreemFilePath.substring(dreemFilePath.lastIndexOf('/')+1, dreemFilePath.length);

    var nameParts = compName.split('-');
    if (nameParts[nameParts.length-1] == dreemFileName.replace('.dre', '')) {
      nameParts.pop();
    }
    nameParts.push(dreemFileName);

    app.use('/classes/' + nameParts.join('/'), express.static(dreemFilePath))
  }
}

server = http.createServer(app);
if (streem) streem.startServer(server);

//var vfs = require('vfs-local')({
//   root: dreemroot,
//   httpRoot: root,
//});
//app.use(require('vfs-http-adapter')("/fs/", vfs));

server.listen(process.env.PORT || 8080, process.env.IP || "0.0.0.0", function() {
  var addr = server.address();
  console.log("server listening at", addr.address + ":" + addr.port);
});
<commit_msg>moved private component dre loading above the redirect of .dre files to docs because the wrapper redirect was intercepting the private component requests<commit_after>/*
 The MIT License (MIT)

 Copyright ( c ) 2014 - 2015 Teem2 LLC

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/
var http = require('http');
var path = require('path');
http.globalAgent.maxSockets = 25;
var express = require('express');
var app = express();
var fs = require('fs');

var compress = require('compression')
app.use(compress());

var components = {};
var privateComponents = {};

var componentsFiles = fs.readdirSync("./components")
for (var i = 0, len = componentsFiles.length; i < len; i++) {
  var fileName = componentsFiles[i];
  var component = require("./components/" + fileName);
  components[fileName.replace('.js', '')] = component;
  console.log('Loading Component: ', fileName.replace('.js', ''));
}

var server,
  dreemroot = path.normalize(__dirname + "/" + process.env.DREEM_ROOT),
  projectsroot,
  assembler = components['assembler'],
//  apiProxy = components['apiproxy'],
  validator = components['validator'],
  watchfile = components['watchfile'],
  smokerun = components['smokerun'],
  saucerun = components['saucerun'],
  streem = components['streem'],
  info = components['info'],
  wrapper = components['wrapper'];

console.log('serving Dreem from', dreemroot);
if (process.env.DREEM_PROJECTS_ROOT) {
  projectsroot = path.normalize(__dirname + "/" + process.env.DREEM_PROJECTS_ROOT);
  console.log('serving project root from', projectsroot);
}

if (process.env.DREEM_COMPONENTS_ROOT) {
  componentsroot = path.normalize(__dirname + "/" + process.env.DREEM_COMPONENTS_ROOT);

  if (!fs.existsSync(componentsroot)) {
    console.warn('DREEM_COMPONENTS_ROOT dir not found, no components loaded.');
    return;
  }
  console.log('Loading private components from', componentsroot);

  var componentDirs = fs.readdirSync(componentsroot)
  for (var i = 0, len = componentDirs.length; i < len; i++) {
    var dirName = componentDirs[i];

    var componentDir = componentsroot + '/' + dirName;
    var componentDescriptorPath = componentDir + '/package.json'
    var componentMainPath = componentDir + '/index.js'

    if (!fs.existsSync(componentDescriptorPath)) {
      console.warn('Could not load component from directory', dirName, 'because no package.json found.');
      continue;
    }

    if (!fs.existsSync(componentMainPath)) {
      console.warn('Could not load component from directory', dirName, 'because no index.js found.');
      continue;
    }

    var component = require(componentMainPath);
    if (!component.initialize) {
      console.warn('Could not load component from directory', dirName, 'because no initialize method found.');
      continue;
    }

    var pjson = require(componentDescriptorPath);
    console.log('Loading component', pjson.name);
    privateComponents[pjson.name] = {"component":component, "component_dir":componentDir};
  }
}

// Start:Routing
app.use(function(req, res, next) {
  if (req.url.match(/^\/(css|js|img|font|api)\/.+/)) {
    res.setHeader('Cache-Control', 'public, max-age=3600');
  }
  next();
});

for (var compName in privateComponents) {
  var comp = privateComponents[compName];
  var expressComp = comp['component'];

//  init the server component
  expressComp.initialize(app);

//  Now find all of the .dre files and serve them under the /classes/ path
  var glob = require("glob");
  var dres = glob.sync(comp['component_dir'] + '/dre/' + "**/*.dre");

  for (var i=0; i<dres.length; i++) {
    var dreemFilePath = dres[i];

    var ext = '.dre';
    if (dreemFilePath.indexOf(ext, dreemFilePath.length - ext.length) == -1) continue; //skip non-dre files

    var dreemFileName = dreemFilePath.substring(dreemFilePath.lastIndexOf('/')+1, dreemFilePath.length);

    var nameParts = compName.split('-');
    if (nameParts[nameParts.length-1] == dreemFileName.replace('.dre', '')) {
      nameParts.pop();
    }
    nameParts.push(dreemFileName);

    app.use('/classes/' + nameParts.join('/'), express.static(dreemFilePath))
  }
}

if (wrapper) app.get(/\.dre$/, wrapper(projectsroot, dreemroot));
if (assembler) app.all('/core/*', assembler(projectsroot, dreemroot, 'core' + path.sep));
app.use(express.static(dreemroot));

if (projectsroot) app.use('/projects', express.static(projectsroot));
if (validator) app.get(/^\/(validate).+/, validator(projectsroot, dreemroot));
if (watchfile) app.get(/^\/(watchfile).+/, watchfile(projectsroot, dreemroot));
if (smokerun) {
  app.get(/^\/smokerun.*/, smokerun.get(projectsroot, dreemroot));
  app.post(/^\/smokerun.*/, smokerun.post(projectsroot, dreemroot));
}
if (saucerun) app.get(/^\/saucerun.*/, saucerun.get(projectsroot, dreemroot));
if (info) app.get(/^\/(version|info)/, info());
// End:Routing

server = http.createServer(app);
if (streem) streem.startServer(server);

//var vfs = require('vfs-local')({
//   root: dreemroot,
//   httpRoot: root,
//});
//app.use(require('vfs-http-adapter')("/fs/", vfs));

server.listen(process.env.PORT || 8080, process.env.IP || "0.0.0.0", function() {
  var addr = server.address();
  console.log("server listening at", addr.address + ":" + addr.port);
});
<|endoftext|>


<commit_before>var maniUtils = _altboilerScope.maniUtils
var boilerUtils = _altboilerScope.boilerUtils
var configUtils = _altboilerScope.configUtils
var StaticUtils = _altboilerScope.StaticUtils

/*********************************
 *********** DEFINITION **********
 *********************************/

/* altboiler.Boilerplate()
 * returns the rendered boilerplate
 * It renderes the template `assets/main.html`
 */
Altboiler.Boilerplate = (function () {
  var mainTemplate = SpacebarsCompiler.compile(Assets.getText('assets/main.html'), { isBody: true })
  return function Boilerplate (config) {
    config = config || this.configuration
    return Blaze.toHTML(Blaze.With(
      boilerUtils.getBoilerTemplateData(
        maniUtils.getIncludes(),
        configUtils.execFuncs(config.action),
        config,
        this._staticFiles.get('script').url
      ),
      eval(mainTemplate)
    ))
  }
})()

/* altboiler.serveBoilerplate(req, res, next)
 * `req` - `Object`: A request-obeject as defined by the node-docs
 * `res` - `Object`: A response-obeject as defined by the node-docs
 * `next` - `Function`: The next function on the stack check the [connect docs](https://www.npmjs.com/package/connect) for more info
 * The function used to serve the boilerplate
 * It manipulates the tempConf and checks showLoader
 */
Altboiler.serveBoilerplate = function serveBoilerplate (req, res, next) {
  var config = this.getConfig()
  this.tmpConf = {}
  if(configUtils.isTruthy(config.showLoader)) {
    res.end(this.Boilerplate(config))
  } else {
    next()
  }
}

/* Altboiler.addFiles()
 * Adds all files to staticUtils
 */
Altboiler.addFiles = function AltboilerAddFiles () {
  this._staticFiles.add(
    'script',
    this.getConfig('routes').script,
    maniUtils
      .getScripts(maniUtils.getIncludes()['js'])
  )
  this._staticFiles.add(
    'styles',
    this.getConfig('routes').styles,
    []
      .concat(this.getConfig('css'))
      .join('\n')
  )
}

/*********************************
 ********** CONFIGURATION ********
 *********************************/

altboiler._staticFiles = Object.create(StaticUtils)
altboiler.addFiles()

/* Configuration options that can be set server-side */
altboiler.config({
    /* HTML that should be loaded and not removed onLoad */
    content: '',

    /* HTML that is removed onLoad */
    action: Assets.getText('assets/default.html'),

    /* CSS that is served with the boilerplate, is also served when the normal boilerplate is loaded */
    css: [Assets.getText('assets/styles.css')],

    /* JS that is removed onLoad */
    js: [Assets.getText('assets/fader.js')],
})

/*********************************
 ***** LISTEN FOR CONNECTIONS ****
 *********************************/

/*
 * This listens for all routes,
 * but doesn't conflict with any resources
 * I'm putting this to the end of the queue
 * to make sure it gets added last.
 * If I wouldn't do this, iron:router server-side would break.
 * This isn't perfect, because a component of your app might
 * try add a handler to serve a ressource asynchronously.
 * That's why the `showLoader` options exists.
 */
_.defer(function () {
  WebApp.connectHandlers.use(
    altboiler.serveBoilerplate.bind(altboiler)
  )
})

/*
 * This serves the concatenated app script.
 */
; (function () {
  var scriptFile = altboiler._staticFiles.get('script')
  WebApp.rawConnectHandlers.use(
    scriptFile.path,
    altboiler._staticFiles.server('script')
  )
})()

/*
 * This route serves the static css you added via the configuration
 */
WebApp.rawConnectHandlers.use(
  altboiler._staticFiles.get('styles').path,
  altboiler._staticFiles.server('styles')
)
<commit_msg>Fix the static CSS served<commit_after>var maniUtils = _altboilerScope.maniUtils
var boilerUtils = _altboilerScope.boilerUtils
var configUtils = _altboilerScope.configUtils
var StaticUtils = _altboilerScope.StaticUtils

/*********************************
 *********** DEFINITION **********
 *********************************/

/* altboiler.Boilerplate()
 * returns the rendered boilerplate
 * It renderes the template `assets/main.html`
 */
Altboiler.Boilerplate = (function () {
  var mainTemplate = SpacebarsCompiler.compile(Assets.getText('assets/main.html'), { isBody: true })
  return function Boilerplate (config) {
    config = config || this.configuration
    return Blaze.toHTML(Blaze.With(
      boilerUtils.getBoilerTemplateData(
        maniUtils.getIncludes(),
        configUtils.execFuncs(config.action),
        config,
        this._staticFiles.get('script').url
      ),
      eval(mainTemplate)
    ))
  }
})()

/* altboiler.serveBoilerplate(req, res, next)
 * `req` - `Object`: A request-obeject as defined by the node-docs
 * `res` - `Object`: A response-obeject as defined by the node-docs
 * `next` - `Function`: The next function on the stack check the [connect docs](https://www.npmjs.com/package/connect) for more info
 * The function used to serve the boilerplate
 * It manipulates the tempConf and checks showLoader
 */
Altboiler.serveBoilerplate = function serveBoilerplate (req, res, next) {
  var config = this.getConfig()
  this.tmpConf = {}
  if(configUtils.isTruthy(config.showLoader)) {
    res.end(this.Boilerplate(config))
  } else {
    next()
  }
}

/* Altboiler.addFiles()
 * Adds all files to staticUtils
 */
Altboiler.addFiles = function AltboilerAddFiles () {
  this._staticFiles.add(
    'script',
    this.getConfig('routes').script,
    maniUtils
      .getScripts(maniUtils.getIncludes()['js'])
  )
  this._staticFiles.add(
    'styles',
    this.getConfig('routes').styles,
    []
      .concat(this.getConfig('css'))
      .join('\n')
  )
}

/*********************************
 ********** CONFIGURATION ********
 *********************************/

/* Configuration options that can be set server-side */
altboiler.config({
    /* HTML that should be loaded and not removed onLoad */
    content: '',

    /* HTML that is removed onLoad */
    action: Assets.getText('assets/default.html'),

    /* CSS that is served with the boilerplate, is also served when the normal boilerplate is loaded */
    css: [Assets.getText('assets/styles.css')],

    /* JS that is removed onLoad */
    js: [Assets.getText('assets/fader.js')],
})

altboiler._staticFiles = Object.create(StaticUtils)
altboiler.addFiles()

/*********************************
 ***** LISTEN FOR CONNECTIONS ****
 *********************************/

/*
 * This listens for all routes,
 * but doesn't conflict with any resources
 * I'm putting this to the end of the queue
 * to make sure it gets added last.
 * If I wouldn't do this, iron:router server-side would break.
 * This isn't perfect, because a component of your app might
 * try add a handler to serve a ressource asynchronously.
 * That's why the `showLoader` options exists.
 */
_.defer(function () {
  WebApp.connectHandlers.use(
    altboiler.serveBoilerplate.bind(altboiler)
  )
})

/*
 * This serves the concatenated app script.
 */
WebApp.rawConnectHandlers.use(
  altboiler._staticFiles.get('script').path,
  altboiler._staticFiles.server('script')
)

/*
 * This route serves the static css you added via the configuration
 */
WebApp.rawConnectHandlers.use(
  altboiler._staticFiles.get('styles').path,
  altboiler._staticFiles.server('styles')
)
<|endoftext|>