All Things Techie With Huge, Unstructured, Intuitive Leaps

MySQL - Getting a List of Users by who logged in last

I have a list of users.  I collect their activity. I want to get a list of users, but I want to order it by who logged in last.  Some users haven't logged in in months and don't appear in the login table.  There are two tables.  One is a users table and the other is a loginTable.  The login time is recorded by a timestamp.  Here is one way of getting the list and ordering by the last login.


SELECT users.id, MAX(loginTable.timeIn), loginTable.userId  FROM  loginTable
right join users on loginTable.userId = users.id
ORDER BY MAX(loginTable.timeIn) DESC ;

The right join insures that we get all of the users, even those who don't appear in the loginTable.  This is a slow query for a large user table, but since we are collecting metrics it doesn't matter.

Hopes this help someone.

Smart Road JSON or XML Template For Messaging and Data Transmission and Receiving



With the Internet of Everything here among us, and GPS is ubiquitous in vehicles, and cars will have full internet capability, it will not be long before we have Smart Roads.  Each road will have an IP address.

I began wondering what the data package would look like for Smart Roads, and then I was struck with the axiom that the best way to predict the future is to invent it.

So without further ado, I cracked together and XML file of what the Smart Road dataset would look like.  Here is my first crack at creating a Smart Road Data Standard:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Smart Road Markup Language version 1.0-->

<smart_road>  
<!-- Main Element.  It consists of a header, data and trailer elements. -->


    <header>
      <!-- Header Element.  -->

      <ref_number></ref_number> <!-- This could be a database Primary Key Identifier.  -->
      <country></country>
      <state></state>
      <province></province>
      <county></county>
      <township></township>
      <ip6_address></ip6_address> <!-- Each Smart Road will have their own IP address.  -->
      <start_gps></start_gps>
      <classification></classification> <!-- There could be subclassifications like carriageways etc.  -->
      <end_gps></end_gps>
      <length_kms></length_kms>
      <maximum_speed_limit></maximum_speed_limit>
      <options></options> <!-- This could be a control element for suppressing some header info on subsequent exchanges.  -->

     </header>

  <data> <!-- Message Section.  -->
  <data_sent>
     <general_data> <!-- This section contains general data sent to the vehicles  -->
            <alerts>
                   <current_alerts></current_alerts>
                   <upcoming_alerts></upcoming_alerts>
             </alerts>
            <flags></flags>
           <messages>
              <alerts></alerts>
              <construction></construction>
              <law_enforcement></law_enforcement>
              <traffic></traffic>
              <weather></weather>
             <misc></misc>
            <user_specific>
                 <destination_address></destination_address>
                 <ack_flag> </ack_flag>
                <message_payload></message_payload>
                <delivery_receipt></delivery_receipt>
            </user_specific>
         </messages>
    </general_data> 
      <location_specific_data><!-- This section contains location-specific sent to the vehicles  -->
          <current_gps_marker>
               <current_maximum_allowable_speed></current_maximum_allowable_speed>
                <alerts>
                      <current_alerts></current_alerts>
                      <upcoming_alerts></upcoming_alerts>
                </alerts>
                <flags></flags>
               <messages>
               <alerts></alerts>
               <construction></construction>
               <law_enforcement></law_enforcement>
               <traffic></traffic>
               <weather></weather>
              <misc></misc>
               <user_specific><!-- It is anticipated that if a vehicle's onboard messaging is not working, one can billboard messages -->
               <!-- This can also be used to send violation notices and service-related messages to the vehicle -->
                     <destination_address></destination_address>
                     <ack_flag> </ack_flag>
                    <message_payload></message_payload>
                    <delivery_receipt></delivery_receipt>
               </user_specific>
              </messages>
       </current_gps_marker>
      </location_specific_data>
   </data_sent>
   <data_received>
        <vehicle>
           <type></type>
           <description></description>
           <direction></direction>
           <velocity></velocity>
           <timestamp></timestamp>
        </vehicle>
   </data_received>
  </data>

  <meta-data>
  <!-- Big Data meta data on usage stats etc.-->
  </meta-data>
  <trailer>
    <protocols_supported> </protocols_supported><!-- Various devices will have their own native Smart Road protocols -->
    <device_types></device_types>
    <end></end>
    
  </trailer>
</smart_road>

