text: /*!
   * Copyright 2013 Apereo Foundation (AF) Licensed under the
   * Educational Community License, Version 2.0 (the "License"); you may
   * not use this file except in compliance with the License. You may
   * obtain a copy of the License at
   *
   *     http://opensource.org/licenses/ECL-2.0
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an "AS IS"
   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
   * or implied. See the License for the specific language governing
   * permissions and limitations under the License.
   */
  
  var _ = require('underscore');
  
  var OAE = require('oae-util/lib/oae');
  var OaeUtil = require('oae-util/lib/util');
  
  var PrincipalsAPI = require('./api');
  
  /*!
   * Create a group
   */
  OAE.tenantServer.post('/api/group/create', function(req, res) {
      req.telemetryUrl = '/api/group/create';
      var managers = req.body.managers || [];
      var members = req.body.members || [];
      if (managers && !Array.isArray(managers)) {
          managers = [managers];
      }
      if (members && !Array.isArray(members)) {
          members = [members];
      }
  
      var memberHash = {};
      for (var i = 0; i < managers.length; i++) {
          memberHash[managers[i]] = 'manager';
      }
      for (var j = 0; j < members.length; j++) {
          memberHash[members[j]] = 'member';
      }
  
      PrincipalsAPI.createGroup(req.ctx, req.body.displayName, req.body.description, req.body.visibility, req.body.joinable, memberHash, {}, function(err, groupId) {
          if (err) {
              return res.send(err.code, err.msg);
          }
  
          PrincipalsAPI.getGroup(req.ctx, groupId, function(err, group) {
              if (err){
                  return res.send(err.code, err.msg);
              }
              res.send(201, group);
          });
      });
  });
  
  /*!
   * Get a full group profile by its id
   */
  OAE.tenantServer.get('/api/group/:id', function(req, res) {
      req.telemetryUrl = '/api/group/id';
      PrincipalsAPI.getFullGroupProfile(req.ctx, req.params.id, function(err, group) {
          if (err) {
              return res.send(err.code, err.msg);
          }
          res.send(200, group);
      });
  });
  
  /*!
   * Update a group profile
   */
  OAE.tenantServer.post('/api/group/:id', function(req, res) {
      req.telemetryUrl = '/api/group/id';
      // Get the fields we wish to update.
      PrincipalsAPI.updateGroup(req.ctx, req.params.id, req.body, function(err, updatedGroup) {
          if (err) {
              return res.send(err.code, err.msg);
          }
          res.send(200, updatedGroup);
      });
  });
  
  /*!
   * Get the members of a group
   */
  OAE.tenantServer.get('/api/group/:id/members', function(req, res) {
      req.telemetryUrl = '/api/group/id/members';
      var limit = OaeUtil.getNumberParam(req.query.limit, 10, 1, 25);
      PrincipalsAPI.getGroupMembers(req.ctx, req.params.id, req.query.start, limit, function(err, members, nextToken) {
          if (err) {
              return res.send(err.code, err.msg);
          }
          res.send(200, {'results': members, 'nextToken': nextToken});
      });
  });
  
  /*!
   * Update the members of a group
   */
  OAE.tenantServer.post('/api/group/:id/members', function(req, res) {
      req.telemetryUrl = '/api/group/id/members';
      // Convert the string 'false' to a proper boolean.
      var members = req.body;
      var principals = _.keys(members);
      for (var i = 0; i < principals.length; i++) {
          if (members[principals[i]] === 'false') {
              members[principals[i]] = false;
          }
      }
      PrincipalsAPI.setGroupMembers(req.ctx, req.params.id, members, function(err) {
          if (err) {
              return res.send(err.code, err.msg);
          }
          res.send(200);
      });
  });
  
  /*!
   * Join a group as the current user
   */
  OAE.tenantServer.post('/api/group/:id/join', function(req, res) {
      req.telemetryUrl = '/api/group/id/join';
      PrincipalsAPI.joinGroup(req.ctx, req.params.id, function(err) {
          if (err) {
              return res.send(err.code, err.msg);
          }
  
          res.send(200);
      });
  });
  
  /*!
   * Leave a group as the current user
   */
  OAE.tenantServer.post('/api/group/:id/leave', function(req, res) {
      req.telemetryUrl = '/api/group/id/leave';
      PrincipalsAPI.leaveGroup(req.ctx, req.params.id, function(err) {
          if (err) {
              return res.send(err.code, err.msg);
          }
  
          res.send(200);
      });
  });
  
  /*!
   * Set / update a group's profile picture
   */
  OAE.tenantServer.post('/api/group/:id/picture', function(req, res) {
      req.telemetryUrl = '/api/group/id/picture';
      req.files = req.files || {};
      PrincipalsAPI.storePicture(req.ctx, req.params.id, req.files.file, function(err, principal) {
          if (err) {
              return res.send(err.code, err.msg);
          }
  
          // Set the response type to text/plain, as the UI uses an iFrame upload mechanism to support IE9
          // file uploads. If the response type is not set to text/plain, IE9 will try to download the response.
          res.set('Content-Type', 'text/plain');
          res.send(200, principal);
      });
  });
  