Combining Two Objects in Java 8

Say you have a simple POJO:

@Getter @Setter
public class Person {
    private Long id;
    private String name;
}

You extend that POJO for a specific use case:

@Getter @Setter
public class SpecificPerson extends Person {
    private String role;
}

If you have a list of both instances how do you combine them?

List<Person> people;
List<SpecificPerson> specificPeople;

If both list contain a similar subset of people were Person has part of the data and SpecificPerson has the other part of the data a way to combine the two would be through a BiFunction. (This, of course, is a assuming you have a common attribute, like Id).

public static BiFunction<Person, SpecificPersion, SpecificPerson> fromPersonToSpecific =(person, specificPerson) -> {
    specificPerson.setName(person.getName());
    return specificPerson;
}

Use this in a list:

final Map<Long, Person> mappedPeople = people
    .stream()
    .collect(Collectors.toMap(Person:getId, Function.identity()));

List<SpecificPerson> s = specificPeople
    .stream()
    .map(specific ->fromPersonToSpecific.apply(mappedPeople.get(specificPeople.getId()), specificPerson))
    .collect(Collectors.toList());

My First Atlassian Plugin

Goal

An Atlassian Macro that will query Stash and return a list of valid branches from a particular repo, with the option to filter.

Basically, this rest call:

stash/rest/api/1.0/projects/{project}/repos/{repo}/branches?filterText=release

Problem

Where I work we have a couple dozen micro-services that may or may not need to be updated during a particular release phase. In order to not miss an artifact I wanted to make a way to get automatic exposure to the required deployment.

Recently, I had configured each one of our projects to align with GitFlow (using JGitFlow).  The workflow looks something like this: A developer makes update to develop branch and when they determine their fix/feature is complete the cut a new release using the JGitFlow tools. The automatic build sees this new branch, creates and deploys to the test servers and then QA gets around to it when they have time.  One of the advantages of the Maven JGitFlow plugin is that it only allows for one release branch at a time. The idea being that if someone looks at a repo and see that it has a release branch, then it needs to be part of the next production release.

We use Confluence and Stash, the simple solution would be to have a macro ask for release branches and display them on our release page. After searching I found one potential candidate that would display information from Stash on a confluence page. There were two problem with this plugin: 1) Even though it would allow you to list branches, it would not allow you to filter branches, and 2) it cost money.

So, what’s a developer to do? Write my own plugin. This turned out to be more of a chore than I expected. I won’t delve into my–mostly–negative opinions about the Atlassian developer community or the Atlassian documentation, but what I will do is give a brief rundown of what I did and what I learned.

Getting Started

Skip the blah, blah and go straight to the code.

Whenever I read the words, “just install the SDK”, I cringe a little inside. It never seems to be that straight forward and the Atlassian SDK is no different. The problem for me is that I do regular Maven development and that gets in the way the Atlassian’s flavor of Maven that is built into their SDK. I would rant about how the Atlassian SDK could be greatly simplified by using a Gradle-like wrapper feature, but that’s not the point of this blog. I hope here to give a short concise example of creating an Atlassian plugin and clear up some ambiguity that I found along the way. I’ll also briefly touch on some solutions to problems I had.

Installing

To install, use the standard documentation. When you go to run commands skip any troubles you might have by explicitly pointing the the SDK settings instead of trying to incorporate the Atlassian settings with what is probably you corporate settings that are already in your ~/.m2/settings.xml file.

atlas-run --settings /usr/share/atlassian-plugin-sdk-6.2.6/apache-maven-3.2.1/conf/settings.xml

This is a problem with Maven not allowing mirror definitions in profiles.

Running

Running confluence is pretty straight forward, but you’ll probably want to add a few developer flags to make debugging easier.

atlas-run --settings /usr/share/atlassian-plugin-sdk-6.2.6/apache-maven-3.2.1/conf/settings.xml \
--http-port 1990 -Datlassian.dev.mode=true \
-Datlassian.webresource.disable.minification=true

After you have confluence up and running, you’ll inevitably do some configuration of the app. If you’re working with two apps you’ll inevitably create a link between the two. This can be a tedious process (watch out for case-sensitivity when defining url links). At about a day of when you first started the app it will complain about licensing. The way to fix this is to run an atlas-clean and rerun the app. However that will destroy the work that you’ve done. To get around this you need to create a snapshot of your configuration using the atlas-home command. It will create a zip directory that you can refer to in your pom.xml file.

&amp;lt;productDataPath&amp;gt;${project.basedir}/src/test/resources/generated-test-resources.zip&amp;lt;/productDataPath&amp;gt;

