rhomobile prepopulated DB and reset - rhodes

Hi I am prepopulating the db as said here
http://docs.rhomobile.com/faq#how-to-pre-populate-client-database
but I have a problem, that when I make reset DB with default code
def do_reset
Rhom::Rhom.database_full_reset
SyncEngine.dosync
#msg = "Database has been reset."
redirect :action => :index, :query => {:msg => #msg}
end
then I am losing the data. How can I make that the prepopulated database alsways will be loaded, when I make reset.
Cheers
I come up with such solution
in view do_reset.erb
<%
Antwort.delete_all()
file_name = File.join(Rho::RhoApplication::get_model_path('app','Settings'), 'antwort.txt')
file = File.new(file_name,"r")
aid=0
file.each_line("\n") do |row|
col = row.split("|")
aid=aid+1
#antwort=Antwort.create(
{"aid" => aid, "qid" => col[0],"antwort"=>col[1],"richtig"=>col[2]}
)
qty=file.lineno
break if file.lineno > 3000
end
Questions.delete_all()
file_name = File.join(Rho::RhoApplication::get_model_path('app','Settings'), 'questions.txt')
file = File.new(file_name)
file.each_line("\n") do |row|
col = row.split("|")
#question=Questions.create(
{"id" => col[0], "question" => col[1],"answered"=>'0',"show"=>'1',"tutorial"=>col[4]}
)
break if file.lineno > 1500
end
file.close
#msg="OK"
%>
But only problem I have now is single quotes aka ' in the texts. They are then displayed in app as a triangle with ? inside like � one. What to do?

You can seed the database using a pipe delimited text file, as explained here.
So, in the instance you are using the Property Bag model definitions, you will have a file called object_values.txt and load up whichever sources, properties, and values necessary.

Related

Typo3 : add file to FAL when it is not presente in sys_file

I have this situation
I have a table in my db containing some file names in a field1 (eg field1: "my file.ext")
NOTE: the filename does not necessarily pass a Typo3 "sanitizeFilename" check -> it may contain spaces " " or other characters that would be removed by the sanitizeFilename () method
I have the file mentioned above, stored on the server that host typo3
In the sys_file table, the file is not present
the "update storage index" scheduler cannot process all the files, and if i launch it, it "destroy" the file name (my file.ext -> my_file.ext), so the name stored in the field of my table doensn't have much sense anymore.
I would need to absorb the above mentioned files in the FAL, in order to use them in an ext typo3.
I had thought of such a solution
<?php
// read from "field1" of my table
// $filename = the name extracted from my table (e.g. : "my file.ext")
// %path = the path of the file : e.g. "/fileadmin/user_upload")
if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/" . $defaultStorage->getConfiguration()['basePath'] . $path . $filename)) {
// check the folder
if ($defaultStorage->hasFolder($path)) {
$folder = $defaultStorage->getFolder($path);
} else {
throw new \ Exception ($path . "path not found in AbstractImportCommand in method extractFile");
}
// CHECK IF FILE IS IN FAL
$file = $folder->getStorage()->getFileInFolder($filename, $folder);
if ($file) {
// the file already exists in the FAL
} else {
// create new sys_file
$file = $defaultStorage->addFile(
$_SERVER['DOCUMENT_ROOT'] . "/" . $defaultStorage->getConfiguration()['basePath'] . $path . $filename,
$folder,
DuplicationBehavior::REPLACE
);
}
}
Any suggestion?
Put your code into a command.
(optional) create a sys_file_metadata record for your file if you have information that needs to be stored there
create a sys_file_reference to your content record (before that you should adjust TCA accordingly)
For creating the sys_file_reference there is no api. A function doing so could look like this:
/**
* #param $fileUid
* #param $recordUid
* #param $table
*/
private function createSysFileReference($record, $fileUid, $tableName, $fieldName){
$data['sys_file_reference']['NEW_' . uniqid()] = [
'table_local' => 'sys_file',
'uid_local' => $fileUid,
'tablenames' => $tableName,
'uid_foreign' => $record['uid'],
'fieldname' => $fieldName,
'pid' => $record['pid']
];
$dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
$dataHandler->start($data, []);
$dataHandler->process_datamap();
}
What you consider as "destroying the filename" is indeed preserving file functionality, so sanitizing the filenames is required.
As example consider a file with white space like "My Pdf.pdf". It will be shown eventually like "My%20Pdf.pdf" in the URL and also saved like this. If you link to it, at least the link text won't show the "%20" and the expectation that links and the name of the file are always synchronized (no matter how it's stored) isn't reliable as it probably depends on several parameters like operating system or browser too. The same problem might occur for many different signs too.
Consider that the problems occur not only when a user is downloading a file but also when a file is uploaded, where the wrong url-encoded name is saved in the database, this name might be saved differently in the filesystem or not be found even if the saved value is the same as in the filesystem, due to the url-encoded signs. Your file references are broken on the server then and according links might not work and images not displayed.
So circumventing FAL beside tables is a bad decision and while it's likely possible to write an own sanitizer, I would refrain from dropping it completely.

