Class: SaveAction

Inherits:
WebAction show all
Includes:
Assert, Localization
Defined in:
lib/ribit/action.rb

Constant Summary

Constants included from Localization

Localization::ACTION_CATEGORY_CREATION_NOTICE, Localization::ACTION_DEFAULT_PAGE_FAILED, Localization::ACTION_EDIT_CANCELED, Localization::ACTION_NEW_PAGE_OK, Localization::ACTION_SAVE_CATEGORY_OK, Localization::ACTION_SAVE_OK, Localization::NO_PARENT_OPTION, Localization::TITLE_CATEGORIES_PAGE, Localization::TITLE_NEW_CATEGORY, Localization::TITLE_NEW_PAGE

Instance Attribute Summary

Attributes inherited from WebAction

#id

Instance Method Summary collapse

Methods included from Localization

#get_localized, #ribitData_defined?

Methods included from Assert

assert, #assert, assert_nil, #assert_nil, #assert_not_nil, assert_not_nil, raise_exception

Methods inherited from WebAction

#add_headers

Constructor Details

#initialize(ribitData, ribitConfig, pageCache) ⇒ SaveAction

Returns a new instance of SaveAction.



177
178
179
180
181
182
183
184
185
# File 'lib/ribit/action.rb', line 177

def initialize( ribitData, ribitConfig, pageCache )
  assert_not_nil( ribitData, 'RibitData is nil' )
  
  @id = 'form:save'
  @ribitData = ribitData
  @ribitConfig = ribitConfig
  @pageCache = pageCache
  @logger = RibitLogger.new( SaveAction )
end

Instance Method Details

#run(pageRequest, pageResponse) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
238
239
240
241
242
243
244
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
# File 'lib/ribit/action.rb', line 188

def run( pageRequest, pageResponse )
  super( pageRequest, pageResponse )

  pageID = pageRequest.get_requested_page_id
  message = ''
  
  if ( pageID == nil )
    raise RibitException, 'No page ID defined for save action', caller
  end
  
  query = pageRequest.get_query_data
  data = query[Constants::PAGE_DATA_FORM_PARAM_NAME]
  name = query[Constants::PAGE_NAME_FORM_PARAM_NAME]
    
  if ( pageID == Constants::NEW_PAGE_ID )
    categoryName = pageRequest.get_request_param( Constants::CATEGORY_FORM_PARAM_NAME )
    category = @ribitData.get_category_by_full_name( categoryName )
    
    if ( category == nil )
      @logger.debug( "Category #{categoryName} doesn't exists => will be created" )
      # might be short reference or totally new (several levels too)
      # no easy way to make difference => assuming always full ref
      #  => use of :: at the beginning for toplevel
      
      categoryParts = categoryName.split( '::' )
      if ( categoryParts[0] == '' )
        categoryParts.delete_at( 0 )
      end
      cat = ''
      parentCategory = nil
      categoryParts.each do |part|
        cat << part
        @logger.debug( "Checking category #{cat} existance" )
        tmpCategory = @ribitData.get_category_by_full_name( cat )
        if ( tmpCategory == nil )
          @logger.debug( "Creating category #{cat}" )
          tmpCategory = @ribitData.create_category( part, '', parentCategory )
        end
        cat << '::'
        parentCategory = tmpCategory
      end
      
      # now every category should have been created and 
      # parentCategory contains the last one = the current category
      category = parentCategory
    end
    
    # create a new page
    newPageID = category.get_next_free_page_id
    @logger.debug( "creating a new page for ID=#{newPageID}" )
    
    # category should already exists in RibitData => page gets recorded
    page = RibitData::Page.new( newPageID, name, category )
    page.data = data
    
    message = get_localized( ACTION_NEW_PAGE_OK )
    pageID = page.full_id
    
    # NOTE: this is a new page
    #   => categories and all pages linking to this have changed 
    #   => delete all pages

    # TODO: later we could know where pages are referenced
    
    if ( @pageCache != nil )
      @pageCache.delete_all
    end
    
  # check that there exists a page
  elsif ( @ribitData.get_page_by_id( pageID ) == nil )
    raise RibitException, "No page for ID=#{pageID}", caller
  else
    @logger.debug( "saving page for ID=#{pageID}" )

    page = @ribitData.get_page_by_id( pageID )
    page.data = data
    
    # if name has changed then contents of categories page will change
    if ( page.name != name and @pageCache != nil )
      @pageCache.delete( ViewCategoriesAction::PAGE_CACHE_ID )
    end
    
    page.name = name
    
    message = get_localized( ACTION_SAVE_OK )
    @pageCache.delete( page.full_id ) unless @pageCache == nil
  end
  
  @ribitData.save_page( page )
  
  # show the saved page
  page = TextPage.new( @ribitData, page, @ribitConfig )
  page.set_message( message )
   
  # set the base url for whole page (all links are relative to this)
  add_headers( page )

    # finally build the page      
  page.output_page( pageResponse )
  
end