Wednesday, 11 September 2013

rails: array of objects, how to sum values of object attributes?

rails: array of objects, how to sum values of object attributes?

I have an array of objects obtained from some queries and the all the
object have the attributes: id, title and quantity. The question is : How
can I sum the quantity by id and save each fields? I've tried this snippet
h=Hash.new(0)
a.each do |el|
h[el.id] += el.quantity
end
and this goes, but I lose the title! How can I have the title inside the
hash h?
thanks!

Runnable handler correct execution

Runnable handler correct execution

I've got a function with a handler where I execute other functions. These
functions are via bluetooth writing and reading functions where a string
is send and then a another string is read.
I've got the function created this way:
status= 0;
handler.post(new Runnable() {
@Override
public void run() {
try {
if (status== 0) {
initThread_send(); //Strings that send the command to
initialize transmission to the PCB
status= 1;
}
else if (status== 1) {
writeClimb_send(); //Sent string with writing command
status= 2;
}
else if (status== 2 && <Another condition>) {
readClimbConf_send(); //Received string that confirms
the writing
status= 3;
}
else if (estado == 3) {
writePlatform_send(); //Sent string with writing command
estado = 4;
}
else if (status== 4 && <Another condition>) {
readPlatformConf_send(); //Received string that
confirms the writing
status= 5;
}
else if (status== 5) {
writeFast_send(); //Sent string with writing command
status= 6;
}
else if (status== 6 && <Another condition>) {
readFastConf_send(); //Received string that confirms
the writing
status= 7;
}
else if (status== 7) {
saveReq_send();
saveStat_send();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.com_error
+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
The question that I pose here is about how to structure this function in
the right way to meet this requiriments:
* The first problem that I encounter is that when I send a string with the
write command,
in the next state I have to receive the write confirmation from the PCB
and read it,
so, I would need some kind of delay to give to the thread the need time
to process this.
* The thread must be executed linearly, and in this way i think that after
executing the
first function, it goes out of the run.

Tuesday, 10 September 2013

Core Text or not Core Text?

Core Text or not Core Text?

I'm building a Bible app. I read that Core Text does not aid you in
searching or bookmarking, and I'm not sure how to display a text document.
I have the documentation in .pages format, and I display it with a
webView, but I'm not sure if this can solve my problems. Can someone give
me a suggestion on these questions, please?
1) Which framework should I use for displaying the text documentation
(bible) and still cover searching, bookmarking...
2) Should I change the document type to handle it better?

mysql select statement with alias fails with column not found

mysql select statement with alias fails with column not found

mysql select statement with alias fails with column not found
Developemt Environment Reference: Fedora FC18 Tomcat Server 7.0.39
(localhost config) Eclipse Juno Release 2 Mysql-connecor-java Ver. 5.1.26
(jar) Mysql-server Ver. 5.5.32 (jar)
The following select statment fails with "column 'depth' not found:
"select node.subEntityID, node.lft, node.rgt, (count(parent.subEntityID) -
1) as depth from ENTITY as node, ENTITY as parent where node.lft between
parent.lft and parent.rgtgroup by node.subEntityID order by node.lft";
This select statement succeeds using command level mysql as use dbName;
select node.subEntityID, node.lft, node.rgt, (count(parent.subEntityID) -
1) as depth from ENTITY as node, ENTITY as parent where node.lft between
parent.lft and parent.rgtgroup by node.subEntityID order by node.lft;
The select statement also succeeds when using an sql query in Mysql
Workbench Ver. 5.2.4.7
The relevant java code fragment is given below:
rs = stmt.executeQuery(typeEntityList);
// The depth alias does not print out in this for loop
for (int i=1; i<rs.getMetaData().getColumnCount()+1; i++) {
System.out.println(rs.getMetaData().getColumnName(i));
System.out.println(rs.getMetaData().getColumnLabel(i));
}
while (rs.next()) {
// "depth" fails at the following statement
System.out.println("depth: " + rs.getInt("depth"));
String s = rs.getString("lft") + " "
+ rs.getString("subEntityID") + " "
+ rs.getString("rgt");
System.out.println(s);
entityList.add(s);
}
Also, I found on a Google search that mysql alias behavior changed in
recent versions of mysql. A suggested fix was to append
"?useOldAliasMetadataBehavior=true" to the mysql connection string to
revert back to the original behavior, but that did not correct the problem
Any help with this problem is greatly appreciated.
Thanks, Roy W. roycwhitt@gmail.com

How does Javascript return work?

How does Javascript return work?

pThis is what I have/p precode$wrapper.find('li').sort(function (a, b) {
return +a.getAttribute('data-val') - +b.getAttribute('data-val'); });
/code/pre pIt works, as I see in Chrome dev tool an array of objects is
indeed returned but...my question is: What's up with that return? How can
I use that array? /p pWhat I want to do i to take the returned array, and
stick it inside a tag./p

Add the same calculated property to every Symfony2 Controller in bundle

Add the same calculated property to every Symfony2 Controller in bundle

I'm converting an application over from a legacy framework to Symfony2.
One of the things the app has is a tiny little message in the bottom
corner of the rendered page that says
served by appserver3 in 200ms
or similar. It changes depending on the appserver you're on and the amount
of time it took to serve the request. The value for the appserver is set
in
Should I create a BaseController type class that extends Controller and
then have all my controllers extend that? If so, how would I go about
making sure that the BaseController always adds the responseTime and
appServerName to the list of variables that will be passed to my twig
template without having to add it manually in every controller like this:
return $this->render('MyBundle:Default:index.html.twig', array('var1' =>
$var1, 'appServerName' => $this->getAppServerName(), 'responseTime' =>
$this->getResponseTime()));
(Obviously this would assume that I created the getAppServerName and
getResponseTime methods in my BaseController.)
This seems like a lot of copypasta and my whole reason for moving to
Symfony2 is to avoid that :\

ERROR 1006 (HY000) Can't create database (errno: 13) MySQL 5.6.12

ERROR 1006 (HY000) Can't create database (errno: 13) MySQL 5.6.12

I am facing problem in creating the database and it results in following
error.
mysql> show grants for 'admin'@'%';
+---------------------------------------------------------------------------------------------------------------------------------+
| Grants for admin@%
|
+---------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY PASSWORD
'*4ACFE3202A5FF5CF467898FC58AAB1D615029441' WITH GRANT OPTION |
+---------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> create database abc;
ERROR 1006 (HY000): Can't create database 'abc' (errno: 13)
mysql>
Here are my users table.
SELECT * FROM mysql.`user` u;
+-------------+-------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+------------------+
| Host | User | Password |
Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv |
Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv |
Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv |
Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv |
Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv |
Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv |
Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher |
x509_issuer | x509_subject | max_questions | max_updates | max_connections
| max_user_connections | plugin | authentication_string | password_expired
|
+-------------+-------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+------------------+
| localhost | root | *0ED7CCB23F57AFBB530688A270C080BD4956882B | Y
| Y | Y | Y | Y | Y |
Y | Y | Y | Y | Y | Y
| Y | Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| | | | |
0 | 0 | 0 | 0 | |
| N |
|test_dat-DGW | root | *292891EE28681A07B26BD53600C18994D66E8583 | Y
| Y | Y | Y | Y | Y |
Y | Y | Y | Y | Y | Y
| Y | Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| | | | |
0 | 0 | 0 | 0 | |
| Y |
| 127.0.0.1 | root | *292891EE28681A07B26BD53600C18994D66E8583 | Y
| Y | Y | Y | Y | Y |
Y | Y | Y | Y | Y | Y
| Y | Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| | | | |
0 | 0 | 0 | 0 | |
| Y |
| ::1 | root | *292891EE28681A07B26BD53600C18994D66E8583 | Y
| Y | Y | Y | Y | Y |
Y | Y | Y | Y | Y | Y
| Y | Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| | | | |
0 | 0 | 0 | 0 | |
| Y |
| % | admin | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | Y
| Y | Y | Y | Y | Y |
Y | Y | Y | Y | Y | Y
| Y | Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| | | | |
0 | 0 | 0 | 0 | |
| N |
| localhost | admin | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | Y
| Y | Y | Y | Y | Y |
Y | Y | Y | Y | Y | Y
| Y | Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| Y | Y | Y | Y
| | | | |
0 | 0 | 0 | 0 | |
| N |
+-------------+-------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+------------------+
6 rows in set (0.00 sec)
I can create and remove table in the existing database.
The data directory already has mysql:mysql privileges and also the logged
in user has privilege to create the new database.
Please guide what configuration is missed here.
Thank you in advance.