Attempt to access upserted_id property in perl MongoDB Driver returns useless HASH(0x3572074)

I have a Perl script that pulls a table from a SQL database ($row variable) and attempts to do a MongoDB update like so:
my $res = $users->update({"meeting_id" => $row[0]},
{'$set' => {
"meeting_id" => $row[0],
"case_id" => $row[1],
"case_desc" => $row[2],
"date" => $row[3],
"start_time" => $row[4],
"end_time" => $row[5],
#"mediator_LawyerID" => $row[6],
"mediator_LawyerIDs" => \#medLawIds,
"case_number" => $row[6],
"case_name" => $row[7],
"location" => $row[8],
"number_of_parties" => $row[9],
"case_manager" => $row[10],
"last_updated" => $row[11],
"meeting_result" => $row[12],
"parties" => \#partyList
}},
{'upsert' => 1}) or die "I ain't update!!!";
My client now wants ICS style calendar invites sent to their mediators. Thus, I need to know whether an update or insert happened. The documentation for MongoDB::UpdateResult implies that this is how you access such a property:
my $id = $res->upserted_id;
So I tried:
bless ($res,"MongoDB::UpdateResult");
my $id = $res->upserted_id;
After this code $id is like:
HASH(0x356f8fc)
Are these the actual IDs? If so, how do I convert to a hexadecimal string that can be cast to Mongo's ObjectId type? It should be noted I know absolutely nothing about perl; if more of the code is relevant, at request I will post any section ASAP. Its 300 lines so I didn't want to include the whole file off the bat.
EDIT: I should mention before anyone suggests this that using update_one instead of update returns the exact same result.
HASH(0x356f8fc) is a Perl Hash reference. It's basically some kind of (internal) memory address of some data.
The easiest way to get the contents is Data::Dumper:
use Data::Dumper
[...]
my $result = $res->upserted_id;
print Dumper($result);
HASH(0x356f8fc) is just the human readable representation of the real pointer. You must dump it in the same process and can't pass it from one to another.
You'll probably end up with something like
`my $id = $result->{_id};`
See the PerlRef manpage for details.
See also the MongoDB documentation about write concern.
PS: Also remember that you could use your own IDs for MongoDB. You don't need to work with the generated ones.

How can I use HTML::Template without creating separate files for each template?

The normal way of using HTML::Template is as follows:
main file:
my $template = HTML::Template->new(filename => 'secret.tmpl');
$template->param(SECRET_MESSAGE => $bar);
print $template->output;
secret.tmpl:
<h1>Hello <TMPL_VAR NAME=SECRET_MESSAGE></h1>
The question is, can I use that template engine without creating separate files - i.e., generating template's content on the fly?
Or maybe it's possible with any other modules?
Yes, it is possible with HTML::Template: its constructor works with templates stored either in scalar (as whole block of text) or in array (line by line). Like this:
my $t_from_scalar = HTML::Template->new(
scalarref => $ref_to_template_text,
option => 'value',
);
my $t_from_array_of_lines = HTML::Template->new(
arrayref => $ref_to_array_of_lines,
option => 'value',
);
In fact, there are two specific constructor methods for these cases:
my $t_from_scalar = HTML::Template->new_scalar_ref(
$ref_to_template_text, option => 'value');
my $t_from_array_of_lines = HTML::Template->new_array_ref(
$ref_to_array_of_lines, option => 'value');

What's the easiest way to refactor a embed into a reference or vice versa in MongoDB?

