232 lines
4.9 KiB
C++
232 lines
4.9 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 "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", content_id);
|
|
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()
|
|
{
|
|
// IMPROVEME if prepare_status is false then update the 'url' with the 'id'
|
|
// add a method: update_url()
|
|
url_was_prepared_correctly = prepare_url();
|
|
}
|
|
|
|
|
|
void Item::after_insert()
|
|
{
|
|
get_last_sequence(L"core.item_id_seq", id);
|
|
|
|
if( !url_was_prepared_correctly )
|
|
{
|
|
// update_url();
|
|
// what about an error from update_url()?
|
|
}
|
|
}
|
|
|
|
|
|
bool Item::update(morm::ModelData * model_data, bool url_was_changed, bool update_whole_tree)
|
|
{
|
|
bool prepare_url_status = true;
|
|
|
|
if( url_was_changed )
|
|
{
|
|
prepare_url_status = prepare_url();
|
|
}
|
|
|
|
bool update_status = morm::Model::update(model_data, update_whole_tree);
|
|
|
|
if( update_status )
|
|
{
|
|
if( !prepare_url_status )
|
|
{
|
|
// IMPROVEME add update_url() method
|
|
// set 'url' to the same value as 'id'
|
|
//update_status = update_url();
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Item::Clear()
|
|
{
|
|
id = -1;
|
|
parent_id = -1;
|
|
type = none;
|
|
url.clear();
|
|
subject.clear();
|
|
html_template.clear();
|
|
sort_index = 0;
|
|
content_id = -1;
|
|
item_content.Clear();
|
|
|
|
// used only when inserting, not saved to the database
|
|
url_was_prepared_correctly = true;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
bool Item::prepare_url()
|
|
{
|
|
std::wstring temp_url;
|
|
bool is_that_url;
|
|
int index = 1;
|
|
const int max_index = 99;
|
|
wchar_t appendix[20];
|
|
size_t appendix_len = sizeof(appendix) / sizeof(wchar_t);
|
|
|
|
appendix[0] = 0;
|
|
|
|
// only root dir may not have the url
|
|
if( parent_id != -1 && url.empty() )
|
|
url = L"empty"; // IMPROVEME move me to locales
|
|
|
|
do
|
|
{
|
|
temp_url = url;
|
|
temp_url += appendix;
|
|
|
|
long size = calc_items_by_url(parent_id, temp_url);
|
|
|
|
if( size > 0 )
|
|
{
|
|
swprintf(appendix, appendix_len, L"_(%d)", ++index);
|
|
is_that_url = true;
|
|
}
|
|
else
|
|
{
|
|
url = temp_url;
|
|
is_that_url = false;
|
|
}
|
|
}
|
|
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
|
|
|