There are two things to remember here: 1) The app must be running when you call the atlas-run command, so open another window in the same directory, and 2) if you make changes or addition to how you app is setup, you must rerun the atlas-home command–it’s not automatic.

For developing a macro that talks from one app to another (Confluence to Stash), you have to have both apps running and connected. There is supposedly a way to define multiple apps in your pom.xml file and run concurrently but I kept having trouble with that so I just created two projects, one for the macro code and one just to keep the configuration of Stash.

Structure of a Macro

These are the parts to a macro

  • Plugin definition (atlassian-plugin.xml)
  • Macro definition (xhtml-macro inside of the plugin definition)
  • Macro code (which is run during the running of the macro)
    • com.atlassian.confluence.macro.Macro
  • JavaScript (used when rendering the macro editor)
    • optional, used if you want the macro editor to have dynamic behavior…so not really optional.
  • Template (velocity template for generating the resulting macro on the page.)

I’m not going to go into creating a Hello World macro. The standard doc is just fine for getting you up and running.

Using JavaScript to get Dynamic Behavior in Macro Editor

This is where the real power to create dynamic behavior is. For my macro I wanted to get a list of available projects, let the user select a project, then get a list of available repos in that project and let the user select a repo and finally store that information to be used later when rendering the macro on page. The basic features found in the xhmtl-macro tag don’t come anywhere close to this. Here is a couple things I learned. Here is the reference file.

  • AJS.MacroBrowser.setMacroJsOverride has a slew of undocumented methods that allow you to hook into the creation lifecycle, such as  beforeParamsSet, beforeParamsRetrieved, fields, etc
  • beforeParamsSet and beforeParamsRetrieved are important to saving parameters and making sure that your code does not overwrite previously selected values.
  • AJS.$ is just jQuery. When you are wondering how to dynamically change the macro browser, do not search for “how to edit confluence drop down”. Search for “how to edit a drop down with jQuery”.
  • Confluence will combine all the JavaScript and put it into one file. Find that file in the browser developer console, put a breakpoint in your code and explore the other methods available. There is intentionally no documentation for this stuff.

Most of the code speaks for itself. If you do the Hello World example, then take a look at my project, it will fill in some of the blank spots.

https://github.com/Scuilion/confluence-stash-macro

Oracle: No Data Found

ORA-01403: no data found

This is an error that happens in Oracle when you try and stuff nothing into a variable via a select statement.

SELECT id INTO tempvar FROM person WHERE name = 'Steve';

Most places will tell you to work with this problem through exceptions:

BEGIN
    SELECT id INTO tempvar FROM person WHERE first_name = 'Steve';
EXCEPTION WHEN NO_DATA_FOUND
THEN
    -- what to do when no data is found
END;

This seems strange to me, as this is not an exceptional case. You know there is a possibility that the data you want won’t be there. So why don’t you just check for it in the beginning.

I’d like to propose a more explicit solution to the problem.

SELECT count(*) into tempvar from person where first_name = 'Steve';
IF temp_variable &lt;&gt; 0
THEN
    --we're all good
    SELECT id INTO tempvar FROM person WHERE first_name = 'Steve';
END IF;

Exceptions break the flow of a program. Do you really want to break out of what you’re doing or do you want to handle the missing row?

Edit:
Several days after posting I came across a stackoverflow question regrading how to drop a table that may or may not exist. The first (and accepted) answer recommends catching the “table not found” exception, which is what I recommend against. The second answer, however,
makes a similar assertion about using a conditional.

Comment on that answer:

+1 This is better because do not relay on exception decoding to understand what to do. Code will be easier to mantain and understand – daitangio

Takeaways from UberConf 2014

