Hibernate returning BigDecimal instead of Long

I was trying to run a query to get a list of IDs from a database table and then compare that list to individual Ids on the page. The logic was pretty straightforward. Get IDs via a query and then check if other IDs are found in that list of IDs. The query itself was working fine (I’ve reduced the complexity of the query a lot), but my contains statement wasn’t finding any matches (even though they were mostly all matches). It turns out that Hibernate was returning a List of BigDecimal rather than a list of Long and my comparison was a Long value. What I needed to do was force the column to return the datatype I wanted.

addScalar(columnName, dataType)

public List<Long> retrieveOrgTypeWithoutParent() {
 Session session = getHibernateTemplate().getSessionFactory().openSession();

 List<Long> orgTypeIds = session.createSQLQuery(
  "SELECT typeId FROM org_type_cd").addScalar("typeId", StandardBasicTypes.LONG).list();

 session.close();
 return orgTypeIds;
};

Mocking session in a Spock test for a java class

Spock is an excellent test framework that is built on top of Groovy and can be used to replace JUnit for testing Java classes. The majority of spock syntax is simple, but sometimes we run into issues where it is difficult to mock a class. Session is one of those examples. Given we want to do something simple, say get the name from Spring Security in session we’d have a method call like this

private String getUserId() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
return auth.getName();
}

To test this we need to mock session for this method and also for any method that calls this private method. To do so you need to mock the Authentication class and the SecurityContext class. Since this mocking is needed in multiple methods I’ve created a separate method in my spock class to mock the security context. The full test and method look like below

void "test getUserId"() {
given:
setSecurityContext()
when:
def response = service.getUserId()
then:
response == "hello"
}
def setSecurityContext() {
def authentication = Mock(Authentication)
authentication.getName() >> "hello"
def securityContext = Mock(SecurityContext)
securityContext.getAuthentication() >> authentication
SecurityContextHolder.setContext(securityContext)
}

Has anyone found an easier way to do this? Or is this the way it needs to be?

Moving a file from a local computer to a server through a jump server

This is a problem I’ve been routinely facing at work because we have new firewall rules and can only access new servers through a jump box. Accessing the server through ssh isn’t a problem after I’ve ssh’d into the jump server, but moving a file from my local computer to the other server is a pain. What you need to do is move the file from your local computer to an accesible directory on the jump server. For me this involves using WinSCP to drag and drop the file to my /home/mbusche directory.

Once the file has been moved to the jump server you need to use the scp command to move the file from the jump server to the other server. In my case the command looks something like this.

scp /home/mubusche/sonar.jar mbusche@cvms1255:/home/mbusche/

The syntax is scp fileLocationOnCurrentServer username@servername:folderLocationToMoveFile/

Now generally you won’t have write access to all folders under your username and may need to sudo in as another user to move the file to the directory you need. To do that you need to login to the destination server via ssh, sign in as a user with permissions and then move the folder

ssh serverName
sudo su - userWithAccess
sudo mv /home/mbusche/sonar.jar /webdata/plugins/

I’m speaking at dev.Objective() 2016

I’m happy to announce I’ve been selected to speak at dev.Objective() 2016! I will speaking on “Delivering Responsibly”.

Session Description
I’ll cover how to deliver fast, flexible and accessible websites. How you can deliver a consistent user experience to all users whether they’re on a small screen device or a 4k monitor. Whether they have a 2G connection or Google Fiber.

I’ll discuss some of the challenges we as developers face when trying to deliver responsibly and how to overcome some of those challenges. New specifications will make some of these challenges easier and I’ll cover how you can implement some of these features in non greenfield browsers.

I will discuss how http/2 mitigates some of the challenges we face, but how it doesn’t change as much as we might think when it comes to delivering responsibly.

Lastly I’ll cover the new Service Worker Specification and what it brings to the table.

Multiple insert statement without counter

I often have a need to write an INSERT statement that adds multiple rows to a database. Obviously you can run any  number of inserts separately, but that’s innefficient, so I loop over a list with a counter to know when to end. Recently I learned that the counter is unnecessary and you can create a fake SELECT statement to avoid needing a counter

<cfquery>
INSERT INTO Attendees (Schedule_ID, Attendee_ID, Meeting_ID, User_ID)
<cfloop list="#variables[eventlocation]#" index="i">
  SELECT #Schedule_ID#, #i#, #Meeting_ID#, 1
  UNION ALL
</cfloop>
<!--- returns no rows --->
SELECT 0, 0, 0, 0
WHERE 1 = 0
</cfquery>

For the sake of brevity I’ve avoided omitted the cfqueryparam’s that should always be used in queries.

I’m speaking at Iowa Code Camp Fall 2015

I’m excited to announce I will be speaking at Iowa Code Camp on December 5th in Ankeny, Iowa on Responsible Responsive Web Design.

I’ll be discussing how you can design a website responsibly and delivery a consistent user experience to users on small and large screen devices. I’ll also talk about how to decrease your website’s page size quickly and effectively to make your website load fast even on the slowest of connections. We’ll also cover using the newest HTML specifications even on browsers that do not have native support.

Opening/Editing Sharepoint documents in Chrome

If you’re like most people you don’t use Internet Explorer for any of your web browsing needs unless you’re required to. Thankfully most websites are compatible with most browsers and those instances are few and far between. If you use Sharepoint though, you know most activities function much better when you use Internet Explorer and some functionality is not available.

For example if you want to edit a document clicking the EDIT button gives you an error saying it cannot find the appropriate program to edit with. Thankfully I stumbled across an “edit in browser” option. When viewing a document click on the “…” which brings up a pop up. From there click on “…” again and you will see an Edit in Browser option. Not all functionality is available when you do this, but for most quick edits this will work excellent.

EditSharepointDocumentInChrome

Increasing heap size for a Grails application

I had been running into a lot of issues with Grails running out of heap space on my computer and finally found a solution. In the Grails command line documentation is mentions you can set a maximum heap space when you run grails through the command line. I would keep forgetting to set this and have to restart the app.

export GRAILS_OPTS="-XX:PermSize=128m -XX:MaxPermSize=512m -Xms256m -Xmx512m"
grails run-app

I knew that had to be an easier way, so I created an Environment Variable called GRAILS_OPTS and set the value to

-XX:PermSize=128m -XX:MaxPermSize=512m -Xms256m -Xmx512m -server

Now these settings are used anytime I run grails.

Fixing ESPN FantasyCast being broken in Chrome

This is the 2nd year in a row that ESPN has had a bug in their Fantasy Cast website so it does not render properly in Chrome. If your page looks like this, it’s broken. The good news is it’s easy to fix with a simple bookmarklet.

 

fantasy

Simply create a bookmark with this as the url

javascript:(function(){$('#real .progame-list').css("width", "101%");})()

and click on it to fix the page. Once clicked the page will look like this, which is much more user friendly.

ESPNFantasyCastChromeFixed