This is just a first iteration.  It needs to be tested and validated in real time.  It is anticipated that the vehicle will send its GPS coordinates to the database defined by the IP address, and the location specific data will be returned.

Java - How to calculate and find out how many seconds to an event in the future

Java lesson.  Let's suppose that you have an event happening in the future.  The date and time of that event is stored in a database and you want to calculate how many seconds there are to that event. Once you get seconds, you can divide by 60 to get minutes, and again by 60 to get hours and then by 24 to get days etc.

Here is a code snippet that lets you do that:

Date now = new Date();
Timestamp eventStart = rs.getTimestamp("eventStart");
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(eventStart);
Date endInterval = gc.getTime();
long difference = Math.abs(endInterval.getTime() - now.getTime());
long secondsToEvent = difference / (1000);

Date is java.util.Date. Timestamp is java.sql.Timestamp.

Hope this helps someone.

How to find and show line numbers in Wordpad



I had an exception, an error that was being thrown from a compiled .jsp file.  Strangely, the console would not give me the line number of the uncompiled jsp, but rather it gave me the line that was causing the exception in the compiled jsp file that was in the work directory.

I didn't feel like opening the file in text editor in Eclipse to locate the line, so I opened the jsp_java file with Wordpad.  The trouble is that Wordpad doesn't show line numbers.  I found the line numbers in Wordpad with this hack:

1) Select the entire text with Select All (CRTL +A will do it).

2) Expand the bullets menu item and click on the numbered list eg 1. 2. 3. etc

3) Every single new line will now be numbered and you scroll down to the offending line.

Hope this helps someone.

Code Snippet for Creating Secure Token in Java



Tokens can be used for many things.  Among them are Single Sign On, or saving an identification number without exposing it to the web.  Here is a quick code snippet to create a 16 character token

First you need an import:

import java.security.SecureRandom;

here is the code snippet:

String token1 = null;
SecureRandom secureRandom = new SecureRandom();
String secString = new BigInteger(130, secureRandom).toString(32);
token1 = secString.substring(0, 15);  //creates a 16 character token

Alter the substring end value to change the size of the token.

Hope this helps someone.

Amazon Mobile Ads API


Amazon

Dear Developer,
We are very excited to announce that the Amazon Mobile Ads API is now
available for iOS apps. Now you can monetize apps across platforms,
including Android, iOS and Kindle Fire tablets and Fire phones.

With the Amazon Mobile Ads API, you have the opportunity to earn
more and you are paid on ad impressions served instead of clicks on
 ads. The Amazon Mobile Ads API is easy to integrate and delivers
highly-relevant banners, rich media and higher paying interstitials
from Amazon and brand advertisers ensuring great experience for
 your users.

With this release, we are also expanding ad serving to users in Italy
and Spain across both Android and iOS. Starting today, the Amazon
Mobile Ad Network is serving ads to Android and iOS users in the US,
 the UK, Germany, France, Spain and Italy.

Developers currently using the Amazon Mobile Ads API in their iOS
 apps share their experience*:

“Amazon Mobile Ads API is consistently delivering 20% higher eCPM
 on both Android and iOS platforms compared to other ad networks.
In addition to increased earnings, the high-quality tailored ads provides
 an engaging experience for our users achieving 25% higher CTR”
                               - Tim Oswald, Co-Founder & CEO of Harpan LLC.

“Amazon Mobile Ad Network is a top ad network for our iOS and Android
apps. Our eCPM with Amazon Mobile Ad Network are higher across the board.”
                              - Todd Moore, Founder & CEO of TMSOFT LLC.
* Your results may vary.