As usual, the NFJS guys put on a very informative event. I figured I would write down my thought on the week before they get stale. This is my fifth event to attend but only my first UberConf. The added workshops was definitely the hook that got me to pester my boss to pay for this. It has to be a tough task to come up with a simple yet helpful/doable set a exercises, especially if the workshop is only half a day. Most of the workshops could have been twice as long; By the time the presenting part of the session was done there was little time to do actual work. (System setup was usually not handled in the best way.) In one class that I attended, the “workshop” title was just an excuse to talk longer. Fortunately, the long discussion was not bothersome. The following is a list in no order, just a data dump.

  • Neo4J – Six months ago I finished a small one-off project that would have benefited if I had used Neo4J. I wish that I had thought to use a graph database. Here in the next week I will rewrite that program. (Future post to follow on this.)
  • Apache Camel – This pops up just about everywhere. A month ago I consulted on how to setup an Apache ServiceMix project and several of the presenters were using Camel as part of their examples. I need to come up with a project, so I can get more hands on.
  • Continuous Delivery vs Continuous Deployment – This reminded me of the Shippable vs Saleable argument (or minimum viable product). My company’s product (at least the one I work on) is such a behemoth legacy app that even the discussion of Continuous Deployment is far down the road.
  • NGINX – came up in discussion several times. I don’t remember it being so popular before.
  • Clojure – I’ve deciding on which new language to delve into and plan on spending a good amount of time learning. For a while it was a toss up between Clojure and Scala. Clojure has officially won for me. The thought is that with Java 8 and it’s added functional features that I would go full on and learn Lisp as opposed to some middle ground. I’m using the following resources (along with the obvious Clojure references and cheat sheet maintained on clojure.org): Koans, exercism.io, and Seven Languages in Seven Weeks.
  • OSGi – For several months now I’ve been dealing with a rewrite of our OSGi implementation, which was fragile to begin with and is out-of-date (It’s stuck on Java 6 and buckminster). Talking with one of the presenters I found out that I’d missed OSGi DevCon by only a couple of weeks. This would have been a tremendous resource at figuring out the last few missing pieces. It also appears that the DevCon people didn’t put up any videos, which would have been helpful. Guess I find out what the slides give me.
  • Pomodoro Technique – On Wednesday the conference went late–up until 10. I decided that I would take a session that was less technical since I had been stuffing my brain all day. A friend of mine had encouraged me to try out the Pomodoro Technique. It seemed like a good idea but I didn’t read his blog post about it and eventually forgot. After debugging my way through this session, I decided I would give it a try. In fact, my first two pomodoros were done the next night after the conference and hit the mark for time. An abnormality that I chalk up to beginners luck, as my next 5 were staggeringly underestimated.
  • Docker – Another thing that the aforementioned friend put me onto. I’d mostly let it go without further research because I’m currently stuck on Windows at work. The session was enlightening and the flexibility of Docker was reiterated in a preceding workshop where environment setup could have been drastically reduced with the help of Docker.
  • Bintray – I was reminded about my stale account. Hopefully I’ll be using jcenter as soon as I get the Neo4J project going.

Books

  • Scalability Rules
  • The Design of Everyday Things – I’m halfway through this book. It’s my airplane book, and seeing as I don’t go on a lot of trips, it is taking a while for me to get through it.
  • Presentation Matters – This is co-authored by a couple of regular NFJS presenters. This is a soft skills topic that techy people seem to try and avoid, especially me. The last presentation I gave in front of my organization was about Java 8’s added annotations to type and repetitive annotations. Afterwards my boss commended me on my  presentation but suggested that I tell people who I am before jumping in.
  • How To Win Friends And Influence People – You hear about a certain book and after you hear about it a number of times, it just automatically gets put on your reading list.
  • Understanding Cryptography

Two soft skill books and two technical books added to my reading list. Plus two pages of bullet point task of personal work that I need to get done.

The Embedded Idea That Turned Into a Perl Script

My first purchase from SparkFun was a camera module (which they have since discontinued). The idea was to embedded it into a project.  When the device arrived, I thought, “Why don’t I just whip out a quick Perl module to test the device”. I already had the hardware to hook it to a computer and thought it would be an easy way to prove the device worked and find out what the quality of the photos would be. So I kludged together the hardware that I needed and got to writing some Perl.

The Hardware

  • USB to RS-232 convert (Cause, who really has a COMM port still on their computer)
  • RS232 to TTL
  • C328-7640

On my old computer I actually had RS232 ports due to all of the times I worked on projects that had debug serial signals but there are lots of options out there that are inexpensive and easy to use. Level converter, I have several lying around but again, choices, cheap, and easy. Now, you have to cut me some slack for the way the hardware looks, remember I was just trying for a quick proof.

The Hardware - Notice the Freescale eval board that is only being used for an easy, portable power supply(3.3V).

And even though it looks ‘spagatti-ish’, the zip tie and duck tape have held well and I haven’t had to scope the signals or debug connection issues.

The Software

It took about a couple of days to get my first full image. The module requires you to set the parameters (eg color type, resolution), set the package size, take the picture, ask for the picture (which returns the number of packages) and then read an acknowledge each data package. See the datasheet.

One of the First Successful Images - Full aperture/broad focal range and decent color fidelity.

