251 lines
5.3 KiB
C++
251 lines
5.3 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()
|
|
{
|
|
//ItemModelData * item_model_data = dynamic_cast<ItemModelData>(get_model_data());
|
|
|
|
int type_helper = static_cast<int>(type);
|
|
|
|
field(L"id", id, morm::FT::no_insertable | morm::FT::no_updatable | morm::FT::primary_key);
|
|
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, morm::FT::foreign_key);
|
|
|
|
// may we should add a method setTypeFromInt(int t)?
|
|
type = static_cast<Type>(type_helper);
|
|
}
|
|
|
|
|
|
void Item::prepare_table()
|
|
{
|
|
table(L"core", L"item");
|
|
}
|
|
|
|
|
|
void Item::before_insert()
|
|
{
|
|
}
|
|
|
|
|
|
void Item::after_insert()
|
|
{
|
|
get_last_sequence_for_primary_key(L"core.item_id_seq", id);
|
|
}
|
|
|
|
|
|
bool Item::insert(morm::ModelData * model_data, bool update_whole_tree)
|
|
{
|
|
ItemModelData * item_model_data = dynamic_cast<ItemModelData*>(model_data);
|
|
bool url_prepared_correctly = true;
|
|
|
|
if( !item_model_data || item_model_data->prepare_unique_url )
|
|
{
|
|
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);
|
|
insert_status = morm::Model::update(model_data, false);
|
|
}
|
|
}
|
|
|
|
return insert_status;
|
|
}
|
|
|
|
|
|
bool Item::update(morm::ModelData * model_data, bool update_whole_tree)
|
|
{
|
|
ItemModelData * item_model_data = dynamic_cast<ItemModelData*>(model_data);
|
|
bool url_prepared_correctly = true;
|
|
|
|
if( !item_model_data || item_model_data->prepare_unique_url )
|
|
{
|
|
CalcItemsHelper helper = calc_items_by_url(parent_id, url);
|
|
|
|
if( helper.size == 1 && helper.item_id == id )
|
|
{
|
|
// the same item
|
|
}
|
|
else
|
|
{
|
|
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);
|
|
update_status = morm::Model::update(model_data, false);
|
|
}
|
|
}
|
|
|
|
return update_status;
|
|
}
|
|
|
|
|
|
|
|
|
|
void Item::Clear()
|
|
{
|
|
id = -1;
|
|
parent_id = -1;
|
|
type = none;
|
|
url.clear();
|
|
subject.clear();
|
|
html_template.clear();
|
|
sort_index = 0;
|
|
item_content.Clear();
|
|
}
|
|
|
|
|
|
CalcItemsHelper 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, min(id) as item_id from core.item").
|
|
where().
|
|
eq(L"parent_id", parent_id).
|
|
eq(L"url", url).
|
|
get();
|
|
|
|
return helper;
|
|
}
|
|
|
|
|
|
/*
|
|
* may we should check for '.' and '..' here too?
|
|
*/
|
|
bool Item::prepare_url()
|
|
{
|
|
std::wstring temp_url;
|
|
bool is_that_url;
|
|
const int max_index = 99;
|
|
size_t index = 1;
|
|
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);
|
|
|
|
CalcItemsHelper helper = calc_items_by_url(parent_id, temp_url);
|
|
|
|
if( helper.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();
|
|
// }
|
|
|
|
}
|
|
|
|
void Item::propagate_connector()
|
|
{
|
|
item_content.set_connector(model_connector);
|
|
}
|
|
|
|
|
|
} // namespace Winix
|
|
|