For a limited time, when you send the first ad request from a qualified app,
 you will receive a guaranteed $6 CPM on interstitial ads (up to a maximum
of 1 million impressions per app per month). Qualifying apps now include
 iOS apps. Please find more details here.
    To get started with the Amazon Mobile Ads API check out our Developer
    Portal here as well as our blog with tips on ad mediation and tracking ad 
    events inside your apps.

    Best Regards,
    Your Amazon Mobile Ad Network team

    Diffusion Without Photoshop Filters

    (click on picture for panorama view)

    I photographed this bucolic scene in the country, and I knew that because of the dramatic clouds and fields that I was going to make it look like a painting.  My normal way of doing this, as an uber geek, is to feed it into Photoshop, and use an array of software filters.

    However, it was raining that day, and I had my camera in a ziplock bag.  I decided to shoot through the ziplock bag for a diffusion screen.  It turned out fantastic.  The result is above.  It is a great way to get diffusion without Photoshop software image manipulation filters.

    A Java class to escape illegal characters



    Here is a Java Class to escape illegal characters with a static method:



    package com.mypackage;

    import java.util.HashMap;
    import java.util.regex.Pattern;

    /**
     * The Class StringHelper.
     */
    public class StringHelper {

      /**
    * Escapes characters that have special meaning to regular expressions.
    * 
    * @param s
    *            String to be escaped
    * @return escaped String
    */
      public static String fixString(String s){


        int length = s.length();
        int newLength = length;
        // first check for characters that might
        // be dangerous and calculate a length
        // of the string that has escapes.
        for (int i=0; i<length; i++){
          char c = s.charAt(i);
          if (!((c>='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z'))){
            newLength += 1;
          }
        }
        if (length == newLength){
          // nothing to escape in the string
          return s;
        }
        StringBuffer sb = new StringBuffer(newLength);
        for (int i=0; i<length; i++){
          char c = s.charAt(i);
          if (!((c>='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z'))){
            sb.append('\\');
          }
          sb.append(c);
        }
        return sb.toString();
      }
    }

    Hope this helps someone.

    Upcoming CloudKit Data Wipe

    This email came from Apple:



    Dear Developer,

    We will be performing a server-side data wipe on all CloudKit public and private databases for iOS 8 beta and OS X Yosemite Developer Preview users on July 7, 2014.  The following iCloud features will be affected: iCloud Drive, iCloud Photo Library, and Mail Drop. 

    Photos and videos stored in iCloud Photo Library will remain on their original iOS 8 devices and will upload to iCloud Photo Library again automatically. iCloud Drive can be re-enabled from Set up Assistant after upgrade. If you choose to store your documents in iCloud Drive, your Documents & Data will automatically be copied to iCloud Drive. iCloud Drive will not update across earlier seeds or operating systems. Attachments sent through Mail Drop will expire and need to be resent after you upgrade.

    If you have any questions, visit the Apple Developer Forums.

    Best regards, 
    Apple Developer Technical Support

    Getting Freshdesk.com SSO to work



    We recently decided to use Freshdesk.com for our help desk, knowledge base etc.  Since we want our knowledge base to be private for clients only, Freshdesk requires a login.  We don't want to make our clients log in twice, so Freshdesk has this SSO or Single Sign On token system.

    We use Java, and there was a java servlet showing how to sign on with a token.  However the online java in a repository didn't quite work.

    Here is a working Java servlet for the Freshdesk SSO token system:

    import java.io.IOException;
    import java.math.BigInteger;
    import java.net.URLEncoder;
    import java.security.MessageDigest;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.application.User; (replace with your own user bean)

    public class FreshDeskSSOServlet extends HttpServlet {

    private static final long serialVersionUID = 7027717204177362374L;
    private final String BASE_URL = "baseUrl including trailing slash" + "login/sso";
    private final String sharedSecret = "put in shared secret";



    @Override

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    try {
    String url = this.getSSOURL();
    response.sendRedirect(url);
    } catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
        System.out.println(e.getMessage());
    }
    }

    private static String getHash(String text) throws Exception {
        MessageDigest m=MessageDigest.getInstance("MD5");
            m.update(text.getBytes(),0,text.length());
        return ""+new BigInteger(1,m.digest()).toString(16);
    }



    private String getSSOURL() {

    String hash = null;
    String url = null;
    User user; //Get the user details using your current authentication system
    String name = "First & Last Name";// Full name of the user
    String email = "email@youraddress.com";// Email of the user
    try {
    hash = getHash(name + email + sharedSecret);
    } catch (Exception e1) {
    System.out.println(e1.getMessage());
    e1.printStackTrace();
    }

    try {
    name = URLEncoder.encode(name);
    email = URLEncoder.encode(email);
    url = BASE_URL + "?name="+name+"&email="+email+"&hash=" + hash; 


    }catch (Exception e) {
    //Handle appropriate code
    System.out.println("There is an exception while constructing the URL");
    e.printStackTrace();
    }

    return url;

    }
    }

    Hope this helps.

    Javascript Evaluation Errors in Eclipse

    I have a big huge app developed in Eclipse Juno.  The app has a lot of stuff including some jQuery and other javascript frameworks.  Every time that I do a build, I get errors in the Problems tab.  When I look at them, they are all errors that Eclipse says that I have in the Javascript libraries, yet they work fine.  So what to do?

    The obvious thing is to turn off the javascript evaluation in Eclipse.  This is how you do it:


    1. Right click your project
    2. Select Properties -> JavaScript -> Include Path
    3. Select Source tab. ( It's identical to Java Build Path Source tab )
    4. Expand JavaScript source folder
    5. Highlight Excluded pattern
    6. Click Edit button
    7. Click Add button next to Exclusion patterns box. (You can either use Ant-style wildcard patterns or click Browse button to mention the JavaScript file by name).
    Hope this helps someone.

    Joining Shutterstock.com


    I take a lot of pictures.  A camera is never very far away from me.  I lead an eclectic life and I travel a lot.  I have lived in a lot of neat places.  I have a huge amount of pictures.  Some are good.  I decided that I might as well monetize my pics. I would sell them as stock photos.  Shutterstock seemed the place to do it.  They seem to charge the most for stock photos to consumers.

    The first step is signing up.  No problem.  Took seconds to do. Then they want 10 of your best shots to evaluate.  I figure that most of my work is very good, so I picked 10 at random from a day's worth of shooting.  Here are my ten photos:











    These are all high res, so you can click on any of them to get a larger version.

    I figured that these ten photos were good enough to get me accepted as a photographer on the site.  Some of them may not be very marketable as stock photos, but they do demonstrate artistic and technical quality.

    OK, so I had my portfolio.  The next step was the deal killer for me.  They wanted me to upload a piece of official government identification.  Not on your frigging life.

    Every since LinkedIn was hacked and I found my password on the hacked site, do I trust anyone with my identity.  To upload a document like that is anathema to me. I am a crusader for data privacy.  I simply do not trust them with my info.

    And when I saw the royalties (you can google them), I decided that Shutterstock.com was not for me.  The deal killer was them wanting identity information in the form of government documentation.  No thanks.  Live Free or Die.

    By the way, all images are copyrighted by me.

    Typepad Major Outage this Weekend


    This weekend, I went to read one of my favorite blogs, and I got a 404 not found error.  Since this blog is slightly political, and somewhat critical of the government in a banana republic, I was concerned because the owner is also a friend of mine.  This is the government that arrested a prosecuted an activist who posted the picture of a person who died while suffering injuries in police custody.  Yes, the Bahamas is that country, and corruption and political interference in basic freedoms (speech, sexual orientation, equality of women) is a fact of life there.

    As it turns out, Typepad was down with a major outage for the weekend.  They did have Twitter reports as to their status.  I feel better about my software bugs when major sites screw up.

    I had to laugh at Typepad though.  They had this disclaimer:

    This page (http://www.typepad.com/) is currently offline. However, because the site uses CloudFlare's Always Online™ technology you can continue to surf a snapshot of the site. We will keep checking in the background and, as soon as the site comes back, you will automatically be served the live version. Always Online™ is powered by CloudFlare | Hide this Alert

    The CloudFlare thing didn't work. So much for CloudFlare. Here is a worse one for you though.  Since I was worried about my friend who is the author of the blog, I went to a website to check if TypePad was up or down.  The website is www.downrightnow.com.  I queried it about TypePad.  They said that TypePad was up -- possibly because the server was able to deliver a 404 HTTP error.  That was useless.

    Calling A MySQL Stored Procedure From The Command Line With Output Parameters



    I had a junior programmer ask me how to do stored procedure calls from the MySQL command line that had output parameters.  It had stumped him, and he had no other choice but to use the command line instead of a GUI tool, because he was working remotely on a Linux server.

    Here is how to do it.  Suppose that the stored procedure has two input parameters and two output parameters.  Here is the syntax:


    call doStoredProcedure('param1', 'param2', @t, @s);
    select @t, @s;

    The second line retrieves the output of the stored procedure.

    Hope this helps someone.

    Very Cool Gear and iOS app -Tile


    OK, I gotta admit that this is very cool.  It works on bluetooth and networking.  Unfortunately it doesn't work with my beloved Android.  More information is here:  http://www.thetileapp.com/