Class: Location

Inherits:
Sequel::Model
  • Object
show all
Includes:
ASModel, AutoGenerator, ExternalIDs
Defined in:
backend/app/model/location.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoGenerator

included

Methods included from ExternalIDs

included

Methods included from ASModel

all_models, included, update_publish_flag, update_suppressed_flag

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Class Method Details

.batch_update(location) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'backend/app/model/location.rb', line 83

def self.batch_update(location)
  location[:record_uris].map do |uri|
    id = JSONModel.parse_reference(uri)[:id]
    json = Location.to_jsonmodel(id)
    json.update(location.to_hash)

    cleaned = JSONModel(:location).from_hash(json.to_hash)

    Location.get_or_die(id).update_from_json(cleaned)
  end
end

.building_dataObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'backend/app/model/location.rb', line 159

def self.building_data
  buildings = {}
  all = self.exclude(building: nil).order_by(:building, :floor, :room, :area)
  all.each do |location|

    if !buildings.has_key?(location.building)
      buildings[location.building] = {}
    end

    floors = buildings[location.building]

    if location.floor.nil?
      next
    elsif !buildings[location.building].has_key?(location.floor)
      floors[location.floor] = {}
    end

    rooms = floors[location.floor]

    if location.room.nil?
      next
    elsif !rooms.has_key?(location.room)
      rooms[location.room] = []
    end

    areas = rooms[location.room]

    if location.area.nil?
      next
    elsif !areas.include?(location.area)
      areas.push(location.area)
    end
  end

  buildings
end

.create_for_batch(batch) ⇒ Object



78
79
80
81
# File 'backend/app/model/location.rb', line 78

def self.create_for_batch(batch)
  locations = generate_locations_for_batch(batch)
  locations.map {|location| self.create_from_json(location)}
end

.create_from_json(json, opts = {}) ⇒ Object



66
67
68
69
# File 'backend/app/model/location.rb', line 66

def self.create_from_json(json, opts = {})
  self.uniqify_functions(json)
  super
end

.for_building(building, floor = nil, room = nil, area = nil) ⇒ Object



197
198
199
200
201
202
203
204
205
206
# File 'backend/app/model/location.rb', line 197

def self.for_building(building, floor = nil, room = nil, area = nil)
  query = {
    :building => building
  }
  query[:floor] = floor if floor && floor != ''
  query[:room] = room if room && room != ''
  query[:area] = area if area && area != ''

  self.filter(query).all
end

.generate_indicators(opts) ⇒ Object



137
138
139
140
# File 'backend/app/model/location.rb', line 137

def self.generate_indicators(opts)
  range = (opts["start"]..opts["end"]).take(AppConfig[:max_location_range].to_i)
  range.map {|i| "#{opts["prefix"]}#{i}#{opts["suffix"]}"}
end

.generate_locations_for_batch(batch) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'backend/app/model/location.rb', line 100

def self.generate_locations_for_batch(batch)
  indicators_1, indicators_2, indicators_3 = [batch["coordinate_1_range"], batch["coordinate_2_range"], batch["coordinate_3_range"]].
                                                compact.
                                                map {|data| generate_indicators(data)}

  source_location = batch.clone

  results = []

  indicators_1.each do |indicator_1|
    source_location["coordinate_1_label"] = batch["coordinate_1_range"]["label"]
    source_location["coordinate_1_indicator"] = indicator_1

    if indicators_2
      indicators_2.each do |indicator_2|
        source_location["coordinate_2_label"] = batch["coordinate_2_range"]["label"]
        source_location["coordinate_2_indicator"] = indicator_2

        if indicators_3
          indicators_3.each do |indicator_3|
            source_location["coordinate_3_label"] = batch["coordinate_3_range"]["label"]
            source_location["coordinate_3_indicator"] = indicator_3

            results.push(JSONModel(:location).from_hash(source_location))
          end
        else
          results.push(JSONModel(:location).from_hash(source_location))
        end
      end
    else
      results.push(JSONModel(:location).from_hash(source_location))
    end
  end

  results
end

.generate_title(json) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'backend/app/model/location.rb', line 26

def self.generate_title(json)
  title = ""

  title << json['building']
  title << ", #{json['floor']}" if json['floor']
  title << ", #{json['room']}" if json['room']
  title << ", #{json['area']}" if json['area']

  others = []
  others << json['barcode'] if json['barcode']
  others << json['classification'] if json['classification']
  others << "#{json['coordinate_1_label']}: #{json['coordinate_1_indicator']}" if json['coordinate_1_label']
  others << "#{json['coordinate_2_label']}: #{json['coordinate_2_indicator']}" if json['coordinate_2_label']
  others << "#{json['coordinate_3_label']}: #{json['coordinate_3_indicator']}" if json['coordinate_3_label']

  title << " [#{others.join(", ")}]"

  title
end

.titles_for_batch(batch) ⇒ Object



95
96
97
98
# File 'backend/app/model/location.rb', line 95

def self.titles_for_batch(batch)
  locations = generate_locations_for_batch(batch)
  locations.map {|location| self.generate_title(location)}
end

.uniqify_functions(json) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'backend/app/model/location.rb', line 53

def self.uniqify_functions(json)
  found_fns = []
  json['functions'] = json['functions'].select do |fn|
    if found_fns.include? fn['location_function_type']
      false
    else
      found_fns << fn['location_function_type']
      true
    end
  end
end

Instance Method Details

#deleteObject

Raises:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'backend/app/model/location.rb', line 143

def delete
  # only allow delete if the location doesn't have any relationships that should be preserved
  object_graph = self.object_graph

  # These relationships should not prevent deletion if the location is otherwise unlinked.
  ignored_relationships = [Location.find_relationship(:location_profile),
                           Location.find_relationship(:owner_repo)]

  if object_graph.models.any? {|model| model.is_relationship? && !ignored_relationships.include?(model) }
    raise ConflictException.new("Location cannot be deleted if linked")
  end

  super
end

#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object



72
73
74
75
# File 'backend/app/model/location.rb', line 72

def update_from_json(json, opts = {}, apply_nested_records = true)
  self.class.uniqify_functions(json)
  super
end