After getting a good picture, I didn’t stop with the Perl. This pushed the work on well over the time I should have spent.  I kept saying things like, “let me make it easier to change the parameters on the fly”, “maybe I should make this more OO using the Moose module”, and so on so forth. So it took ~2-3 days to get an image and one year to move past.

The Video

I got the idea to take some time laps photography with this. So I set up my computer in the  shed about back and took a video from sun up to sun set.

Backyard Setup - Using the cinder block to hold the module adds to kludgyness of this project.

And here is the video. You can see there some data issues. Maybe in a year I’ll get around to debugging those.

Big Problem? Nope, It’s Small.

In an attempt to backup an Oracle 10g Express Edition using their handy Backup scripts that are automatically put in your start menu, I ran into the following error:

====================ERROR=============================
Backup of the database failed.
Unknown log mode : SP2-0640:.
Log file is at SP2-0640:.
====================ERROR=============================

In searching I found a post about changing the username password in the Batch file. This started me down the wrong road but he did make the sound suggestion of turning the echo on. There are about six places where the script stores log files in the %temp% directory and when I looked each one of these files read:

SP2-0640: Not connected

Turns out even when I tried to log on using two logins that I KNEW had administrator rights I could still not log in as a sysdba. The solution for a Windows user was to edit the Local Users and Groups and add the current user to the ORA_DBA group.

Side note: Another reason you might get this error is if your database is offline.

“Hello World!” Is Hopefully An Appropriate Introductory Post Name

As the default tagline suggested, this is, “Just another WordPress.com site”. Just another blog. For a long time I’ve held apprehensions to the idea of blogging.  Blogging in an arbitrary manner, that is. With regards to this I hope to keep the following informative and helpful–if only in the slightest.

Platitudes to Actions

On several occasion, when I’ve spent more time than I’ll admit, debugging a problem only to stumble on the solution buried deep in search engine results or stumble on by chance, I inevitably utter my own little platitude, “I should post this online so that it might help others in a similar situation. ” Yesterday, the preceding events happened again and today I resolved to take action.

This leads me to mention briefly  the subject of search-engine-searching. There is an art form in finding information, an art form that, for me, was only budding when I was in college. (If only I had the experience that I have now there would have been much alleviation of stress.) There are several things that impede me on my daily searching: Debug messages changing, solutions to problems getting old and falling back in the returned results…Just the other day I found a solution to a technical problem on a mostly religious blog of a Filipino denizen. How I got there, I couldn’t tell you.

And, hey, maybe one day I’ll post something uniquely helpful, too.

From CE to EE to CE

As the tagline alludes, my background is a Bachelor of Science in Electrical Engineering but the bread is brought to my table strictly through software. This recent shifting of gears is not something I depress over for two reasons. Firstly, I feel that the lack of attention paid to the art of programming in an Electircal curriculum is distressing. Now I can only speak for one school but the total sum of programming credits that I graduated with were somewhere around seven. (Maybe I should have opted for the VHDL class instead of MEMS to boost that number to ten.) The first project I took on as a Hardware Design Engineer was to create a piece of hardware that would be controlled over ethernet. The hardware was already designed by NXP so the bulk of the work turned out to be manipulating an embedded TCP/IP stack written by InterNiche. Next project, C# controlling hardware. Project after that, Perl controlling hardware. The realization was that even though I was doing EE work, the hardware I was dealing with was greatly affected by the lack of experience and core knowledge that I had in programming. The project I would have worked on if I continued my employment at that shop–embedded Linux. Secondly, when I decided to go to college it was software that drew me in. My dallying in high school was spent writing web sites and that is what I intended on doing with and education. It was due to a proclivity to the plastic engineering that lead to the degree I have. It probably helped that I paid my way through school working on PLC as a maintenance mechanic for one of the largest bakerys in the world.

Fortunately the fervour I feel for EE has subsisted, but spilt over into my home and currently litters the nightstand, the chest of drawers, the TV stand, etc, in the form of Maker type projects. The idea is that in an effort to keep the upcoming post versatile I’ll need to pull myself away from late night coding and solder things together. This pressure could lead to the completion of some physical projects!

So, here’s to doing!

Hello world!

Welcome to WordPress.com. After you read this, you should delete and write your own post, with a new title above. Or hit Add New on the left (of the admin dashboard) to start a fresh post.

Here are some suggestions for your first post.

  1. You can find new ideas for what to blog about by reading the Daily Post.
  2. Add PressThis to your browser. It creates a new blog post for you about any interesting  page you read on the web.
  3. Make some changes to this page, and then hit preview on the right. You can alway preview any post or edit you before you share it to the world.