events_controller.rb 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  2. #
  3. # Copyright (c) 2016, Electric Power Research Institute (EPRI)
  4. # All rights reserved.
  5. #
  6. # OpenADR ("this software") is licensed under BSD 3-Clause license.
  7. #
  8. # Redistribution and use in source and binary forms, with or without modification,
  9. # are permitted provided that the following conditions are met:
  10. #
  11. # * Redistributions of source code must retain the above copyright notice, this
  12. # list of conditions and the following disclaimer.
  13. #
  14. # * Redistributions in binary form must reproduce the above copyright notice,
  15. # this list of conditions and the following disclaimer in the documentation
  16. # and/or other materials provided with the distribution.
  17. #
  18. # * Neither the name of EPRI nor the names of its contributors may
  19. # be used to endorse or promote products derived from this software without
  20. # specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. # OF SUCH DAMAGE.
  32. #
  33. # This EPRI software incorporates work covered by the following copyright and permission
  34. # notices. You may not use these works except in compliance with their respective
  35. # licenses, which are provided below.
  36. #
  37. # These works are provided by the copyright holders and contributors "as is" and any express or
  38. # implied warranties, including, but not limited to, the implied warranties of merchantability
  39. # and fitness for a particular purpose are disclaimed.
  40. #
  41. #########################################################################################
  42. # MIT Licensed Libraries
  43. #########################################################################################
  44. #
  45. # * actionmailer 3.2.12 (http://www.rubyonrails.org) - Email composition, delivery, and receiving framework (part of Rails).
  46. # * actionpack 3.2.12 (http://www.rubyonrails.org) - Web-flow and rendering framework putting the VC in MVC (part of Rails).
  47. # * activemodel 3.2.12 (http://www.rubyonrails.org) - A toolkit for building modeling frameworks (part of Rails).
  48. # * activerecord 3.2.12 (http://www.rubyonrails.org) - Object-relational mapper framework (part of Rails).
  49. # * activeresource 3.2.12 (http://www.rubyonrails.org) - REST modeling framework (part of Rails).
  50. # * activesupport 3.2.12 (http://www.rubyonrails.org) - A toolkit of support libraries and Ruby core extensions extracted from the Rails framework.
  51. # * arel 3.0.2 (http://github.com/rails/arel) - Arel is a SQL AST manager for Ruby
  52. # * bootstrap-sass 3.1.1.0 (https://github.com/twbs/bootstrap-sass) - Twitter's Bootstrap, converted to Sass and ready to drop into Rails or Compass
  53. # * builder 3.0.4 (http://onestepback.org) - Builders for MarkUp.
  54. # * bundler 1.12.5 (http://bundler.io) - The best way to manage your application's dependencies
  55. # * capybara 2.4.4 (http://github.com/jnicklas/capybara) - Capybara aims to simplify the process of integration testing Rack applications, such as Rails, Sinatra or Merb
  56. # * coffee-rails 3.2.2 () - Coffee Script adapter for the Rails asset pipeline.
  57. # * coffee-script-source 1.6.3 (http://jashkenas.github.com/coffee-script/) - The CoffeeScript Compiler
  58. # * docile 1.1.5 (https://ms-ati.github.io/docile/) - Docile keeps your Ruby DSLs tame and well-behaved
  59. # * edn 1.0.0 () - 'edn implements a reader for Extensible Data Notation by Rich Hickey.'
  60. # * erubis 2.7.0 (http://www.kuwata-lab.com/erubis/) - a fast and extensible eRuby implementation which supports multi-language
  61. # * execjs 1.4.0 (https://github.com/sstephenson/execjs) - Run JavaScript code from Ruby
  62. # * factory_girl 4.5.0 (https://github.com/thoughtbot/factory_girl) - factory_girl provides a framework and DSL for defining and using model instance factories.
  63. # * factory_girl_rails 4.5.0 (http://github.com/thoughtbot/factory_girl_rails) - factory_girl_rails provides integration between factory_girl and rails 3
  64. # * gem-licenses 0.1.2 (http://github.com/dblock/gem-licenses) - List all gem licenses.
  65. # * hike 1.2.3 (http://github.com/sstephenson/hike) - Find files in a set of paths
  66. # * i18n 0.6.5 (http://github.com/svenfuchs/i18n) - New wave Internationalization support for Ruby
  67. # * jdbc-postgresql 9.2.1000 (https://github.com/rosenfeld/jdbc-postgresql) - PostgresSQL jdbc driver for JRuby
  68. # * journey 1.0.4 (http://github.com/rails/journey) - Journey is a router
  69. # * jquery-rails 3.0.4 (http://rubygems.org/gems/jquery-rails) - Use jQuery with Rails 3
  70. # * json-schema 2.6.2 (http://github.com/ruby-json-schema/json-schema/tree/master) - Ruby JSON Schema Validator
  71. # * mail 2.4.4 (http://github.com/mikel/mail) - Mail provides a nice Ruby DSL for making, sending and reading emails.
  72. # * metaclass 0.0.4 (http://github.com/floehopper/metaclass) - Adds a metaclass method to all Ruby objects
  73. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  74. # * mocha 1.1.0 (http://gofreerange.com/mocha/docs) - Mocking and stubbing library
  75. # * multi_json 1.7.9 (http://github.com/intridea/multi_json) - A common interface to multiple JSON libraries.
  76. # * nokogiri 1.6.5 (http://nokogiri.org) - Nokogiri (鋸) is an HTML, XML, SAX, and Reader parser
  77. # * polyglot 0.3.3 (http://github.com/cjheath/polyglot) - Augment 'require' to load non-Ruby file types
  78. # * rack-test 0.6.2 (http://github.com/brynary/rack-test) - Simple testing API built on Rack
  79. # * railties 3.2.12 (http://www.rubyonrails.org) - Tools for creating, working with, and running Rails applications.
  80. # * rake 10.1.0 (http://rake.rubyforge.org) - Ruby based make-like utility.
  81. # * rspec-core 2.14.3 (http://github.com/rspec/rspec-core) - rspec-core-2.14.3
  82. # * rspec-expectations 2.14.0 (http://github.com/rspec/rspec-expectations) - rspec-expectations-2.14.0
  83. # * rspec-mocks 2.14.1 (http://github.com/rspec/rspec-mocks) - rspec-mocks-2.14.1
  84. # * rspec-rails 2.14.0 (http://github.com/rspec/rspec-rails) - rspec-rails-2.14.0
  85. # * sass 3.2.9 (http://sass-lang.com/) - A powerful but elegant CSS compiler that makes CSS fun again.
  86. # * sass-rails 3.2.6 () - Sass adapter for the Rails asset pipeline.
  87. # * simplecov 0.9.0 (http://github.com/colszowka/simplecov) - Code coverage for Ruby 1.9+ with a powerful configuration library and automatic merging of coverage across test suites
  88. # * spork 1.0.0rc3 (http://github.com/sporkrb/spork) - spork
  89. # * therubyrhino 2.0.2 (http://github.com/cowboyd/therubyrhino) - Embed the Rhino JavaScript interpreter into JRuby
  90. # * thor 0.18.1 (http://whatisthor.com/) - A scripting framework that replaces rake, sake and rubigen
  91. # * tilt 1.4.1 (http://github.com/rtomayko/tilt/) - Generic interface to multiple Ruby template engines
  92. # * treetop 1.4.14 (https://github.com/cjheath/treetop) - A Ruby-based text parsing and interpretation DSL
  93. # * uglifier 2.1.2 (http://github.com/lautis/uglifier) - Ruby wrapper for UglifyJS JavaScript compressor
  94. # * xpath 2.0.0 (http://github.com/jnicklas/xpath) - Generate XPath expressions from Ruby
  95. # * blankslate 2.1.2.4 (http://github.com/masover/blankslate) - BlankSlate extracted from Builder.
  96. # * bourbon 3.1.8 (https://github.com/thoughtbot/bourbon) - Bourbon Sass Mixins using SCSS syntax.
  97. # * coffee-script 2.2.0 (http://github.com/josh/ruby-coffee-script) - Ruby CoffeeScript Compiler
  98. # * diff-lcs 1.2.4 (http://diff-lcs.rubyforge.org/) - Diff::LCS computes the difference between two Enumerable sequences using the McIlroy-Hunt longest common subsequence (LCS) algorithm
  99. # * jquery-ui-rails 4.0.3 (https://github.com/joliss/jquery-ui-rails) - jQuery UI packaged for the Rails asset pipeline
  100. # * parslet 1.4.0 (http://kschiess.github.com/parslet) - Parser construction library with great error reporting in Ruby.
  101. # * rack 1.4.5 (http://rack.github.com/) - a modular Ruby webserver interface
  102. # * rack-cache 1.2 (http://tomayko.com/src/rack-cache/) - HTTP Caching for Rack
  103. # * rack-ssl 1.3.3 (https://github.com/josh/rack-ssl) - Force SSL/TLS in your app.
  104. # * rails 3.2.12 (http://www.rubyonrails.org) - Full-stack web application framework.
  105. # * simplecov-html 0.8.0 (https://github.com/colszowka/simplecov-html) - Default HTML formatter for SimpleCov code coverage tool for ruby 1.9+
  106. # * tzinfo 0.3.37 (http://tzinfo.rubyforge.org/) - Daylight-savings aware timezone library
  107. # * warbler 1.4.0.beta1 (http://caldersphere.rubyforge.org/warbler) - Warbler chirpily constructs .war files of your Rails applications.
  108. #
  109. #########################################################################################
  110. # BSD Licensed Libraries
  111. #########################################################################################
  112. #
  113. # * activerecord-jdbc-adapter 1.2.9.1 (https://github.com/jruby/activerecord-jdbc-adapter) - Copyright (c) 2006-2012 Nick Sieger <nick@nicksieger.com>, Copyright (c) 2006-2008 Ola Bini <ola.bini@gmail.com>
  114. # * jdbc-postgres 9.2.1004 (https://github.com/jruby/activerecord-jdbc-adapter) - Copyright (c) 1997-2011, PostgreSQL Global Development Group
  115. # * d3js 3.5.16 (https://d3js.org/) Copyright (c) 2015 Mike Bostock
  116. #
  117. #########################################################################################
  118. # Ruby Licensed Libraries
  119. #########################################################################################
  120. #
  121. # * json 1.8.0 (http://json-jruby.rubyforge.org/) - JSON implementation for JRuby
  122. # * rubyzip 0.9.9 (http://github.com/aussiegeek/rubyzip) - rubyzip is a ruby module for reading and writing zip files
  123. # * httpclient 2.3.4.1 (http://github.com/nahi/httpclient) - gives something like the functionality of libwww-perl (LWP) in Ruby
  124. # * test-unit 2.5.5 (http://test-unit.rubyforge.org/) - test-unit - Improved version of Test::Unit bundled in Ruby 1.8.x.
  125. #
  126. #########################################################################################
  127. # Public domain - creative commons Licensed Libraries
  128. #########################################################################################
  129. #
  130. # * torquebox 3.1.2 (http://torquebox.org/) - TorqueBox Gem
  131. # * torquebox-cache 3.1.2 (http://torquebox.org/) - TorqueBox Cache Gem
  132. # * torquebox-configure 3.1.2 (http://torquebox.org/) - TorqueBox Configure Gem
  133. # * torquebox-core 3.1.2 (http://torquebox.org/) - TorqueBox Core Gem
  134. # * torquebox-messaging 3.1.2 (http://torquebox.org/) - TorqueBox Messaging Client
  135. # * torquebox-naming 3.1.2 (http://torquebox.org/) - TorqueBox Naming Client
  136. # * torquebox-rake-support 3.1.2 (http://torquebox.org/) - TorqueBox Rake Support
  137. # * torquebox-security 3.1.2 (http://torquebox.org/) - TorqueBox Security Gem
  138. # * torquebox-server 3.1.2 (http://torquebox.org/) - TorqueBox Server Gem
  139. # * torquebox-stomp 3.1.2 (http://torquebox.org/) - TorqueBox STOMP Support
  140. # * torquebox-transactions 3.1.2 (http://torquebox.org/) - TorqueBox Transactions Gem
  141. # * torquebox-web 3.1.2 (http://torquebox.org/) - TorqueBox Web Gem
  142. #
  143. #########################################################################################
  144. # Apache Licensed Libraries
  145. #########################################################################################
  146. #
  147. # * addressable 2.3.8 (https://github.com/sporkmonger/addressable) - URI Implementation
  148. # * bcrypt-ruby 3.0.1 (http://bcrypt-ruby.rubyforge.org) - OpenBSD's bcrypt() password hashing algorithm.
  149. # * database_cleaner 1.4.0 (http://github.com/bmabey/database_cleaner) - Strategies for cleaning databases. Can be used to ensure a clean state for testing.
  150. # * annotate 2.5.0 (http://github.com/ctran/annotate_models) - Annotates Rails Models, routes, fixtures, and others based on the database schema.
  151. # * nvd3 1.8.4 (http://nvd3.org/) Copeyright (c) 2014 Novus Partners - chart library based on d3js
  152. # * smack 3.3.1 (https://www.igniterealtime.org/projects/smack/) - XMPP library
  153. #
  154. #########################################################################################
  155. # LGPL
  156. #########################################################################################
  157. #
  158. # * jruby-1.7.4
  159. # * jruby-jars 1.7.4 (http://github.com/jruby/jruby/tree/master/gem/jruby-jars) - The core JRuby code and the JRuby stdlib as jar
  160. # ** JRuby is tri-licensed GPL, LGPL, and EPL.
  161. #
  162. #########################################################################################
  163. # MPL Licensed Libraries
  164. #########################################################################################
  165. #
  166. # * therubyrhino_jar 1.7.4 (http://github.com/cowboyd/therubyrhino) - Rhino's jars packed for therubyrhino
  167. #
  168. #########################################################################################
  169. # Artistic 2.0
  170. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  171. #
  172. #########################################################################################
  173. #
  174. #########################################################################################
  175. # GPL-2
  176. #########################################################################################
  177. # * mime-types 1.23 (http://mime-types.rubyforge.org/) - This library allows for the identification of a file's likely MIME content type
  178. #
  179. #########################################################################################
  180. # No License Given
  181. #########################################################################################
  182. #
  183. # * spork-testunit 0.0.8 (http://github.com/timcharper/spork-testunit) - spork-testunit
  184. # * sprockets 2.2.2 (http://getsprockets.org/) - Rack-based asset packaging system
  185. #
  186. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  187. # Ensures module definition in 'models/event_interface' will be loaded, even if `EventInterface` module is already
  188. # present due to its definition in, for example, 'models/event_interface/standard' (which is as far as Rails's
  189. # autoloading system would typically look before declaring, for example, a no method error)
  190. require_dependency 'event_interface'
  191. class EventsController < BaseController
  192. before_filter :logged_in_account
  193. before_filter :admin_account, except: [:index, :new, :create, :show, :update, :publish, :cancel, :add_vens, :remove_vens, :add_targets, :remove_targets, :destroy, :calendar]
  194. before_filter :correct_account, only: [:show, :edit, :update, :publish, :cancel, :add_vens, :remove_vens, :add_targets, :remove_targets, :destroy]
  195. before_filter :correct_account_for_nested_ven, only: [:add_vens, :remove_vens]
  196. before_filter { |c| c.set_parent_menu_item_class NavigationHelper::EVENTS_MENU_ITEM_CLASS }
  197. #####################################################################
  198. def cancel
  199. @event = Event.find(params[:id])
  200. if @event.cancel
  201. flash[:notice] = "Event cancelled."
  202. else
  203. flash[:error] = "Cannot cancel a completed event."
  204. end
  205. respond_to do |format|
  206. format.html { redirect_to event_path(@event) }
  207. #format.html { render :edit }
  208. format.json { head :no_content }
  209. end
  210. end
  211. #####################################################################
  212. def publish
  213. @event = Event.find(params[:id])
  214. if @event.publish
  215. flash[:notice] = "Event published."
  216. else
  217. flash[:error] = "Failed to publish event."
  218. end
  219. respond_to do |format|
  220. format.html { redirect_to event_path(@event) }
  221. #format.html { render :edit }
  222. format.json { head :no_content }
  223. end
  224. end
  225. #####################################################################
  226. def add_parties
  227. @event = Event.find(params[:id])
  228. begin
  229. market_context_subscriptions = params[:market_context_subscription][:id]
  230. market_context_subscriptions.each do |market_context_subscription_id|
  231. event_party = @event.event_parties.new
  232. event_party.market_context_subscription_id = market_context_subscription_id
  233. event_party.save
  234. end
  235. flash.now[:notice] = "Party added to event."
  236. rescue NoMethodError
  237. end
  238. render_tab "parties"
  239. end
  240. #####################################################################
  241. def remove_parties
  242. @event = Event.find(params[:id])
  243. event_parties = params[:event_party][:id]
  244. event_parties.each do |event_party_id|
  245. @event.event_parties.destroy(event_party_id)
  246. end
  247. flash.now[:notice] = "Party removed from event."
  248. # redirect_to_event
  249. render_tab "parties"
  250. end
  251. #####################################################################
  252. def add_vens
  253. @event = Event.find(params[:id])
  254. begin
  255. vens = params[:ven][:id]
  256. vens.each do |ven_id|
  257. event_ven = @event.event_vens.new
  258. event_ven.ven_id = ven_id
  259. event_ven.save
  260. end
  261. flash.now[:notice] = "VEN added to event."
  262. rescue NoMethodError
  263. end
  264. render_tab "vens"
  265. end
  266. #####################################################################
  267. def remove_vens
  268. @event = Event.find(params[:id])
  269. event_vens = params[:event_ven][:id]
  270. event_vens.each do |event_ven_id|
  271. @event.event_vens.destroy(event_ven_id)
  272. end
  273. flash.now[:notice] = "VEN removed from event."
  274. #redirect_to_event
  275. render_tab "vens"
  276. end
  277. #####################################################################
  278. def add_targets
  279. @event = Event.find(params[:id])
  280. target_ids = params[:target][:id]
  281. successful_additions_count = 0
  282. failed_additions_count = 0
  283. target_ids.each do |target_id|
  284. if @event.event_targets.create(target_id: target_id)
  285. successful_additions_count += 1
  286. else
  287. failed_additions_count += 1
  288. end
  289. end
  290. if successful_additions_count > 0
  291. @event.unpublish
  292. flash.now[:notice] = "#{ ActionController::Base.helpers.pluralize(successful_additions_count, 'Target') } added to event."
  293. end
  294. if failed_additions_count > 0
  295. flash.now[:error] = "#{ ActionController::Base.helpers.pluralize(failed_additions_count, 'Target') } could not be added to event."
  296. end
  297. render_tab "targets"
  298. end
  299. #####################################################################
  300. def remove_targets
  301. @event = Event.find(params[:id])
  302. target_ids = params[:target][:id]
  303. successful_removals_count = 0
  304. failed_removals_count = 0
  305. target_ids.each do |target_id|
  306. if @event.event_targets.find_by_target_id(target_id).destroy
  307. successful_removals_count += 1
  308. else
  309. failed_removals_count += 1
  310. end
  311. end
  312. if successful_removals_count > 0
  313. @event.unpublish
  314. flash.now[:notice] = "#{ ActionController::Base.helpers.pluralize(successful_removals_count, 'Target') } removed from event."
  315. end
  316. if failed_removals_count > 0
  317. flash.now[:error] = "#{ ActionController::Base.helpers.pluralize(failed_removals_count, 'Target') } could not be removed from event."
  318. end
  319. render_tab "targets"
  320. end
  321. #####################################################################
  322. def add_groups
  323. @event = Event.find(params[:id])
  324. begin
  325. groups = params[:group][:id]
  326. groups.each do |group_id|
  327. event_group = @event.event_groups.new
  328. event_group.group_id = group_id
  329. event_group.save
  330. end
  331. flash.now[:notice] = "Group added to event."
  332. rescue NoMethodError
  333. end
  334. render_tab "groups"
  335. end
  336. #####################################################################
  337. def remove_groups
  338. @event = Event.find(params[:id])
  339. event_groups = params[:event_group][:id]
  340. event_groups.each do |event_group_id|
  341. @event.event_groups.destroy(event_group_id)
  342. end
  343. flash.now[:notice] = "Group removed from event."
  344. # redirect_to_event
  345. render_tab "groups"
  346. end
  347. #####################################################################
  348. def add_resources
  349. @event = Event.find(params[:id])
  350. begin
  351. resources = params[:resource_type][:id]
  352. resources.each do |resource_id|
  353. event_resource = @event.event_resources.new
  354. event_resource.resource_type_id = resource_id
  355. event_resource.save
  356. end
  357. flash.now[:notice] = "Resource added to event."
  358. rescue NoMethodError
  359. end
  360. render_tab "resources"
  361. end
  362. #####################################################################
  363. def remove_resources
  364. @event = Event.find(params[:id])
  365. event_resources = params[:event_resource][:id]
  366. event_resources.each do |event_resource_id|
  367. @event.event_resources.destroy(event_resource_id)
  368. end
  369. flash.now[:notice] = "Resource removed from event."
  370. #redirect_to_event
  371. render_tab "resources"
  372. end
  373. #####################################################################
  374. def index
  375. # if current_account_is_admin
  376. # @events = Event.where("template <> true or template is null")
  377. # else
  378. # @events = Event.where(account_id: current_account).where("template <> true or template is null")
  379. # end
  380. # TODO: add try/catch
  381. if params[:commit]
  382. @events = Event.search(params, current_account.time_zone, current_account)
  383. # Instance variables used to "persist" search params across requests
  384. @market_context_ids = params[:market_context_ids]
  385. @status_ids = params[:status_ids]
  386. @comment_string = params[:comment_string]
  387. @first_date_string = params[:first_date_string]
  388. @last_date_string = params[:last_date_string]
  389. # These params are appended to paths of links to destroy action (so destroy can
  390. # then append them to the index path when handling redirect)
  391. @search_parameters = request.query_parameters
  392. else
  393. @first_date_string = (Time.now - 1.day).strftime("%a, %b %-d %Y")
  394. @last_date_string = (Time.now + 2.days).strftime("%a, %b %-d %Y")
  395. @events = Event.search(
  396. {first_date_string: @first_date_string, last_date_string: @last_date_string},
  397. current_account.time_zone, current_account
  398. )
  399. end
  400. @grouped_event_interface_class_names = grouped_available_event_interface_class_names
  401. end
  402. #####################################################################
  403. def show
  404. @event = Event.find(params[:id])
  405. type = @event.event_interface_name
  406. @event_interface = instantiate_type_specific_event_interface(type)
  407. @event_interface.show(params[:id], current_account)
  408. render "event_interfaces/#{ @event_interface.view_directory }/show"
  409. end
  410. #####################################################################
  411. def advanced_show
  412. @event = Event.find(params[:id])
  413. type = 'Standard'
  414. @event_interface = instantiate_type_specific_event_interface(type)
  415. @event_interface.show(params[:id], current_account)
  416. flash.now[:notice] = 'Editing advanced settings can break with expectations and cause problems. Proceed with caution.'
  417. render "event_interfaces/#{ @event_interface.view_directory }/show"
  418. end
  419. ########################################################
  420. def calendar
  421. @market_context_ids = params[:market_context_ids]
  422. if params[:year]
  423. # Build date string using whichever :year/:month/:date params are available (:month & :date default to '01')
  424. @date_string = []
  425. @date_string << params[:year]
  426. @date_string << (params[:month] ? params[:month] : "01")
  427. @date_string << (params[:date] ? params[:date] : "01")
  428. @date_string = @date_string.join("-")
  429. end
  430. # Ratio of pixels rendered (height, position, etc.) per minute of time (dtstart, duration, etc.)
  431. @view_pixels_per_minute = 1
  432. @time_zone = current_account.time_zone
  433. @date_string ||= Time.now.in_time_zone(@time_zone).strftime("%Y-%m-%d")
  434. # `view_context` allows controller action to use helper method
  435. @week_range = view_context.week_range_from_date_string_and_time_zone(@date_string, @time_zone)
  436. @array_of_dates_in_week_range = @week_range[0].to_date.step( @week_range[1].to_date ).to_a
  437. if @market_context_ids.present?
  438. events_in_week_range = Event.in_datetime_range(@week_range[0], @week_range[1]).where(market_context_id: @market_context_ids).order_by_dtstart.order_by_duration
  439. else
  440. events_in_week_range = Event.in_datetime_range(@week_range[0], @week_range[1]).order_by_dtstart.order_by_duration
  441. end
  442. # For non-admin accounts (is_admin: false OR is_admin: nil), return only the account's events
  443. unless current_account.is_admin?
  444. events_in_week_range = events_in_week_range.where(account_id: current_account.id)
  445. end
  446. @events_by_day_of_week = view_context.events_by_day_of_week_by_time_zone(events_in_week_range, @time_zone, @view_pixels_per_minute)
  447. @grouped_event_interface_class_names = grouped_available_event_interface_class_names
  448. end
  449. #####################################################################
  450. # GET /events/new
  451. # GET /events/new.json
  452. def new
  453. type = params[:type]
  454. @event_interface = instantiate_type_specific_event_interface(type)
  455. # Call `new` method on event interface instantiated above
  456. @event_interface.new(current_account)
  457. render "event_interfaces/#{ @event_interface.view_directory }/new"
  458. end
  459. #####################################################################
  460. # POST /events
  461. # POST /events.json
  462. def create
  463. type = params[:event_interface][:event][:event_interface_name]
  464. @event_interface = instantiate_type_specific_event_interface(type)
  465. @event_interface.create(params[:event_interface], current_account)
  466. if @event_interface.event.persisted?
  467. redirect_to event_path(@event_interface.event.id), notice: 'Event was successfully created.'
  468. else
  469. render "event_interfaces/#{ @event_interface.view_directory }/new"
  470. end
  471. end
  472. #####################################################################
  473. # PUT /events/1
  474. # PUT /events/1.json
  475. def update
  476. @event = Event.find(params[:id])
  477. type = @event.event_interface_name
  478. @event_interface = instantiate_type_specific_event_interface(type)
  479. update_succeeded = @event_interface.update(params[:id], params[:event_interface], current_account)
  480. if update_succeeded
  481. @event_interface.event.unpublish
  482. flash.now[:notice] = 'Event was successfully updated'
  483. @event_interface.show(params[:id], current_account)
  484. render "event_interfaces/#{ @event_interface.view_directory }/show"
  485. else
  486. render "event_interfaces/#{ @event_interface.view_directory }/show"
  487. end
  488. end
  489. #####################################################################
  490. def advanced_update
  491. @event = Event.find(params[:id])
  492. type = 'Standard'
  493. @event_interface = instantiate_type_specific_event_interface(type)
  494. update_succeeded = @event_interface.update(params[:id], params[:event_interface], current_account)
  495. if update_succeeded
  496. @event_interface.event.unpublish
  497. flash.now[:notice] = 'Event was successfully updated'
  498. @event_interface.show(params[:id], current_account)
  499. render "event_interfaces/#{ @event_interface.view_directory }/show"
  500. else
  501. render "event_interfaces/#{ @event_interface.view_directory }/show"
  502. end
  503. end
  504. #####################################################################
  505. # DELETE /event/1
  506. # DELETE /event/1.json
  507. def destroy
  508. @request_query_parameters = request.query_parameters
  509. type = Event.find(params[:id]).event_interface_name
  510. @event_interface = instantiate_type_specific_event_interface(type)
  511. if @event_interface.destroy(params[:id])
  512. flash[:notice] = 'Event was successfully destroyed'
  513. redirect_to events_path(@request_query_parameters)
  514. else
  515. flash[:error] = 'Event could not be destroyed'
  516. redirect_to events_path(@request_query_parameters)
  517. end
  518. end
  519. ########################################################
  520. def status
  521. @event_interface = EventInterface::Standard.new
  522. @event_interface.show(params[:id], nil)
  523. @report_interval_descriptions = []
  524. @event_interface.event.targets.each do |target|
  525. @report_interval_descriptions.concat target.report_interval_descriptions.map { |rid| ["#{rid.rid} - #{rid.reading_type_name} - #{rid.unit_desc}", rid.rid] }.uniq
  526. end
  527. @report_interval_descriptions = @report_interval_descriptions.uniq
  528. end
  529. ########################################################
  530. def status_query
  531. @event = Event.find(params[:id])
  532. start_hour = params[:start_hour]
  533. stop_hour = params[:stop_hour]
  534. rid = params[:rid]
  535. report_interval_descriptions = []
  536. Time.use_zone(@event.time_zone) do
  537. @event.reload
  538. @event.targets.each do |target|
  539. report_interval_descriptions.concat target.report_interval_descriptions.where(rid: rid)
  540. end
  541. start_datetime = @event.dtstart.beginning_of_day + start_hour.to_i.hour
  542. end_datetime = @event.dtstart.beginning_of_day + stop_hour.to_i.hour
  543. data, duration, unit_string = ReportIntervalDescription.sum_rids(report_interval_descriptions, start_datetime, end_datetime)
  544. dtend = @event.dtstart + @event.duration.minutes
  545. # indicate in each interval if the event is active or inactive
  546. data.each do |d|
  547. # create a date from the hours field in d[0]: hour:minute[am|pm] - example: 10:00am
  548. t = Time.parse(d[0])
  549. data_point_date = @event.dtstart.beginning_of_day + t.hour.hours + t.min.minutes
  550. if d[0] == "12:00am"
  551. data_point_date = data_point_date + 24.hours
  552. end
  553. # puts data_point_date.to_s + " " + @event.dtstart.to_s
  554. if data_point_date + 5.minutes >= @event.dtstart && data_point_date <= dtend + 5.minutes
  555. d << 'active'
  556. else
  557. d << 'inactive'
  558. end
  559. end
  560. render json: {
  561. report_intervals: data,
  562. time_zone: "#{current_account.time_zone} (#{duration})",
  563. unit_string: unit_string
  564. }
  565. end
  566. end
  567. #####################################################################
  568. private
  569. def render_tab(tab_name)
  570. @event_interface = EventInterface::Standard.new.show(params[:id], current_account)
  571. @tab_name = tab_name
  572. respond_to do |format|
  573. format.html { render 'event_interfaces/standard/show' }
  574. format.js { render 'event_interfaces/standard/target_tab'}
  575. end
  576. end
  577. #####################################################################
  578. def redirect_to_event
  579. respond_to do |format|
  580. format.html { redirect_to event_path(@event)}
  581. format.json { render json: @event }
  582. end
  583. end
  584. #####################################################################
  585. def create_event(event, signal_type_id)
  586. begin
  587. Event.transaction do
  588. event.test_event = "false" if event.test_event == ""
  589. # create an event, signal, and interval in a single transaction
  590. event.save!
  591. signal = event.event_signals.new
  592. signal.signal_type_id = signal_type_id #SignalType.find_by_name(signal_type)
  593. # TODO: active record should default these fields
  594. signal.name = "simple"
  595. signal.signal_id = SecureRandom.hex(10)
  596. signal.save!
  597. interval = signal.event_signal_intervals.new
  598. interval.duration = event.duration
  599. interval.uid = 0
  600. interval.payload_type = PayloadType.find_by_name("normal")
  601. interval.save!
  602. end
  603. true
  604. rescue
  605. # if an error occurs and it's not in event, something went wrong with the database
  606. # in either signal or interval
  607. # TODO: examine signal and interval and report the error, or reply with
  608. # a generic error
  609. false
  610. end
  611. end
  612. #####################################################################
  613. def instantiate_type_specific_event_interface(type_string)
  614. if event_interface_class_for_type_exists?(type_string)
  615. # Dynamically form the appropriate class name and instantiate
  616. event_interface = "EventInterface::#{ type_string.camelize }".constantize.new
  617. else
  618. event_interface = EventInterface::Standard.new
  619. end
  620. end
  621. #####################################################################
  622. def event_interface_class_for_type_exists?(type_string)
  623. type_specific_class_name = "#{ type_string.to_s.camelize }" # Normalize format pre-check
  624. available_event_interface_class_names.include?(type_specific_class_name)
  625. end
  626. #####################################################################
  627. def available_event_interface_class_names
  628. # Returns array of class names for files in `app/models/event_interface`,
  629. # e.g. ["GridEmergency", "LoadUp", "Standard", "OtherInterface"]
  630. event_interface_classes = EventInterface.available_event_interface_classes
  631. # Convert each class (e.g. `EventInterface::Standard`) to a simple string (e.g. "Standard")
  632. event_interface_class_names = event_interface_classes.map { |klass| klass.to_s.demodulize }
  633. return event_interface_class_names
  634. end
  635. #####################################################################
  636. def grouped_available_event_interface_class_names
  637. # Returns a hash of arrays, with event interface collection names as keys,
  638. # and event interface class names as array items
  639. grouped_event_interface_class_names = {}
  640. # The :available_event_interface_class_names method has already done the work of stripping
  641. # out EventInterface.constants which we don't want to include in this list of options
  642. available_event_interface_class_names.each do |ei_class_name|
  643. begin
  644. ei_collection_name = "EventInterface::#{ ei_class_name }::EI_COLLECTION_NAME".constantize
  645. rescue NameError => e
  646. OadrLogger.instance.log_caught_exception(e)
  647. ei_collection_name = "Ungrouped"
  648. end
  649. if grouped_event_interface_class_names[ei_collection_name]
  650. # If the collection name is already present, then add ei_class_name to that key's value (an array)
  651. grouped_event_interface_class_names[ei_collection_name] << ei_class_name
  652. else
  653. # Else, add both the key and its value (an array which, so far, contains only ei_class_name) to the hash
  654. grouped_event_interface_class_names.merge!(ei_collection_name => [ei_class_name])
  655. end
  656. end
  657. return grouped_event_interface_class_names
  658. end
  659. #####################################################################
  660. def correct_account
  661. @event = Event.find(params[:id])
  662. @account = @event.account
  663. correct_account_or_admin @account
  664. end
  665. #####################################################################
  666. def correct_account_for_nested_ven
  667. unless current_account_is_admin # If current_account_is_admin, it's possible targeted VEN's account_id == nil
  668. if params[:ven].present?
  669. ven_ids = params[:ven][:id]
  670. ven_ids.each do |ven_id|
  671. ven_account = Ven.find(ven_id).account
  672. correct_account_or_admin ven_account
  673. end
  674. elsif params[:event_ven].present?
  675. event_ven_ids = params[:event_ven][:id]
  676. event_ven_ids.each do |event_ven_id|
  677. ven_account = EventVen.find(event_ven_id).ven.account
  678. correct_account_or_admin ven_account
  679. end
  680. end
  681. end
  682. end
  683. end