<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming Examples</title>
	<atom:link href="http://yoyar.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://yoyar.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 05 Nov 2011 20:18:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using curl to Automate WordPress Administration</title>
		<link>http://yoyar.com/blog/2011/11/using-curl-to-automate-wordpress-administration/</link>
		<comments>http://yoyar.com/blog/2011/11/using-curl-to-automate-wordpress-administration/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 20:16:58 +0000</pubDate>
		<dc:creator>Matt Friedman</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://yoyar.com/blog/?p=294</guid>
		<description><![CDATA[I am working on automating data migration from an existing site to a new WordPress version. The following scripts can help you to login to WordPress automatically, save the WordPress session cookie, and use the cookie with subsequent curl commands to automate WordPress administration. All of this is done using the curl command with no [...]]]></description>
			<content:encoded><![CDATA[<p>I am working on automating data migration from an existing site to a new WordPress version. </p>
<p>The following scripts can help you to login to WordPress automatically, save the WordPress session cookie, and use the cookie with subsequent curl commands to automate WordPress administration. </p>
<p>All of this is done using the curl command with no other dependencies (such as php). Many scripts use the php curl extension but this method is simpler and will work anywhere curl is installed without having bother with php. Few dependencies == good in my books.</p>
<p>The first bit is all about logging in.</p>
<pre class="brush: bash; title: ; notranslate">
# Let the fun begin

user=admin
password=pass
host=localhost
port=88

cookiejar=&quot;/tmp/wp-cookie.txt&quot;
baseurl=&quot;http://$host:$port&quot;
loginurl=&quot;$baseurl/wp-login.php&quot;

#csv import / bulk delete plugins mentioned in a later section
csvtool=&quot;$baseurl/wp-admin/tools.php?page=csv-importer/csv_importer.php&quot;
deltool=&quot;$baseurl/wp-admin/options-general.php?page=bulk-delete.php&quot;

postdata=&quot;log=$user&amp;pwd=$password&amp;wp-submit=Log%20In&amp;redirect_to=$csvtool&quot;

[ -e $cookiejar ] &amp;&amp; rm $cookiejar

# this will login to wp and download the session cookie
curl    --insecure \
        --connect-timeout 60 \
        --cookie-jar $cookiejar \
        --data $postdata \
        --silent \
        $loginurl

[ -e $cookiejar ] &amp;&amp; echo &quot;WP cookie created: $cookiejar&quot;

# now you have a WP session cookie!
</pre>
<p>Once you run this successfully you&#8217;ll have a valid WordPress session cookie in your /tmp dir. </p>
<p><strong>NOTE: The following snippet will delete all of your posts!</strong> Use with caution. This makes sense for me because I&#8217;m working on data migration to a new WordPress staging installation. Use at your own risk! You have been warned. </p>
<p>You need the Bulk Delete WordPress plugin for the following to work.</p>
<pre class="brush: bash; title: ; notranslate">
# prepare post data so that all posts are deleted (be careful!)
postdata=&quot;&quot;

cats=$()
for i in {0..99} # assumes you have no more than 100 categories
do
        cats[$i]=smbd_cats%5B%5D=$i
done

cats[((i++))]=&quot;smbd_cats_all=-1&quot;
cats[((i++))]=&quot;smbd_cats_force_delete=true&quot;
cats[((i++))]=&quot;smbd_cats_private=false&quot;
cats[((i++))]=&quot;submit=Bulk&amp;20Delete&amp;20»&quot;
# this plugin appears to always use the same nonce
cats[((i++))]=&quot;_wpnonce=345dabd47c&quot;
cats[((i++))]=&quot;_wp_http_referer=/wp-admin/options-general.php?page=bulk-delete.php&quot;
cats[((i++))]=&quot;smbd_action=bulk-delete-cats&quot;

joined=$(printf &quot;&amp;%s&quot; &quot;${cats[@]}&quot;)
postdata=${joined:1}

# delete the existing articles
curl    --insecure \
        --data $postdata \
        --cookie $cookiejar \
        --silent \
        $deltool

# now there's a clean database ready for new data
</pre>
<p>The follow snippet uses the csv importer. I&#8217;ve already generated a large quantity of csv import files and I will use the following to import the articles automatically.</p>
<pre class="brush: bash; title: ; notranslate">
# use csv importer wp plugin to import a csv file
curl    --cookie $cookiejar \
        --insecure \
        --form 'csv_import=@/home/somedude/import.csv' \
        --form 'submit=Import' \
        --form 'csv_importer_cat=' \
        --silent \
        $csvtool
# a new article has just been loaded into WP
</pre>
<p>By inspecting the forms that WordPress uses for administration one could use this method to automate any WordPress task. </p>
<p>I like being lazy so this really works for me. </p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://yoyar.com/blog/2011/11/using-curl-to-automate-wordpress-administration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL GROUP BY, AGGREGATE FUNCTIONS and HAVING</title>
		<link>http://yoyar.com/blog/2011/09/sql-group-by-aggregate-functions-and-having/</link>
		<comments>http://yoyar.com/blog/2011/09/sql-group-by-aggregate-functions-and-having/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 19:49:25 +0000</pubDate>
		<dc:creator>Matt Friedman</dc:creator>
				<category><![CDATA[AGGREGATE FUNCTIONS]]></category>
		<category><![CDATA[GROUP BY]]></category>
		<category><![CDATA[HAVING]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://yoyar.com/blog/?p=188</guid>
		<description><![CDATA[First, the `tl;dr&#8216; version: GROUP BY and HAVING work together with aggregate functions such as SUM(), AVG() etc&#8230; Here&#8217;s an example from later on in this post: Note: I used Postgresql to write this post so you may need to slightly adjust the queries if you are using another database such as Mysql. The GROUP [...]]]></description>
			<content:encoded><![CDATA[<p>First, the `<em>tl;dr</em>&#8216; version: </p>
<p><code>GROUP BY</code> and <code>HAVING</code> work together with aggregate functions such as <code>SUM()</code>, <code>AVG()</code> etc&#8230;</p>
<p>Here&#8217;s an example from later on in this post:</p>
<pre class="brush: sql; title: ; notranslate">
select customer_name, SUM(amount)
from customers c join purchases p on c.cid = p.cid
group by customer_name
having SUM(amount) &gt; '20.00';
</pre>
<div style="margin:10px; padding:10px; width: 210px; float:right; background-color:#F1F1F1;">
Note: I used Postgresql to write this post so you may need to slightly adjust the queries if you are using another database such as Mysql.
</div>
<p>The <code>GROUP BY</code> statement is used in together with aggregate functions to group the results by one or more columns.</p>
<p>The <code>HAVING</code> statement is used to refine aggregated results because the <code>WHERE</code> keyword can&#8217;t be used with aggregate functions.</p>
<hr />
<p>Now on to the full post for <code>GROUP BY</code>, <code>HAVING</code>, and aggregate functions.</p>
<p>Let&#8217;s start with some simple tables.</p>
<pre class="brush: sql; title: ; notranslate">
DROP TABLE IF EXISTS purchases;
DROP TABLE IF EXISTS customers;

-- Your set of customers
CREATE TABLE customers (
    cid integer NOT NULL,
    customer_name character varying(255),
    phone_number character varying(255),
    street_address character varying(255)
);

-- the purchases that your customers have made
CREATE TABLE purchases (
    pid integer NOT NULL,
    cid integer,
    amount numeric,
    purchase_date date
);

-- create the primary key for customers
ALTER TABLE customers
    ADD CONSTRAINT cid_pkey PRIMARY KEY (cid);

-- create the primary key for purchases
ALTER TABLE purchases
    ADD CONSTRAINT pid_pkey PRIMARY KEY (pid);

-- add the relational key so that each purchase
-- is related to a customer
ALTER TABLE purchases
    ADD CONSTRAINT cid_fkey FOREIGN KEY (cid)
    REFERENCES customers(cid) ON DELETE CASCADE;
</pre>
<p>Hopefully, it&#8217;s obvious that the above tables will track the set of purchases for each customer.</p>
<p>Let&#8217;s insert some data.</p>
<pre class="brush: sql; title: ; notranslate">
INSERT INTO customers
(cid, customer_name, phone_number, street_address)
VALUES
(1, 'Matt', '555 1234', '22 Tech Ave.');

INSERT INTO customers
(cid, customer_name, phone_number, street_address)
VALUES
(2, 'Susan', '334 4563', '2 Baz Lane');

INSERT INTO customers
(cid, customer_name, phone_number, street_address)
VALUES
(3, 'Sally', '333 4343', '1 Cranston Road');

INSERT INTO purchases
(pid, cid, amount, purchase_date)
VALUES
(1, 1, '20.00', '2011-08-22');

INSERT INTO purchases
(pid, cid, amount, purchase_date)
VALUES
(2, 1, '10.00', '2011-08-01');

INSERT INTO purchases
(pid, cid, amount, purchase_date)
VALUES
(3, 2, '15.00', '2011-07-11');

INSERT INTO purchases
(pid, cid, amount, purchase_date)
VALUES
(4, 3, '5.00', '2011-10-19');

INSERT INTO purchases
(pid, cid, amount, purchase_date)
VALUES
(5, 2, '5.00', '2010-08-10');

INSERT INTO purchases
(pid, cid, amount, purchase_date)
VALUES
(6, 1, '15.00', '2011-02-10');

INSERT INTO purchases
(pid, cid, amount, purchase_date)
VALUES
(7, 3, '25.00', '2010-01-02');
</pre>
<p>Now we can query things like: &#8220;<em>show me the purchases made by Matt</em>&#8221;</p>
<pre class="brush: sql; title: ; notranslate">
select customer_name, amount
from customers c join purchases p on c.cid = p.cid
where customer_name = 'Matt';
</pre>
<pre>
 customer_name | amount
---------------+--------
     Matt      | 20.00
     Matt      | 10.00
     Matt      | 15.00
</pre>
<p>That&#8217;s fairly interesting, but what if we want to know the total of the purchases made by &#8216;Matt&#8217;. You could just add up the amounts easily enough in your head but it would be too time consuming if there were many purchases.</p>
<p>So, let&#8217;s have the database to do this calculation for us. We&#8217;ll use the aggregate function <code>SUM()</code> on the amount column and <code>GROUP BY</code> on the <code>customer_name</code> column.</p>
<pre class="brush: sql; title: ; notranslate">
select customer_name, SUM(amount)
from customers c join purchases p on c.cid = p.cid
where customer_name = 'Matt'
group by customer_name;
</pre>
<p>The result is the sum of the purchases made by Matt.</p>
<pre>
 customer_name |  sum
---------------+--------
 Matt          | 45.00
</pre>
<p>More often, we want a report on the total purchases made by all of our customers.</p>
<p>Commenting out the <code>WHERE</code> clause will get us what we we&#8217;re looking for.</p>
<pre class="brush: sql; title: ; notranslate">
select customer_name, SUM(amount)
from customers c join purchases p on c.cid = p.cid
-- where customer_name = 'Matt'
group by customer_name;
</pre>
<p>Here&#8217;s the report on all of our customers:</p>
<pre>
 customer_name |  sum
---------------+--------
 Susan         | 20.00
 Sally         | 30.00
 Matt          | 45.00
</pre>
<p>Let&#8217;s pretend that we have a promotional campaign and we want to reward customers that have made total purchases greater than $20.00?</p>
<p>A <code>WHERE</code> clause won&#8217;t work in this case. That is, you can&#8217;t say <em><code>WHERE SUM(amount) &gt; $20.00</code></em>.</p>
<p><code>HAVING</code> is designed to work with <code>GROUP BY</code> to refine queries which use aggregate functions.</p>
<pre class="brush: sql; title: ; notranslate">
select customer_name, SUM(amount)
from customers c join purchases p on c.cid = p.cid
group by customer_name
having SUM(amount) &gt; '20.00';
</pre>
<p>The result no longer contains Susan&#8217;s purchases because she hasn&#8217;t in total spent more than $20.00.</p>
<pre>
 customer_name |  sum
---------------+--------
 Sally         | 30.00
 Matt          | 45.00
</pre>
<p>You may want to create your own database and execute these queries yourself. (I suggest that you do!) Then you can try things like adding other columns to the <code>SELECT</code> clause to see what happens. Try making a report on the average purchase made by each customer. Can you use <code>HAVING</code> and <code>GROUP BY</code> to make use of different aggregate functions in the same query?</p>
<p>The key things to remember are that <code>GROUP BY</code> works together with <code>HAVING</code> and aggregate functions. Using <code>WHERE</code> still works to filter out rows overall but does nothing to refine information regarding grouped or aggregated information.</p>
]]></content:encoded>
			<wfw:commentRss>http://yoyar.com/blog/2011/09/sql-group-by-aggregate-functions-and-having/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java String Literals, Construction and Comparison using ==</title>
		<link>http://yoyar.com/blog/2011/09/java-string-literals-construction-and-comparison-using/</link>
		<comments>http://yoyar.com/blog/2011/09/java-string-literals-construction-and-comparison-using/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 00:47:32 +0000</pubDate>
		<dc:creator>Matt Friedman</dc:creator>
				<category><![CDATA[Java Strings]]></category>

		<guid isPermaLink="false">http://yoyar.com/blog/?p=138</guid>
		<description><![CDATA[I recently ran across the following questions that highlight the differences in how Java String objects are constructed. My original assumption was that s1 and s2 would hold references to two different objects. Thanks to Java Prepare for the questions The documentation says that == is the object comparison operator. It compares two references to [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran across the following questions that highlight the differences in how Java String objects are constructed. </p>
<pre class="brush: java; title: ; notranslate">
public class test {
	public static void main(String args[]) {
		String s1 = &quot;abc&quot;;
		String s2 = &quot;abc&quot;;
		if (s1 == s2)
			System.out.println(1);
		else
			System.out.println(2);
		if (s1.equals(s2))
			System.out.println(3);
		else
			System.out.println(4);
	}
}

// Prints out 1 and 3
</pre>
<p>My original assumption was that <code>s1</code> and <code>s2</code> would hold references to two different objects.</p>
<pre class="brush: java; title: ; notranslate">
public class test {
	public static void main(String args[]) {
		String s1 = &quot;abc&quot;;
		String s2 = new String(&quot;abc&quot;);

		if (s1 == s2)
			System.out.println(1);
		else
			System.out.println(2);
		if (s1.equals(s2))
			System.out.println(3);
		else
			System.out.println(4);
	}
}

// Prints out 2 and 3
</pre>
<p><a rel="nofollow" href="http://www.javaprepare.com/quests/test.html">Thanks to Java Prepare for the questions</a></p>
<p>The documentation says that == is the object comparison operator. It compares two references to see if they refer to the same object. </p>
<p>The trick here is that the assignments <code>s1 = "abc"</code> and <code>s2 = "abc"</code> together create only one object. More precisely, the statement <code>"abc"</code> in the first assignment creates a String object and the statement <code>"abc"</code> in the second assignment reuses the same reference to the first <code>"abc"</code> String object. The original <code>"abc"</code> statement creates an object in a common pool which is reused as the compiler sees fit. </p>
<p>If there is a variable component to the String then <code>s1</code> and <code>s2</code> are created as separate objects. </p>
<pre class="brush: java; title: ; notranslate">
public class test {
	public static void main(String args[]) {
		String s1 = &quot;abc&quot;;
		String s2 = &quot;abc&quot; + args[0];
		if (s1 == s2)
			System.out.println(1);
		else
			System.out.println(2);
		if (s1.equals(s2))
			System.out.println(3);
		else
			System.out.println(4);
	}
}
// run as java test
// prints out 2 and 3
</pre>
<p>Running this program with no arguments creates two separate objects that have the same contents.</p>
<p>Reference: <a rel="nofollow" href="http://www3.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html">String is Special</a></p>
]]></content:encoded>
			<wfw:commentRss>http://yoyar.com/blog/2011/09/java-string-literals-construction-and-comparison-using/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java In-place String Reverse</title>
		<link>http://yoyar.com/blog/2011/09/java-in-place-string-reverse/</link>
		<comments>http://yoyar.com/blog/2011/09/java-in-place-string-reverse/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 21:52:29 +0000</pubDate>
		<dc:creator>Matt Friedman</dc:creator>
				<category><![CDATA[Java Strings]]></category>

		<guid isPermaLink="false">http://yoyar.com/blog/?p=132</guid>
		<description><![CDATA[Here&#8217;s my interpretation of how to reverse a String in Java. For entertainment value I have picked a fun string.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s my interpretation of how to reverse a String in Java. </p>
<p>For entertainment value I have picked a <a target="_new" rel="nofollow" href="http://www.omniglot.com/language/phrases/hovercraft.htm">fun string</a>.</p>
<pre class="brush: java; title: ; notranslate">
public class StringReverse {

	public static void main(String[] args) {

		String aStr = &quot;My hovercraft is full of eels.&quot;;

		char[] charbuf = aStr.toCharArray();

		for(int i = charbuf.length - 1, j = 0; i &gt; j; i--,j++ ) {

			if( charbuf[i] == charbuf[j]) {
				continue;
			}

			char temp = charbuf[i];
			charbuf[i] = charbuf[j];
			charbuf[j] = temp;
		}

		System.out.println(charbuf);
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://yoyar.com/blog/2011/09/java-in-place-string-reverse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Autowired Hibernate Configuration and SchemaExport</title>
		<link>http://yoyar.com/blog/2011/07/autowired-hibernate-configuration-schemaexport-ddl/</link>
		<comments>http://yoyar.com/blog/2011/07/autowired-hibernate-configuration-schemaexport-ddl/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 02:13:49 +0000</pubDate>
		<dc:creator>Matt Friedman</dc:creator>
				<category><![CDATA[Autowired]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[SchemaExport]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[DDL]]></category>

		<guid isPermaLink="false">http://yoyar.com/blog/?p=48</guid>
		<description><![CDATA[When configuring Hibernate in Spring, there are a few ways to get a handle on the Hibernate Configuration object in your code. I believe this way is the most concise. I wanted to get the reference to the Hibernate Configuration object so that I could use it with SchemaExport to auto-generate the ddl and save [...]]]></description>
			<content:encoded><![CDATA[<p>When configuring Hibernate in Spring, there are a few ways to get a handle on the Hibernate Configuration object in your code. I believe this way is the most concise.</p>
<p>I wanted to get the reference to the Hibernate Configuration object so that I could use it with SchemaExport to auto-generate the ddl and save it to a file for examination.</p>
<p>Here&#8217;s a simple way too do it.</p>
<pre class="brush: xml; title: hibernate-bean-config.xml; notranslate">

&lt;bean id=&quot;sessionFactory&quot;
  class=&quot;org.springframework.orm.hibernate3
   .annotation.AnnotationSessionFactoryBean&quot;&gt;
  &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;
  &lt;property name=&quot;hibernateProperties&quot;&gt;
    &lt;props&gt;
      &lt;prop key=&quot;hibernate.show_sql&quot;&gt;true&lt;/prop&gt;
      &lt;prop key=&quot;hibernate.format_sql&quot;&gt;true&lt;/prop&gt;
      &lt;prop key=&quot;hibernate.hbm2ddl.auto&quot;&gt;
        create-drop&lt;/prop&gt;
  &lt;/props&gt;
  &lt;/property&gt;
        &lt;!-- details omitted... --&gt;
&lt;/bean&gt;

&lt;!-- The &amp;amp; tells Spring to fetch the Factory object,
which is distinct from the objects that it produces.
Use the encoded entity as you see it shown here
in the factory-bean attribute. i.e. &amp;amp;
Using the factory-method attribute tells Spring which
method to call. --&gt;
&lt;bean id=&quot;hibernateConfiguration&quot;
       factory-bean=&quot;&amp;amp;sessionFactory&quot;
       factory-method=&quot;getConfiguration&quot; /&gt;
</pre>
<pre class="brush: java; title: Hibernate testing code; notranslate">

@Autowired
Configuration hibernateConfiguration;

@Test
public void testHibernateConfigInjection() {
  assertNotNull(hibernateConfiguration);
  assertTrue(hibernateConfiguration
    instanceof Configuration);
}

@Test
public void runSchemaGeneration() {
  SchemaExport export
    = new SchemaExport(hibernateConfiguration);
  export.setOutputFile(&quot;my-schema.sql&quot;);
  export.setDelimiter(&quot;;&quot;);
  export.execute(true, false, false, true);
}
</pre>
<p>You could load the application context explicitly in your code, but then you&#8217;d have to mention the path to the xml file. This approach hides the path from your code and so I think it is more resilient to changes in the bean xml.</p>
<p>I hope this saves you some time and messing around.</p>
<p>For your reference:<br />
<a rel="nofollow" href="http://static.springsource.org/spring/docs/3.0.0.RELEASE/reference/html/beans.html#beans-factory-class-instance-factory-method">Spring Documentation &#8211; Instantiation using an instance factory method</a></p>
]]></content:encoded>
			<wfw:commentRss>http://yoyar.com/blog/2011/07/autowired-hibernate-configuration-schemaexport-ddl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

