winix/winixd/models/item.cpp

299 lines
6.1 KiB
C++

/*
* This file is a part of Winix
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2010-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "models/item.h"
#include "core/misc.h"
#include "finder.h"
namespace Winix
{
Item::Item()
{
Clear();
}
void Item::map_fields()
{
int type_helper = static_cast<int>(type);
field(L"id", id, false, false, true);
field(L"parent_id", parent_id);
field(L"type", type_helper);
field(L"url", url);
field(L"subject", subject);
field(L"template", html_template);
field(L"sort_index", sort_index);
field(L"content_id", item_content, true, true, true);
// may we should add a method setTypeFromInt(int t)?
type = static_cast<Type>(type_helper);
}
void Item::table_name(PT::TextStream & stream)
{
stream << "core.item";
}
void Item::before_insert()
{
}
void Item::after_insert()
{
get_last_sequence(L"core.item_id_seq", id);
}
bool Item::insert(morm::ModelData * model_data, bool update_whole_tree)
{
bool url_prepared_correctly = prepare_url();
bool insert_status = morm::Model::insert(model_data, update_whole_tree);
if( insert_status )
{
if( !url_prepared_correctly )
{
PT::Toa(id, url);
morm::Model::update(model_data, false);
}
}
return insert_status;
}
bool Item::insert(morm::ModelData & model_data, bool update_whole_tree)
{
return insert(&model_data, update_whole_tree);
}
bool Item::insert(bool update_whole_tree)
{
return insert(nullptr, update_whole_tree);
}
/*
*
* this can be done better
* instead of calculating if more than zero items are in a directory
* just get the one item (should be one in normal circumstances)
* and compare its 'id' with 'this.id' and if these ids are different
* then prepare a new url
*
*
*/
bool Item::update(morm::ModelData * model_data, bool url_was_changed, bool update_whole_tree)
{
bool url_prepared_correctly = true;
if( url_was_changed )
{
url_prepared_correctly = prepare_url();
}
bool update_status = morm::Model::update(model_data, update_whole_tree);
if( update_status )
{
if( !url_prepared_correctly )
{
PT::Toa(id, url);
morm::Model::update(model_data, false);
}
}
return update_status;
}
bool Item::update(morm::ModelData & model_data, bool url_was_changed, bool update_whole_tree)
{
return update(&model_data, url_was_changed, update_whole_tree);
}
bool Item::update(bool url_was_changed, bool update_whole_tree)
{
return update(nullptr, url_was_changed, update_whole_tree);
}
bool Item::save(morm::ModelData * model_data, bool url_was_changed, bool save_whole_tree)
{
bool url_prepared_correctly = true;
if( save_mode == DO_INSERT_ON_SAVE || (save_mode == DO_UPDATE_ON_SAVE && url_was_changed) )
{
url_prepared_correctly = prepare_url();
}
bool save_status = morm::Model::save(model_data, save_whole_tree);
if( save_status )
{
if( !url_prepared_correctly )
{
PT::Toa(id, url);
morm::Model::save(model_data, false);
}
}
return save_status;
}
bool Item::save(morm::ModelData & model_data, bool url_was_changed, bool save_whole_tree)
{
return save(&model_data, url_was_changed, save_whole_tree);
}
bool Item::save(bool url_was_changed, bool save_whole_tree)
{
return save(nullptr, url_was_changed, save_whole_tree);
}
void Item::Clear()
{
id = -1;
parent_id = -1;
type = none;
url.clear();
subject.clear();
html_template.clear();
sort_index = 0;
item_content.Clear();
}
long Item::calc_items_by_url(long parent_id, const std::wstring & url)
{
morm::Finder<CalcItemsHelper> finder(model_connector);
CalcItemsHelper helper = finder.
prepare_to_select().
raw("select count(id) as size from core.item").
where().
eq(L"parent_id", parent_id).
eq(L"url", url).
get();
return helper.size;
}
/*
* may we should check for '.' and '..' here too?
*/
bool Item::prepare_url()
{
std::wstring temp_url;
bool is_that_url;
int index = 1;
const int max_index = 99;
std::wstring postfix;
// only root dir may not have the url
if( parent_id != -1 && url.empty() )
url = L"_";
do
{
if( index > 1 )
{
postfix = L"_(";
PT::Toa(index, postfix, false);
postfix += L")";
}
PrepareNewFileName(url, postfix, temp_url);
long size = calc_items_by_url(parent_id, temp_url);
if( size > 0 )
{
is_that_url = true;
}
else
{
url = temp_url;
is_that_url = false;
}
index += 1;
}
while( is_that_url && index <= max_index );
return !is_that_url;
}
void Item::do_migration(morm::ModelConnector * model_connector)
{
return;
///////
morm::Finder<Item> finder(model_connector);
std::list<Item> list = finder.select().where().eq(L"type", static_cast<int>(Item::dir)).eq(L"content_id", -1).get_list();
// for(Item & item : list)
// {
// item.item_content.set_save_mode(morm::Model::DO_INSERT_ON_SAVE);
// item.save();
// }
}
} // namespace Winix