понедельник, 5 апреля 2010 г.

Quick Tip: An Introduction to jQuery Templating

JavaScript Templating is a neat idea: it allows you to easily convert JSON to HTML without having to parse it. At Microsoft’s MIX10 conference, they announced that they are starting to contribute to the jQuery team. One of their efforts is to provide atemplating plugin. In this quick tip, I’ll show you how to use it!
You’ll need the data to template; you’ll likely retrieve JSON from your server; of course, Object / Array literals work just as well, so that’s what we use:
  1. var data = [
  2. { name : "John",  age : 25 },
  3. { name : "Jane",  age : 49 },
  4. { name : "Jim",   age : 31 },
  5. { name : "Julie", age : 39 },
  6. { name : "Joe",   age : 19 },
  7. { name : "Jack",  age : 48 }
  8. ];
var data = [
  { name : "John",  age : 25 },
  { name : "Jane",  age : 49 },
  { name : "Jim",   age : 31 },
  { name : "Julie", age : 39 },
  { name : "Joe",   age : 19 },
  { name : "Jack",  age : 48 }
 ];
The template is written in tags; for each item in your JSON, the template will render the HTML; then, it will return the entire HTML fragment to you. We can get to the JavaScript values from within the template by using {% and %} as tags. We can also execute regular JavaScript within these tags. Here’s our template:
  1. <li>
  2. <span>{%= $i + 1 %}span>
  3. <p><strong>Name: strong> {%= name %}p>
  4. {% if ($context.options.showAge) {  %}
  5. <p><strong>Age: strong> {%= age %}p>
  6. {% } %}
  7. li>
  • {%= $i + 1 %} Name: {%= name %}
    {% if ($context.options.showAge) { %} Age: {%= age %}
    {% } %}
  • To render the data with the template, call the plugin; pass the data to the plugin method; you can optionally pass in an options object as well. (These aren’t predefined options; they’re values you want to use within the template, perhaps for branching.)
    1. $("#listTemplate").render(data, { showAge : true }).appendTo("ul");
    $("#listTemplate").render(data, { showAge : true }).appendTo("ul");
    
    Source: http://net.tutsplus.com

    What Is the Top Mobile Platform for Open Source Developers?

    Mobile platforms like Apple's iPhone and Google's Android have become a key focus for open source developers. And the trend is only increasing, though new research has found that over the course of the last year, there has been a shift in which mobile platform has the most open source development activity.
    A new study by Black Duck Software found that at the end of 2009, there were 224 new open source software projects on Google's Android operating system, bringing its total to 357 open source projects in all. That's enough to leapfrog Apple's (NASDAQ: AAPL) iPhone to take the top spot in the number of open source projects being developed on either platform.
    Android's 2009 open source software tally represents a 168 percent gain over the number of projects reported for it in 2008.
    In contrast, Apple's iPhone garnered 76 new open source software project in 2009, representing 43 percent growth over 2008. In total, Black Duck reported that at the end of 2009 there were 252 open source software projects for Apple's iPhone.

    Overall, 2009 was a year of expansion for open source development across the whole of the mobile realm.
    "We're seeing robust growth in open source projects for mobile platforms, with 39 percent growth in the number of projects that are available [and] over 3,200 available now," Peter Vescuso, executive vice president of marketing and business development at Black Duck Software, told InternetNews.com. "The dynamics within that are pretty interesting with Android representing really the bulk of the growth with 224 projects, which is about 25 percent of all new projects."
    Coming in third place behind Android and iPhone is Windows Mobile with 75 new projects in 2009, raising its total to 248 open source software projects in all. According to a recent study,Windows Mobile has been losing share in terms of adoption over the course of the last year.
    Black Duck's data comes from its knowledge base, which is produced by a team of people referred to as "spiders" that scour every known open source repository to collect data. The Black Duck knowledge base is also at the heart ofBlack Duck's business, which aims to provide users with information on code licensing and related issues.
    In terms of methodology, Black Duck's report isn't limited to looking at only full-fledged, downloadable applications that reside in an app store.
    "Not all of these projects are applications. Some are but there also libraries, widgets and frameworks," Vescuso said. "None of these projects come from app stores -- they come from the project repositories. We're going right to the source. We're not going to the stores where these projects might be available for download."
    Vescuso noted that Android likely gained in popularity in 2009 thanks in part to its open source nature and the fact it has the support of a wide range of handset vendors and operators.
    Still, Vescuso added that open source and the iPhone also have an important relationship.
    "Even though you think of the iPhone as a closed, proprietary platform, it is significantly built on open source," Vescuso said. "It heavily leverages open source and so it benefits from open source. In an odd way ... as the iPhone succeeds, to a large extent, open source is succeeding -- even though iPhone is not an open platform."

    Source: http://www.developer.com

    10 Cool Firefox Add-Ons

    This is a must-read for Firefox fans! We'll review 10 cool add-ons that will make your cross-platform Mozilla web browser even better. We'll review add-ons to help fix annoyances, save time, discover advanced functionality, and stay connected. Lets get started!

    #1 NewTabURL to control your new tab page

    One of my pet peeves about tabbed web browsers is the blank tab page. More specifically, it annoys me when I click to open another tab and I get a "New Tab" page, or completely blank page. I'm a Google fan; I want Google to load. I just don't understand why Microsoft and Mozilla won't make the default setting load your homepage for new tabs!
    If you're the same way, you may have already checked (and double-checked) the tab settings in Firefox and found no homepage setting for new tabs. However, there are add-ons that will help; for instance, NewTabURL. This add-on lets you choose the URL for new tabs: blank page, home page, current page, or specific URL.
    NewTabURL also gives you another feature that automatically loads URLs from the clipboard. For example, you can copy a website address from a document, browser, or anywhere and when you open another tab in Firefox, the copied URL will automatically load in the new tab.

    #2 iMacros for Firefox for automating browser tasks and tests

    This is a very interesting add-on, giving you the ability to record and play macros in Firefox. Pretty much anything repetitive you do in Firefox you can automate with iMacros. You can teach it to fill out forms or download and upload files. It can import or export data to and from CSV or XML files or databases. It even includes support for working with PDF files, capturing screenshots, user agent simulation, and proxies.
    iMacros for Firefox also includes a password manager. These passwords can be used within macros. Plus they can be secured with 256-Bit AES encryption

    #3 Web Developer to design, test, and troubleshoot sites and applications

    This add-on is great for anyone that designs or maintains websites or web applications. It gives you a new Firefox menu and toolbar with various web developer tools. Use it to test, inspect, or troubleshoot cookies, forms, images, and many other web components.
    It gives you control over client-side settings by letting you easily toggle Java, JavaScript, cache, cookies, pop-blocker, and other features on and off. You can view CSS details and even edit the style sheets to see live results. It includes many inspection and manipulation features for forms and images. It also features code validaters and many other miscellaneous tools.

    #4 Yoono for keeping tabs on your social networking and IM friends

    Yoono is a must-have add-on for anyone that communicates via social networks and/or instant messaging services. It can serve as a single spot to check your social networking feeds and update your status for all networks at once. If you use multiple sites or services, this add-on can save you a lot of time.

    #5 Gmail Manager for quick and easy Gmail access

    If Google's Gmail is your email provider of choice, you ought to check out the Gmail Manager add-on. It gives you a icon in the status bar of Firefox, loaded with shortcuts to create and checks messages among other tasks. You'll be notified of incoming messages. It even detects email links and can bring up Gmail when clicking on mailto links. Best of all, Gmail Manager supports multiple accounts.
    Source: http://www.linuxplanet.com

    The New Open Source Business Model Still Relies on Closed Source

    Over the last couple of years a number of different open source business strategies have evolved. According to the 451 Group, it's an evolution that includes the broader adoption and usage of open source overall by both open source and proprietary software vendors.
    Back in 2008, the 451 Group put out a landmark report on open source business strategies. According to 451 Group analyst Matt Aslett there has been some change since then. Among the changes is a decline in the dual-licensing strategy that was once a popular business strategy for vendors aiming to profit from their open source technologies.
    "I didn't expect it to be significant but when I looked at the vendors we analyzed in 2008 16 percent were using dual-licensing strategy," Aslett, analyst at the 451 Group told InternetNews.com. "In 2010, it's just 5 percent of the same 114 vendors -- that really bears out the fact that there has been a shift away from dual licensing ."
    Dual-licensing is an approach whereby the vendor provides their software under both an open source and a commercial license. It's an approach that was popularized by open source database vendor MySQL. Instead of going the dual-licensing route as a business strategy, other models and approaches have emerged to take its place.

    The Open Core model

    "We've seen a few of the dual-licensed vendors that have dropped the commercial version and have gone the pure open source approach and just relying on support and services revenue," Aslett said. "We've seen a lot more move to the Open Core model. We saw that grow from 24 percent of vendors in 2008 to 30 percent today."
    Aslett defines the Open Core as one where there is a core open source project for which the vendor supplies proprietary extensions. While the dual-licensing is different from open core, there is at least one key similarity.
    "At the end of the day both open core and dual-licensing involve commercially licensed proprietary software," Aslett said. "Both strategies enable a vendor to have some control over the commercial aspects of the business strategy and the enterprise version."
    Overall, Aslett noted that much has changed in open source usage since his 2008 report such that it is now more difficult to actually isolate and identify all of the vendors that have an open source business strategy. More vendors than ever are now using open source at different points in their process and applications and usage is not limited to pure-play open source vendors.
    "If you look know and see how a company like IBM, Oracle or SAP or Microsoft are making money from open source it's not in the way that we've traditionally seen open source specialists make money," Aslett said. "It's though complementary products and services. So those are very different strategies that don't necessarily focus on commercializing the open source software directly."
    For those vendors that are open source specialists, Aslett sees the big challenge as figuring out how to convert community users into paying users. He noted that it's a balancing act for many vendors as not every user wants or needs to be sold on additional services.
    "I think that a lot of vendors have gotten better at realizing to not try and convert all community users as it actually could have a detrimental effect on the image of the company," Aslett said. "So they've been a lot more clever about the techniques they use to make sure they capture users when they're at the point that they want to engage in a subscription or get a commercially licensed extension."
    Source: http://www.linuxplanet.com

    MySQL Prepared Statements to Generate Crosstab SQL

    MySQL Reporting requirements sometimes require both unknown column and row values, necessitating a more powerful means of generating crosstabs. Today's article presents Prepared Statements, which dynamically generate the SQL and assign it to a variable so that we can tailor the output based on the number of data values.
    During the past several weeks, we've been learning how to create crosstabs in MySQL. We've now covered the basics of fetching the row and column data, as well as how to overcome some of the challenges inherent to outputting data as columns. As we saw, organizing data into columns can be an arduous task due to the SQL language's natural tendency of appending data to rows. We can transpose row data to columns easily enough, but the number of possible data values in the horizontal axis need to be known before hand.
    Unfortunately, there will be times that your reporting requirements will require both unknown column and row values, or have a tendency to change often enough to invalidate previous code. In such instances, you need a more powerful means of generating crosstabs. Today's article presents just such a mechanism: Prepared Statements. By dynamically generating the SQL and assigning it to a variable, we can tailor the output based on the number of data values, thus unburdening us from having to anticipate changes.


    How Static Is the Data Really?

    Way back in the Tips for Simplifying Crosstab Query Statements article, we were introduced to a crosstab report that displayed the number of cases by Region broken down by month, and later, by year as well. No sooner was the article published than someone asked the question "What happens when a new Region is introduced?" The answer is simple: an extra column must be added to the SELECT field list. This is straightforward enough to do and can be expected to occur very rarely, as Regions are what you would call a static data set. Other than Regions, other geographic entities, including continents, countries, counties, provinces, states, and cities can also be considered to be static. Having said that, even fixed data sets such as time frames can vary enormously. Now I'm not referring to the elasticity of time-space, as discovered by Einstein, but rather, how the start and end points of a SELECT query depend on reporting needs. With regards to our own crosstab query, going from a single year to multiple ones necessitated many changes to the SQL.
    At the other end of the spectrum is variable data, which can change drastically from one report to the other. Imagine reporting on Starbucks coffee houses in the nineties boom period! Since you could expect the number of shops to increase on an almost daily basis, you’d definitely need a more flexible approach!

    Steps in Converting the Query into a Prepared Statement

    Going from an SQL statement to a Prepared Statement will be done in two steps:
    • First, we'll rewrite the query to generate the Prepared Statement whose output will vary according to the number of columns.
    • Second, we'll insert the SQL generating Prepared Statement into a stored proc, so that we can create the Prepared Statement and execute it in one fell swoop.

    Dynamically Generating the SQL Statement

    In order to dynamically generate an SQL string, we’ll be using the CONCAT() and GROUP_CONCAT() string functions.
    The CONCAT() function accepts a variable number of string parameters, and returns another string, which is comprised of all the input parameters joined together in the order that they were passed in. The following code would concatenate a name in last name (comma) first name format:
    SELECT CONCAT(last_name, ", ", first_name) AS NAME
    FROM   CLIENTS;
    Produces:
    NAME
    _____________
    Jones, Paul
    McDonald, Jim
    Miller, Bruce
    Portman, Bess
    The GROUP_CONCAT() function returns a string result with the concatenated non-NULL values from a group. Here, it's used to aggregate all the rows from the TA_CASES table and return the collection of SELECT list expressions that makes up the horizontal axis of the crosstab. The following query returns a string value that replaces the SQL statement of our previous crosstab query:
    SELECT concat(
        "SELECT CASE WHEN Month_Num IS NULL", "\n", 
        "            THEN 'TOTAL'", "\n", 
        "            ELSE Month", "\n", 
        "       END        AS 'Month',", "\n",
        group_concat( DISTINCT concat("       REGION_", REGION_CODE, 
                                      "  AS 'REGION ", REGION_CODE, "',", "\n"
                               )
                      order by REGION_CODE
                      separator '' 
                    ),
        "       TOTAL", "\n",
        "FROM  (     SELECT   MONTH(CREATION_DATE)\t\t\t\t\t\t\t\tAS Month_Num,", "\n",
        "\t\tMONTHNAME(CREATION_DATE)\t\t\t\t\t\t\t\t\tAS 'Month',", "\n",  
        group_concat( 
            DISTINCT concat("\t\t\t\tCOUNT(CASE WHEN REGION_CODE ='", REGION_CODE, 
                            "' THEN FEE_NUMBER ELSE NULL END) AS 'REGION_", 
                            REGION_CODE, "',", "\n"
                     )
            order by REGION_CODE
            separator '' 
        ),
        "            COUNT(*)\t\t\t\t\t\t\t\t\t\t\t\t\tAS 'TOTAL'", "\n",
        "            FROM  TA_CASES", "\n",
        "            WHERE YEAR(CREATION_DATE)=", YEAR(CREATION_DATE), "\n",
        "            GROUP BY Month_Num WITH ROLLUP) AS CA;"
    ) statement
    FROM TA_CASES
    WHERE YEAR(CREATION_DATE)=1998;
    Here is the resulting SQL code as created by our dynamic SQL generator:
    SELECT CASE WHEN Month_Num IS NULL
                THEN 'TOTAL'
                ELSE Month
           END        AS 'Month',
           REGION_01  AS 'REGION 01',
           REGION_02  AS 'REGION 02',
           REGION_03  AS 'REGION 03',
           REGION_04  AS 'REGION 04',
           REGION_05  AS 'REGION 05',
           TOTAL
    FROM  (SELECT    MONTH(CREATION_DATE)
             MONTHNAME(CREATION_DATE)  
                     COUNT(CASE WHEN REGION_CODE ='01' THEN FEE_NUMBER ELSE NULL END) AS 'REGION_01',
                     COUNT(CASE WHEN REGION_CODE ='02' THEN FEE_NUMBER ELSE NULL END) AS 'REGION_02',
                     COUNT(CASE WHEN REGION_CODE ='03' THEN FEE_NUMBER ELSE NULL END) AS 'REGION_03',
                     COUNT(CASE WHEN REGION_CODE ='04' THEN FEE_NUMBER ELSE NULL END) AS 'REGION_04',
             COUNT(CASE WHEN REGION_CODE ='05' THEN FEE_NUMBER ELSE NULL END) AS 'REGION_05',
                     COUNT(*) 
           FROM  TA_CASES
           WHERE YEAR(CREATION_DATE)=1998
           GROUP BY Month_Num WITH ROLLUP) AS CA;

    Moving the Prepared Statement into a Stored Procedure

    Placing our code in a stored proc will make running the query a lot easier as it can generate the statement and then execute it to retrieve the results. Here is the code for the stored proc:
    CREATE PROCEDURE `p_case_counts_per_region_by_month`() 
    LANGUAGE SQL 
    NOT DETERMINISTIC 
    CONTAINS SQL 
    SQL SECURITY DEFINER 
    BEGIN  
      SELECT concat(
        "SELECT CASE WHEN Month_Num IS NULL", "\n", 
        "            THEN 'TOTAL'", "\n", 
        "            ELSE Month", "\n", 
        "       END        AS 'Month',", "\n",
        group_concat( DISTINCT concat("       REGION_", REGION_CODE, 
                                      "  AS 'REGION ", REGION_CODE, "',", "\n"
                               )
                      order by REGION_CODE
                      separator '' 
                    ),
        "       TOTAL", "\n",
        "FROM  (     SELECT   MONTH(CREATION_DATE)\t\t\t\t\t\t\t\tAS Month_Num,", "\n",
        "\t\tMONTHNAME(CREATION_DATE)\t\t\t\t\t\t\t\t\tAS 'Month',", "\n",  
        group_concat( 
            DISTINCT concat("\t\t\t\tCOUNT(CASE WHEN REGION_CODE ='", REGION_CODE, 
                            "' THEN FEE_NUMBER ELSE NULL END) AS 'REGION_", 
                            REGION_CODE, "',", "\n"
                     )
            order by REGION_CODE
            separator '' 
        ),
        "            COUNT(*)\t\t\t\t\t\t\t\t\t\t\t\t\tAS 'TOTAL'", "\n",
        "            FROM  TA_CASES", "\n",
        "            WHERE YEAR(CREATION_DATE)=", YEAR(CREATION_DATE), "\n",
        "            GROUP BY Month_Num WITH ROLLUP) AS CA;"
      ) statement
      into @case_counts_per_region_by_month
      FROM TA_CASES
      WHERE YEAR(CREATION_DATE)=1998;
    
      prepare case_counts_per_region_by_month   
      from @case_counts_per_region_by_month;    
      execute case_counts_per_region_by_month;   
      deallocate prepare case_counts_per_region_by_month; 
    END
    Inside the procedure, we generate the SQL for the query as we did above, but within a proc we can save it to a variable using the SELECT INTO syntax. A Prepared Statement is then utilized to execute the generated code.
    A SELECT INTO can only be used where the SQL returns exactly one row; yet another reason that generating the SQL statement as a string works so well!
    A Prepared Statement is a combination of three separate SQL statements:
    • PREPARE prepares a statement for execution.
    • EXECUTE executes a prepared statement.
    • DEALLOCATE PREPARE releases a prepared statement.
    Once the proc has been created, all we need to do is call it by entering the following command line:
    mysql> call p_case_counts_per_region_by_month;
    Here is the record set that is returned by our proc:
    Month REGION 01 REGION 02 REGION 03 REGION 04 REGION 05 TOTAL
    April 13 33 76 2 47 171
    May 17 55 209 1 143 425
    June 8 63 221 1 127 420
    July 13 104 240 6 123 486
    August 18 121 274 9 111 533
    September 25 160 239 2 88 514
    October 9 88 295 2 127 521
    November 2 86 292 2 120 502
    December 1 128 232 6 155 522
    TOTAL 106 838 2078 31 1041 4094
    In the last installment of the Crosstab series, we'll make the proc more generic by moving the columns and table(s) names that we're reporting on to input parameters.
    Source: http://www.databasejournal.com