Class: Ribit::ContentStore

Inherits:
Object
  • Object
show all
Includes:
IDataStorage
Defined in:
lib/ribit/contentstore.rb

Instance Method Summary collapse

Constructor Details

#initialize(ribitConfig, repository) ⇒ ContentStore

Returns a new instance of ContentStore.



166
167
168
169
170
171
172
173
# File 'lib/ribit/contentstore.rb', line 166

def initialize( ribitConfig, repository )
  @logger = RibitLogger.new( ContentStore )
  @ribitConfig = ribitConfig
  @dataPath = File.join( ribitConfig[RibitConfig::DATA_PATH], Repository::CVS_PROJECT_NAME )
  
  @repository = repository
  @repository.init( ribitConfig, @dataPath )
end

Instance Method Details

#get_content_dirObject



240
241
242
# File 'lib/ribit/contentstore.rb', line 240

def get_content_dir
  return File.join( @ribitConfig[RibitConfig::DATA_PATH], Repository::CVS_PROJECT_NAME )
end

#get_ribit_dataObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ribit/contentstore.rb', line 176

def get_ribit_data
  contentDir = get_content_dir
  @logger.info( "Reading data from #{contentDir}" )
  
  categoryParser = RibitData::CategoryParser.new( contentDir )
  categories = categoryParser.parse_categories
  
  # RibitData will later use self object for saving
  ribitData = RibitData::RibitData.new( self )
  pageParser = RibitData::PageParser.new( contentDir )
  parse_categories( pageParser, ribitData, categories )
  
  return ribitData
end

#parse_categories(pageParser, ribitData, categories) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/ribit/contentstore.rb', line 192

def parse_categories( pageParser, ribitData, categories )
  categories.each do |category|
    # adds pages to category
    pageParser.parse_pages( category )
    ribitData.add_category( category )
    
    # process child categories
    parse_categories( pageParser, ribitData, category.get_child_categories )
  end
end

#save_categories_hierarchy(ribitData) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/ribit/contentstore.rb', line 245

def save_categories_hierarchy( ribitData )
  contentDir = get_content_dir
  categories = RibitData::CategoriesWriter.new( ribitData )
  
  fileName = File.join( contentDir, RibitData::CategoryParser::CATEGORIES_XML_FILE )
  
  writer = FileWriter.new( fileName )
  writer.write( categories )
  
  # assuming that categories.xml already exists in CVS
  @repository.commit_to_repository( fileName )
  
  # NOTE: reloading is actually same as validating => we load categories
  #       and compare structure to existing 
  if ( @ribitConfig[RibitConfig::DATA_RELOAD] == 'true' )
    @logger.info( "Reloading is true => validate category hierarchies" )
    
    categoryParser = RibitData::CategoryParser.new( contentDir )
    # TODO: BUG: parse_categories returns only root categories
    categories = categoryParser.parse_categories
    
    # build a map to make comparison easier
    categoriesMap = {}
    categories.each do |cat|
      categoriesMap[cat.id.to_s] = cat
      categoriesMap.merge!( get_child_categories( cat ) )
      print "ON DISK=#{cat.id}\n"
    end

    # 'categories' contains now all possible categories
    #  => get same kind of list from the current RibitData and compare
    existingCategories = ribitData.get_all_categories
    
    existingCategories.each do |cat|
        #print "################ MEMORY=#{cat.id}\n"

        reloadCategory = categoriesMap.delete( cat.id.to_s )
        if ( reloadCategory == nil )
          raise RibitException, "On the disk there is no category ID=#{cat.id} that is in memory", caller
        end
        
        # check parent
        if ( cat.get_parent_category == nil and reloadCategory.get_parent_category == nil )
          # OK
        elsif ( cat.get_parent_category.id == reloadCategory.get_parent_category.id )
          # OK
        else
          raise RibitException, "Hierarchy structure on the disk and in memory doesn not match: categoryID=#{cat.id}", caller
        end
    end

    # if everything matched then categoriesMap should be empty now
    if ( not categoriesMap.empty? )
      raise RibitException, "There are more categories on the disk than in memory", caller
    end
    
  end 
end

#save_category(category) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/ribit/contentstore.rb', line 305

def save_category( category )
  contentDir = get_content_dir
  categoryDir = File.join( contentDir, category.id )
  categoryFileName = File.join( categoryDir, RibitData::CategoryParser::CATEGORY_XML_FILE )
  
  # if this category is new then we need to create directory
  if ( not File.exist?( categoryDir ) )
    FileUtils.mkdir( categoryDir )
    @repository.add_to_repository( contentDir, category.id, '' )
  end
  
  fileExists = File.exist?( categoryFileName )
  writer = FileWriter.new( categoryFileName )
  writer.write( category )
  
  if ( not fileExists )
    @logger.debug( "Adding category file to repository: #{categoryFileName}" )
    @repository.add_to_repository( categoryDir, File.basename( categoryFileName ), 'new category created' )
  end
  
  @repository.commit_to_repository( categoryFileName )  
  
  # do data reloading if configured   
  if ( @ribitConfig[RibitConfig::DATA_RELOAD] == 'true' )
    @logger.info( "Reloading is true => reload saved category ID=#{category.id}" )
    
    # category hierarchies has not been changed, only contents of category.xml

    categoryParser = RibitData::CategoryParser.new( contentDir )
    reloadedCategory = categoryParser.parse_category( category.id )
    
    category.name = reloadedCategory.name
    category.set_main_page_id( reloadedCategory.get_main_page_id )
  end
  
end

#save_page(page) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/ribit/contentstore.rb', line 204

def save_page( page )
  contentDir = get_content_dir
  categoryDir = File.join( contentDir, page.category.id )
  fileName = File.join( categoryDir, page.id + '.xml' )
        
  # is the file new ? (we need this information later)
  newFile = !File.exist?( fileName )
  
  writer = FileWriter.new( fileName )
  writer.write( page )

  # is the file in CVS ?
  if ( newFile )
    # no => add it before commit
    @logger.debug( "Adding page file to repository: #{fileName}" )
    @repository.add_to_repository( categoryDir, File.basename( fileName ), 'new page created' )
  end
  
  @repository.commit_to_repository( fileName )
  
  # do data reloading if configured   
  if ( @ribitConfig[RibitConfig::DATA_RELOAD] == 'true' )
    @logger.info( "Reloading is true => reload saved page #{page.full_id}" )
    
    # reloading happends first by deleting old page and then adding it again to category
    pageParser = RibitData::PageParser.new( contentDir )
    
    category = page.category
    category.remove_page( page.id )
    
    pageParser.parse_page( category, fileName )
  end
  
end