Say I have an existing dataset in which something is an embedded document but we want to turn it into a reference instead. Is there any kind of automatic or semi-automatic way to do this refactoring.
Refactoring itself is simple. Just swap "embeds_one" for "has_one" (or substitute appropriate terms for your mapper library). It's the data migration that will cause some pain. Or maybe it won't. Here's a small ruby script I whipped up in under 10 minutes. It should cover what I think you need.
source_collection = 'users'
field_to_expand = 'address'
parent_field = 'user_id'
expanded_collection = 'addresses'
require 'mongo'
db = Mongo::Connection.new.db('test')
users = db.collection(source_collection)
addresses = db.collection(expanded_collection)
# prepare test data
users.remove()
addresses.remove()
users.insert({name: 'Joe', address: {city: 'Rio de Janeiro'}})
users.find().to_a # => [{"_id"=>BSON::ObjectId('50614e910ed4c08a6a000001'), "name"=>"Joe", "address"=>{"city"=>"Rio de Janeiro"}}]
users.find().each do |u|
# move subdocument to a separate collection
addr = u[field_to_expand]
addr[parent_field] = u['_id']
addresses.insert(addr)
# erase from original document
users.update({_id: u['_id']}, {'$unset' => {field_to_expand => 1}})
end
users.find().to_a # => [{"_id"=>BSON::ObjectId('50614e910ed4c08a6a000001'), "name"=>"Joe"}]
addresses.find().to_a # => [{"_id"=>BSON::ObjectId('50614e910ed4c08a6a000002'), "city"=>"Rio de Janeiro", "user_id"=>BSON::ObjectId('50614e910ed4c08a6a000001')}]

Write Web call for iphone in ruby on rail. I want to get json formate and send response but formate of json is incorrect

I have a bug in the following Rails code which I cannot find. Any help would be appreciated.
class Api::ItemsController < ApplicationController
respond_to :json
def create
begin
#puts "params: #{params}"
if params[:item_post][:user][:uid] && params[:item_post][:user][:provider]
#user = User.find_by_provider_and_uid(params[:item_post][:user][:provider], params[:item_post][:user][:uid])
elsif params[:item_post][:user][:token]
#user = User.create_with_token(params[:item_post][:user][:token])
elsif params[:item_post][:user][:email]
#user = User.find_by_email(params[:item_post][:user][:email])
end
if #user
#item = #user.items.new(params[:item_post][:item])
else
#item = Item.new(params[:item_post][:item])
#item.reply_email = params[:item_post][:user][:email]
end
if #item.save
if params[:item_post][:item_images]
params[:item_post][:item_images].each_value do |item_image|
#item.images.create(item_image)
end
end
respond_with(#item)
else
#puts "Errors 1: #{#item.errors}"
respond_with(#item.errors)
end
rescue => e
#puts "Errors 2: #{e.message}"
respond_with(e.message.to_json, :status => :unprocessable_entity)
end
end
end
Problem:
I have been looking into the parameters received by the server. And I've tried to find the code where the parameters are built. Where can I find it?
In the server logs I can see the post as:
{"item_post":"{\"item\":{\"title\":\"Apple TV\",\"price\":\"45.50\",\"description\":\"Dual-Shielded High Speed HDMI Cable with Ethernet 2M are sold yesterday\",\"zipcode\":\"94102\"},\"user\":{\"email\":\"user#email.com\"}}}
I was expecting the post to look like this:
{\"item_post\":{\"item\":{\"title\":\"Apple TV\",\"price\":\"45.50\",\"description\":\"Dual-Shielded High Speed HDMI Cable with Ethernet 2M are sold yesterday\",\"zipcode\":\"94102\"},\"user\":{\"email\":\"user#email.com\"}}}
It seems to me that the 'item_post' block is being wrapped in unnecessary quotes, making it a string instead of a JSON hash:
I think this:
{"item_post":"{
should look like (no quote) this:
{"item_post":{
I am only guessing to what the problem could be.
I think you need avoid the to_json in your rescue
respond_with(e.message.to_json, :status => :unprocessable_entity)
put
respond_with(e.message, :status => :unprocessable_entity)
In your case the to_json before the to_json in the respond_with return a String it's why you have a String convert in JSON and not your Hash convert in JSON