<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sajith Dilshan on Medium]]></title>
        <description><![CDATA[Stories by Sajith Dilshan on Medium]]></description>
        <link>https://medium.com/@sajithdilshan?source=rss-831e3f2ebc88------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uFi7n3bRN4qKB9NFMUWNpg.jpeg</url>
            <title>Stories by Sajith Dilshan on Medium</title>
            <link>https://medium.com/@sajithdilshan?source=rss-831e3f2ebc88------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 15:29:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sajithdilshan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ReactJS for Noobs II — Flux]]></title>
            <link>https://codeburst.io/reactjs-for-noobs-ii-flux-5355adb33dad?source=rss-831e3f2ebc88------2</link>
            <guid isPermaLink="false">https://medium.com/p/5355adb33dad</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[flux]]></category>
            <category><![CDATA[react-js-tutorials]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <dc:creator><![CDATA[Sajith Dilshan]]></dc:creator>
            <pubDate>Sat, 27 Jan 2018 20:57:18 GMT</pubDate>
            <atom:updated>2018-01-29T12:42:23.821Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jj89_VCZmU-3ZQA9YuvOBQ.png" /><figcaption>Photo by <a href="https://unsplash.com/photos/_QoAuZGAoPY?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Denys Nevozhai</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This post is a continuation of ReactJS for Noobs post and if you haven’t read it already, <a href="https://codeburst.io/reactjs-for-noobs-c374d79250e1">first go through it</a> before continuing with this article. ReactJS is a library to manipulate the web page DOM and when building a complex web app, it is paramount to manage the state of the application and most of the time React components want to talk with each other.</p><p>If all the components start to talk with each other, the whole app will be a mess and debugging an issue would be a nightmare. Hence, Flux can be used to make our lives easier. Flux is a data unidirectional architecture pattern. It is mostly a concept, rather than an implementation. First, let’s get a good conceptual understanding of what flux is and later on move on to an example implementation to get hands-on experience.</p><h4>Flux in a Nutshell</h4><p>Flux architecture pattern mainly consists of four components<br>1. View<br>2. Action<br>3. Dispatcher<br>4. Store</p><p>Let’s start with the <strong>View</strong>. The view is the user-visible layer which consists of React Components. Users can interact with React components since a component is mostly an HTML segment rendered on the web page. When a user interacts with a component an action can be invoked, such as showing a pop-up message when a button is clicked, and changing the color of a DIV element on mouse hover and so on.</p><p>This moves to the next component, <strong>Action</strong>. In the above-mentioned example, a user clicking on the button is the event and showing a pop-up message is the action. There can be may predefined actions and for each event, a React component can invoke an action.</p><p>When an action is invoked, the <strong>dispatcher</strong> dispatches the action to the store. The dispatcher is responsible for the wiring of React components and Stores via actions. That means whenever a component invokes an action, the dispatcher will dispatch the action to all the registered stores.</p><p>Next component is the <strong>Store</strong>, which is just an object extended from event emitter which acts as a data store. This data store keeps the state of the complete application or the state of a part of the application. When an action is dispatched to the store, the store can change its state based on the action. As an example when an action to change the color of a DIV is dispatched, the store can change its current state to reflect the new color for the DIV.</p><p>Now comes the last part. When a store changes its state, how can the React components update their component states so that ultimately the change is reflected on the web page? This is done by event listeners. A react component can register an event listener for a particular store and when the store changes its state, it will emit an event. The React Component which is listening to these events will be notified and no the component can query the new state of the store and update its component state. Below diagram summarizes connection between all these components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1_EEJam1c6mSpThsNMofTA.png" /><figcaption>Figure 1: Flux architecture diagram</figcaption></figure><h4>Flux in Action</h4><p>In this hands on tutorial we are going to build a basic React app with two components as shown in figure 2 and see how these two components can communicate with each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*QCDlcyJg8ONUDruU8OyqvA.png" /><figcaption>Figure 2: Components in the app</figcaption></figure><p>First, let’s create a react app by <a href="https://github.com/facebook/create-react-app">executing below command</a></p><blockquote>create-react-app flux-example</blockquote><p>Next, let’s add flux dependency to our app by executing below command</p><blockquote>npm install --save flux</blockquote><p>For this example app, apart from the boilerplate code we need two components. First let’s create a directory called <strong>components</strong> (in <strong>src</strong> directory) and within that create a file named <strong>ButtonComponent.js</strong> with below content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8ebc5c7d2345969dd75a63e043d7bb4/href">https://medium.com/media/d8ebc5c7d2345969dd75a63e043d7bb4/href</a></iframe><p>As you can see the component will render two buttons with Red and Blue labels. Next within the same directory, create a <strong>ColorComponent.js</strong> file with below content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f16b6200f5df7417eee089a856aaa09/href">https://medium.com/media/4f16b6200f5df7417eee089a856aaa09/href</a></iframe><p>As you can see, the component just renders a DIV with <strong>lightgrey</strong> background color. Next modify the content of <strong>App.js</strong> file as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad86d89d8c6be0ba8073a03b3bce357d/href">https://medium.com/media/ad86d89d8c6be0ba8073a03b3bce357d/href</a></iframe><p>Further, after adding below styles to <strong>index.css</strong> file and executing <strong><em>npm start</em></strong> and opening <a href="http://localhost:3000/">http://localhost:3000/</a> in your browser, you should see the content of the app which is similar to figure 2.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccc20b9ac91c5e2b470f83bf193def75/href">https://medium.com/media/ccc20b9ac91c5e2b470f83bf193def75/href</a></iframe><p>Although we have the skeleton of our app configured, clicking on the buttons will not make any change to our color component. To address that, first create a <strong>Dispatcher.js</strong> file in the <strong>src</strong> directory with below content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3dccacf8eb0ed86eb550e420f9143d5/href">https://medium.com/media/f3dccacf8eb0ed86eb550e420f9143d5/href</a></iframe><p>This is the dispatcher for our app which will dispatch actions invoked in components to the registered stores. Now when the user clicks on a button we need an action to be invoked. For that create a directory named <strong>actions</strong> (within <strong>src</strong> directory) and within that create a file named <strong>ColorAppActions.js</strong> with below content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51dfea47d4e7a19b0ec8c0a88eb2cbd9/href">https://medium.com/media/51dfea47d4e7a19b0ec8c0a88eb2cbd9/href</a></iframe><p>You can create an action file for each component or only one file for your complete app. But it would be easier to maintain the app by keeping a separate action file for each main component. In the above code as you can see <strong>changeColor(colorName) </strong>function is exposed, so that our <strong>ColorComponent</strong> can use it to notify the change of color. Within that function we invoke the <strong>dispatch()</strong> method of the dispatcher with an object with two keys as a method argument. The <strong>type</strong> represents a unique name (throughout the app) to distinguish the action and <strong>value</strong> contains the additional information associated with the action. You can include any number of additional key-value pairs in this object. Next we need to update our <strong>ButtonComponent</strong> as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29d3e1fcf94ab6f9f7f895ffa164be59/href">https://medium.com/media/29d3e1fcf94ab6f9f7f895ffa164be59/href</a></iframe><p>When user clicks on a button we have configured the component to invoke <strong>onButtonClick</strong> method through <strong>onClick</strong> <a href="https://reactjs.org/docs/events.html">synthetic event</a>. Further, for each invocation we pass the name of the color as a method argument. Within the <strong>onButtonClick </strong>method, we invoke our <strong>changeColor()</strong> action.</p><p>Next, let’s move on to the store. Create a directory named <strong>stores </strong>(Within <strong>src</strong> directory) and create a file named <strong>ColorAppStore.js</strong> within that directory with below content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4a47242bd975a0ad717a027bd9f61d7/href">https://medium.com/media/e4a47242bd975a0ad717a027bd9f61d7/href</a></iframe><p>This is the data store of our App. As you can see it is extended from <strong>EventEmitter</strong> because once the data in the store changes, the store will emit events so that subscribed components can update their respective states. In this store we have only one data at the moment, i.e. <strong>activeColor</strong> which represent the current background color of the <strong>ColorComponent</strong>.</p><blockquote>const colorAppStore = new ColorAppStore();</blockquote><blockquote>dispatcher.register(colorAppStore.handleActions.bind(colorAppStore));</blockquote><p>As for any store, above two lines are salient. First one creates a new instance of the store and second one registers the store with the dispatcher through a handler function so that whenever the dispatcher fetches an action, the handler function of the store will be invoked with respective action as a method argument to the handler function. In this example, the <strong>handleActions()</strong> method in the store will be invoked for any action invocation.</p><p>Within the <strong>handleActions()</strong> method, the store can query for the identifier of the action (<strong>type</strong>) and if the action is relevant to the store then the store can act upon it or if that is not the case, simply ignore the action altogether. In our case, for <strong><em>COLOR_APP_ACTIONS.CHANGE_COLOR</em></strong> we want the store to modify its <strong>activeColor</strong><em> </em>accordingly and emit an event to notify the components that a change in the store has occurred. This is accomplished through below code.</p><blockquote>this.activeColor = action.value;</blockquote><blockquote>this.emit(“storeUpdated”);</blockquote><p>Next we need to update our <strong>ColorComponent</strong> as shown below to catch events emitted by the store.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b626338f52d543c9cae5f2b3aee5b16/href">https://medium.com/media/4b626338f52d543c9cae5f2b3aee5b16/href</a></iframe><p>First change in the component is that in the constructor for the initial state we obtain the <strong>color</strong> from the store so that we would have a single source for the background color. Next, within the <a href="https://reactjs.org/docs/react-component.html#componentdidmount"><em>componentDidMount()</em></a> lifecycle method, the component is subscribing to <strong><em>storeUpdated</em></strong> events emitted from the <strong>ColorAppStore</strong>. It is mandatory to remove the listener in <a href="https://reactjs.org/docs/react-component.html#componentwillunmount"><em>componentWillUnmount()</em></a><em> </em>method. Otherwise the store subscription will cause a memory leak in your app. As per the above code, when the <strong>ColorAppStore</strong> emits <strong><em>storeUpdated</em></strong> event, the <strong>updateBackgroundColor()</strong> method in the component will be invoked and within that method, we update the current state of the component to reflect the change in the store.</p><p>Now if you run the app and click on the buttons you will see the background color of the <strong>ColorComponent</strong> will change as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Gr7PM2r1h0uQhVNY6rHBXQ.gif" /></figure><p>In a nutshell, when the user clicks on a button, an action will be invoked by <strong>ButtonComponent</strong> with respective color as data and the dispatcher will fetch this action to the <strong>ColorAppStore</strong>. Next, the store will extract the color information from the action and update its store data and emit a <strong><em>storeUpdated</em></strong> event. The <strong>ColorComponent</strong> which is listening to the <strong><em>storeUpdated</em></strong> events from the store will receive the emitted event and the component will update its internal state to match with the data in the <strong>ColorAppStore</strong>.</p><p>As you can see, the store acts as the single source of truth and the components talk with each other through manipulating the data within the store. This example demonstrates the basic usage of pure flux and there are better implementations of flux which abstracts out the boilerplate code to increase the developer efficiency. One such implementation is <a href="https://redux.js.org/">Redux</a> and I will walk you through the basics of Redux in the next article.</p><p>The complete source code of this example app is available at <a href="https://github.com/sajithdilshan/flux-example">https://github.com/sajithdilshan/flux-example</a></p><h3>Call To Action</h3><ul><li><strong>Clap.</strong> Appreciate and let others find this article.</li><li><strong>Comment.</strong> Share your views on this article.</li><li><strong>Follow me. </strong><a href="https://medium.com/@sajithdilshan">Sajith Dilshan</a> to receive updates on articles like this.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5355adb33dad" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/reactjs-for-noobs-ii-flux-5355adb33dad">ReactJS for Noobs II — Flux</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Crunching S3 server access logs with Hadoop]]></title>
            <link>https://medium.com/@sajithdilshan/crunching-s3-server-access-logs-with-hadoop-972590dd3fc7?source=rss-831e3f2ebc88------2</link>
            <guid isPermaLink="false">https://medium.com/p/972590dd3fc7</guid>
            <category><![CDATA[hadoop]]></category>
            <category><![CDATA[ultrastudio]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[s3]]></category>
            <category><![CDATA[mapreduce]]></category>
            <dc:creator><![CDATA[Sajith Dilshan]]></dc:creator>
            <pubDate>Tue, 21 Nov 2017 08:56:19 GMT</pubDate>
            <atom:updated>2017-11-21T08:56:19.974Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sbb09O10st0c6zEoAAWr_g.png" /><figcaption>Photo by <a href="https://unsplash.com/photos/hpjSkU2UYSU?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Carlos Muza</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Nowadays, most of the static web pages and single page web apps are hosted in AmazonS3 buckets and Amazon provides inbuilt functionality to gather server <a href="http://docs.aws.amazon.com/AmazonS3/latest/user-guide/server-access-logging.html">access logs for buckets</a> which record user-agent, remote IP, and <a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/LogFormat.html">many more details</a>. These data can be analyzed in order to identify user visit patterns and highly visited parts of the website which could be useful for marketing. Without any further ado, let’s see how we can crunch these data with Hadoop.</p><p>First, we need to copy the server access logs into <a href="https://hadoop.apache.org/docs/r1.2.1/hdfs_design.html">HDFS</a>. For this let’s use <a href="https://www.adroitlogic.com/products/ultrastudio/">UltraStudio</a> and create a flow to pull server logs from an S3 bucket and copy those logs into HDFS. If you are not familiar with UltraStudio, you can go through this <a href="https://www.youtube.com/watch?v=aXfKjqXjqA0">video tutorial</a> to get an overall idea about it. Let’s first create an <a href="https://developer.adroitlogic.com/ultrastudio/docs/17.07/project_creation.html#creating-an-empty-project">empty project</a> by selecting <a href="https://developer.adroitlogic.com/connectors/docs/17.07/aws/s3_ingress_connector.html">AWS connector</a> and <a href="https://developer.adroitlogic.com/connectors/docs/17.07/hdfs/hdfs_egress_connector.html">HDFS Connector</a> and within that project, we need to <a href="https://developer.adroitlogic.com/ultrastudio/docs/17.07/creating_running_flow.html#creating-the-hello-world-flow">create an integration flow named MainFlow</a>.<br>After that let’s add an S3 ingress connector to the design pane and configure it as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*10kFbqVrJfS4LES6MUXT1A.png" /><figcaption>Figure 1: S3 Ingress Connector Configuration</figcaption></figure><p>In the above configuration, specify the <strong>region</strong> of the S3 bucket and specify the <a href="https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/"><strong>Access Key</strong> and <strong>Secret</strong></a> to pull the records from the <strong>specified bucket</strong>.</p><p>After that, add an HDFS Egress Connector specifying the <strong>hostname</strong> and <strong>port</strong> of the HDFS node as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_B4WvaGZf2dtMOj5YUNTKA.png" /><figcaption>Figure 2: HDFS Egress Connector Configuration</figcaption></figure><p>Next, let’s connect an <a href="https://developer.adroitlogic.com/connectors/docs/17.07/hdfs/hdfs_egress_connector.html#hdfs-payload-saver">HDFS Payload Saver connector operation</a> to the egress connector and specify its properties as shown below. We are going to save the log files into /s3logs directory in HDFS with the same file name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U4gj78gPeLy53FKFvZwPvQ.png" /><figcaption>Figure 3: HDFS Payload Saver Connector Operation Configuration</figcaption></figure><p>After that, connect all the components as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*yp6_ASLqBT5K6p9DAbZV7Q.png" /><figcaption>Figure 4: Complete Integration Flow</figcaption></figure><p>Now after <a href="https://developer.adroitlogic.com/ultrastudio/docs/17.07/creating_running_flow.html#running-the-project-with-flows">running the integration flow</a>, all the files in the specified S3 bucket will be copied to /s3logs directory in HDFS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XGZwN06bBbtlKW1VMYKukw.png" /><figcaption>Figure 5: HDFS File system</figcaption></figure><p>Now let’s execute our map reduce job to find the visit count for each resource path. A particular server log entry would be as below.</p><blockquote>83634ad2474abbbc5e3c1fa2cd1cef87b2bb814fd9accf63c7a85aa5a1acc2 beta.as2gateway.org [20/Nov/2017:07:31:29 +0000] 13.228.69.75 — BDB9C04019087152 REST.GET.OBJECT partner “GET /partner HTTP/1.1” 404 NoSuchKey 278–12 — “-” “Amazon CloudFront” -</blockquote><p>As you can see, the resource path is under <strong><em>“GET /partner HTTP/1.1”</em></strong> section. Hence, we can write our mapper class as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1291d82d849de55d586e87cf6d0d0c3c/href">https://medium.com/media/1291d82d849de55d586e87cf6d0d0c3c/href</a></iframe><p>For each line, we are going to evaluate the regular expression which captures the resource path within quotation marks and add it to the context. Next, in the reducer class, we need to calculate the total occurrence of each resource path as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b62fd7121b4262e5949cf3adaa41610a/href">https://medium.com/media/b62fd7121b4262e5949cf3adaa41610a/href</a></iframe><p>The main method to run the job is shown below. Make sure to specify the correct HDFS URL for configuration and the result path to store the result file of the job.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5aa450d5f099c938e435e62307d0950a/href">https://medium.com/media/5aa450d5f099c938e435e62307d0950a/href</a></iframe><p>The complete code of the map-reduce class is available on <a href="https://github.com/sajithdilshan/access-log-cruncher">Github</a>. After running our job, the result will be available on the specified result path in below format.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0d958d3d1fcb9979eba59f1ba7d4045/href">https://medium.com/media/c0d958d3d1fcb9979eba59f1ba7d4045/href</a></iframe><p>Likewise, we can write custom jobs to crunch remote IP details, visitor frequency and many other useful information.</p><h3>Call To Action</h3><ul><li><strong>Clap.</strong> Appreciate and let others find this article.</li><li><strong>Comment.</strong> Share your views on this article.</li><li><strong>Follow me. </strong><a href="https://medium.com/@sajithdilshan">Sajith Dilshan</a> to receive updates on articles like this.</li><li><strong>Keep in touch. </strong><a href="https://www.linkedin.com/in/sajithdilshan/">LinkedIn</a>, <a href="https://twitter.com/sajithdilshan">Twitter</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=972590dd3fc7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ReactJS for Noobs]]></title>
            <link>https://codeburst.io/reactjs-for-noobs-c374d79250e1?source=rss-831e3f2ebc88------2</link>
            <guid isPermaLink="false">https://medium.com/p/c374d79250e1</guid>
            <category><![CDATA[react-js-tutorials]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[learn-react-js]]></category>
            <dc:creator><![CDATA[Sajith Dilshan]]></dc:creator>
            <pubDate>Sun, 19 Nov 2017 14:08:33 GMT</pubDate>
            <atom:updated>2017-12-04T13:42:45.745Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3SrhT42nL9Sprx6mh6sGnA.png" /></figure><p>ReactJS has been popular among web app developers recently and <a href="https://github.com/facebook/react/wiki/sites-using-react">a lot of websites has adopted</a> the library because of the perks it provides. This post is not about pros and cons of ReactJS library, although you can find a myriad of articles and posts on the web comparing ReactJS with other front-end frameworks.</p><p>In layman’s term, ReactJS is a javascript library which handles the View component of the <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC architecture</a>. ReactJS is used to manipulate the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction">DOM</a> of a webpage in an efficient manner to render a web application.</p><h3>Creating Hello World App</h3><p>First, let’s create our sample React app. For this tutorial, you need <a href="https://www.npmjs.com/get-npm">Node and NPM to be installed in your environment</a>. After that, execute the following command to install the react boilerplate app creator.</p><blockquote>npm install -g create-react-app</blockquote><p>Next, we can create our hello-world app using below command.</p><blockquote>create-react-app hello-world</blockquote><p>Now if we look at the file structure within the hello-world app, there are three important files. Those are <strong>public/index.html</strong>, <strong>src/index.js</strong> and <strong>src/App.js</strong> files. In the index.html file you can see that there is a div with id <em>root</em> and in the index.js file below code segment.</p><p><strong><em>ReactDOM.render(&lt;App /&gt;, document.getElementById(‘root’));</em></strong></p><p>This is the starting point of our app. Basically, react will inject out App component into the div with <em>root</em> id. Now you might be wondering, what is App component. If you look at the App.js file, you will be able to see the App component. In React it is the convention to name the Javascript file with the same name as the component.</p><h3>React Component</h3><p>Now let’s see what is a React component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f93d422570f83870944553cb3dee0fcc/href">https://medium.com/media/f93d422570f83870944553cb3dee0fcc/href</a></iframe><p>Above is the simplest component we can write on React. We create a JavaScript class for our component and extend it from Component class from React library (Note the import statement). After that, we need to implement the <strong>render()</strong> method. Prior to React 16, it was a must to return a single element from render method. But after React 16, we can <a href="https://reactjs.org/blog/2017/09/26/react-v16.0.html#new-render-return-types-fragments-and-strings">return an array of elements</a>. Now if we execute <strong><em>npm start</em></strong> command within the hello-world directory, we can see that Hello world is rendered in <a href="http://localhost:3000/"><strong><em>http://localhost:3000</em></strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V5oiqoD5q8naZipWjrpFbw.png" /><figcaption>Figure1: Hello World</figcaption></figure><h3>React JSX (Syntactic sugar for JS)</h3><p>In the render method of the component, we can almost use the HTML syntax. But in reality, this is actually JavaScript, specifically known as JSX. JSX is a template language which has a similar structure to HTML with the full power of JavaScript. Hence, you can evaluate any JavaScript statement within the render method.</p><p>As an example the JSX code we wrote earlier (shown below).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c00bb9963162cd90d2c2625ed9f5ded/href">https://medium.com/media/9c00bb9963162cd90d2c2625ed9f5ded/href</a></iframe><p>will be compiled into the JavaScript code shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13dd21c271ab4b8a238532daf045cfee/href">https://medium.com/media/13dd21c271ab4b8a238532daf045cfee/href</a></iframe><p>In reality, JSX was introduced to make developer’s life easier by providing a <a href="https://facebook.github.io/react/docs/introducing-jsx.html">familiar syntactic sugar</a>.</p><h3>State vs. Props</h3><p>Now let’s create another component named Alice under <strong>src/Alice.js</strong> file. The content of the Alice component would be as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5743a99c8c45069d1d861d51923f05eb/href">https://medium.com/media/5743a99c8c45069d1d861d51923f05eb/href</a></iframe><p>Now we can include this Alice component as a child component in our App component as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a89bcea1a0fe1f750e2e49b0f4771609/href">https://medium.com/media/a89bcea1a0fe1f750e2e49b0f4771609/href</a></iframe><p>Note the new import statement for the Alice component. Without that our app will break since React is unable to find the component which corresponds to the Alice tag.</p><p>Next, let’s see how we can manipulate the state of our components. For each React component, there is a state and by manipulating the state object we can change the behavior of our component. This state object is specific to the component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a6d889d9488790f13149ffb627a7fbd/href">https://medium.com/media/1a6d889d9488790f13149ffb627a7fbd/href</a></iframe><p>As shown above, we can declare the state object within the constructor (Note that it is a must to call <strong><em>super()</em></strong> within the constructor of a React component). This state object contains <strong><em>greetings</em></strong> property with the value <strong><em>“Hello World!”</em></strong>. In the render method, we have used this property as <strong><em>{this.state.greeting}</em></strong>, and this is the usual way of evaluating JavaScript expressions within JSX. You can add as many properties you want to the state object. Now let’s see how we can pass data from the App component into its child component, Alice.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68b45305dd149f4c21e062f8ba1f0367/href">https://medium.com/media/68b45305dd149f4c21e062f8ba1f0367/href</a></iframe><p>In the modified App component(shown above), I have added another property named <strong><em>parentMessage</em></strong> and passed it to Alice component as shown below.</p><blockquote>&lt;Alice newMsg={this.state.parentMessage} /&gt;</blockquote><p>Note that <strong><em>newMsg</em></strong> is the key I’ve used to pass the data, for that, you can use any valid string. Now let’s look at the corresponding Alice Component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aeb2c433dad4bf7586993794476d03c/href">https://medium.com/media/2aeb2c433dad4bf7586993794476d03c/href</a></iframe><p>If you look at the constructor of the Alice component, there is a method argument named <strong><em>props</em></strong>. This object contains all the data passed from the parent component (Input from parent component to the child component). I have initialized the <strong><em>state</em></strong> of the Alice component using the <strong><em>newMsg</em></strong> property passed from the App component. If you have used another string as the key instead of <strong><em>newMsg</em></strong>, you can access it as same as <strong><em>props.newMsg</em></strong>.</p><p>Now the app should be rendered as shown below (Figure 2).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VVepVDDmOe-5_2C849bCHQ.png" /><figcaption>Figure 2: Modified App</figcaption></figure><p>Below table summarizes the basic differences between <strong>state</strong> and <strong>props</strong> objects of a component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zvytU5AY-XvknYg0LlSx7Q.png" /></figure><p>Another very important thing to note is that whenever we want to modify the <strong>state</strong> object, we do it through <strong><em>this.setState()</em></strong> method as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f74bb6313b57f3d85008b9bc6be66bd7/href">https://medium.com/media/f74bb6313b57f3d85008b9bc6be66bd7/href</a></iframe><p>In the constructor, I have added a timeout function which modifies the greeting message to <strong><em>“Hello World Update!”</em></strong> after 10 seconds. DO NOT use <strong><em>this.state.greeting = “Hello World Updated!”</em></strong> to update the state as it would not work since React uses an event driven approach rather than using dirty checking to maintain the state of React components.</p><h3>React Component Lifecycle</h3><p>React provides highly valuable lifecycle hooks (method) for each state of the component as listed below. These methods can be classified into 3 categories mainly, i.e. <strong>mounting</strong>, <strong>updating</strong> and <strong>unmounting.</strong></p><p>Mounting hooks are fired when the react component is initially being rendered in the browser page. Below methods are fired in the specified order.</p><ol><li><strong>constructor()</strong> - Invoked when creating the react component for the first time.</li><li><strong>componentWillMount()</strong> - Invoked before adding the component to the actual DOM of the web page.</li><li><strong>render()</strong> - Invoked when adding the component to the actual DOM.</li><li><strong>componentDidMount()</strong> - Invoked after adding the component to the actual DOM.</li></ol><p>Updating methods are invoked when the component is re-rendered on the web page. This happens due to changes in state/props objects. Below methods are invoked in the specified order.</p><ol><li><strong>componentWillReceiveProps(newProps)</strong> - This method is invoked when new props are received from parent component. Note that this method will not be invoked at the initial time when the component is created even though parent component passes props. The initial time should be handled within the constructor.</li><li><strong>shouldComponentUpdate(nextProps, nextState)</strong> - This method is invoked before the component is re-rendered. It is a must to return a <strong>boolean</strong> from this method. If true is returned, the component will be re-rendered and the component will not be re-rendered if false is returned.</li><li><strong>componentWillUpdate()</strong> - This method is invoked before the component is re-rendered in the actual DOM</li><li><strong>render()</strong> - Invoked when the component is re-rendered in the actual DOM</li><li><strong>componentDidUpdate()</strong> - Invoked after the modified component is re-rendered on the actual DOM</li></ol><p>As for the unmounting, there is one hook named <strong>componentWillUnmount()</strong>. This method is invoked immediately before the component is removed from the actual DOM. Apart from that starting from React 16, there is another lifecycle hook named <strong>componentDidCatch()</strong> which will be invoked when there is an exception during any of the lifecycle hooks of the component. This method can be used to gracefully show an error message when a React component fails to function as expected. For more information on the component lifecycle events, take a look at the <a href="https://reactjs.org/docs/react-component.html#the-component-lifecycle">awesome react documentation</a>.</p><h3>Virtual DOM vs Real DOM</h3><p>When applying the changes of a React component to the actual web page, React uses a virtual DOM to calculate the changes and then apply only the modified bits into the real DOM. Since React completely builds the virtual DOM, you might be wondering why not build the actual DOM instead. Re-building the actual DOM is quite expensive since the browser needs to re-calculate all the CSS properties, re-draw the canvas and fire all the event listeners unnecessarily. Hence, building the virtual DOM and applying only the difference to the actual DOM is quite efficient and this process is known as <a href="https://reactjs.org/docs/reconciliation.html"><strong>reconciliation</strong></a>. React implements a <a href="https://reactjs.org/docs/reconciliation.html#tradeoffs">heuristic based algorithm</a> to calculate the difference between the virtual and actual DOMs which has a time complexity of O(n).</p><p>As an example when rendering a component, initially both DOMs are empty (Figure 3).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1hu6KYQe3vr6EAabdCe8kw.png" /><figcaption>Figure 3: Empty DOM</figcaption></figure><p>First the virtual DOM is populated with the content (Figure 4).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mDHaLU9_7c1oKggHjxR3vg.png" /><figcaption>Figure 4: Virtual DOM population</figcaption></figure><p>Next the real DOM is populated with the content and the virtual DOM is cleared (Figure 5).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o7iKz61MYRND5QAlXolTTg.png" /><figcaption>Figure 5: Actual DOM population</figcaption></figure><p>After that for any change, the complete virtual DOM is generated with the newly modified content (Figure 6).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W-1aRfqB35SvuMldLeH1bg.png" /><figcaption>Figure 6: Re-creation of Virtual DOM</figcaption></figure><p>Next the difference is calculated and only the modified section is updated in the real DOM and the virtual DOM will be cleared (Figure 7).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P5K1bzbOuX4ANsXSO6dAFA.png" /><figcaption>Figure 7: Updating real DOM</figcaption></figure><p>This sums up the basic introduction to the ReactJS and it is time for you to get your hands dirty and start building your first react app to get a first hand experience. In the next blog post let’s see how we can craft an elegant data flow between React components using <a href="https://facebook.github.io/flux/docs/overview.html">Flux</a>. Until then, happy coding!</p><h3>Call To Action</h3><ul><li><strong>Clap.</strong> Appreciate and let others find this article.</li><li><strong>Comment.</strong> Share your views on this article.</li><li><strong>Follow me. </strong><a href="https://medium.com/@sajithdilshan">Sajith Dilshan</a> to receive updates on articles like this.</li><li><strong>Keep in touch. </strong><a href="https://www.linkedin.com/in/sajithdilshan/">LinkedIn</a>, <a href="https://twitter.com/sajithdilshan">Twitter</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c374d79250e1" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/reactjs-for-noobs-c374d79250e1">ReactJS for Noobs</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JMS Transaction propagation over multiple threads using Spring]]></title>
            <link>https://medium.com/@sajithdilshan/https-medium-com-sajithdilshan-jms-transaction-propagation-over-multiple-threads-with-spring-d2d58be82002?source=rss-831e3f2ebc88------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2d58be82002</guid>
            <category><![CDATA[transactions]]></category>
            <category><![CDATA[ultraesb]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[jms]]></category>
            <category><![CDATA[ultrastudio]]></category>
            <dc:creator><![CDATA[Sajith Dilshan]]></dc:creator>
            <pubDate>Wed, 15 Nov 2017 10:39:48 GMT</pubDate>
            <atom:updated>2017-11-15T10:43:22.497Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O1JljIhvwrCs0IzC7hUhZQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/zIwAchjDirM?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Dan Counsell</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><a href="http://download.oracle.com/otndocs/jcp/jms-2_0-fr-eval-spec/">Java Messaging Service (JMS)</a> is an API which is capable of creating, sending and reading messages. JMS provides loosely coupled, reliable and asynchronous communication functionality. Hence, JMS is used in enterprise applications to integrate multiple systems together.</p><p>When it comes to integration, it is paramount to guarantee the reliability of the overall solution as to not lose any message which is passing through the solution. This is where the JMS transactionality comes in handy where you can read a message transactionally from a JMS queue and after performing all the required logic on the message you can either commit or rollback the message. In the case of committing the message, it will be removed from the source queue and in the case of rollback, the message will remain in the JMS queue for a successive re-try attempt.</p><p>In order to connect to a JMS queue, we need a JMS connection factory and the Spring binds the transaction of the message to the connection factory. Hence, we cannot span a single transaction among multiple JMS queues which are not connected via the same connection factory. Without any further ado, let’s dig into the code.</p><p>First, a Java class with the required thread local as shown below is required. If you are not familiar with thread locals, the best starting point would be the <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html">Java documentation of thread locals</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcb6f896a04670a7f2cb7fa5084f5b85/href">https://medium.com/media/dcb6f896a04670a7f2cb7fa5084f5b85/href</a></iframe><p>The <strong><em>txnStatus</em></strong> thread local is used to store the current status of the transaction. If we evaluate the thread local value during runtime, we will be able to see the content of the TransactionStatus object’s content as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BXqkqZp4_7_qClrffJtspw.png" /><figcaption>Figure1: Evaluation result of <strong><em>txnStatus</em></strong> thread local.</figcaption></figure><p>In order for a transaction to be initiated, we need to wrap the connection factory with the Spring <strong><em>JMSTransactionManager</em></strong>. Further, we need to initialize the <strong><em>txnStatus</em></strong> thread local in the <strong><em>doReceiveAndExecute()</em></strong> method within <strong><em>org.springframework.jms.listener.SpringMessageListenerContainer</em></strong> class. Sample implementation would be as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5743d37640e860c5954dfba49abb6f9/href">https://medium.com/media/c5743d37640e860c5954dfba49abb6f9/href</a></iframe><p>After that, we need to copy the thread locals from one thread to another. This is the tricky part. Of course, we can use reflection and directly copy the thread locals but in this case, we can use the JMS message as a context object and attach the thread locals to the JMS message from the initial IO thread. All the important thread locals which need to be copied resides within <strong><em>org.springframework.transaction.support.TransactionSynchronizationManager</em></strong> class and the <strong><em>resources</em></strong> thread local in that class plays an important role here. The evaluated value of that during the runtime would be as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1dHVfFLoUTY8k8PN7ovA8Q.png" /><figcaption>Figure2: Evaluation result of <strong><em>resources</em></strong> thread local.</figcaption></figure><p>As you can see, this is a map and the key is our connection factory and the value is a resource holder which includes the JMS session information. Using below code segment, we can obtain the resource map and clear the thread local from the current thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c138935f817a45a49639dfc6f9fa3cc5/href">https://medium.com/media/c138935f817a45a49639dfc6f9fa3cc5/href</a></iframe><p>The <strong><em>savedResources</em></strong> variable can be attached as a property to the JMS message and later when the message is being processed by a new thread, we can obtain this resource and set it as the thread local value for the new thread by invoking the <strong><em>bindResource()</em></strong> method of <strong><em>TransactionSynchronizationManager</em></strong> class for each key, value pair in the <strong><em>savedResources</em></strong> map. After that, the new thread will be a part of the initial JMS transaction.</p><p>One salient point to remember is that it is vital to clear the thread locals at the end of the thread execution time if we are re-using the threads. As an example, when using a thread pool, it is mandatory to clean the thread locals before returning them to the pool. Otherwise, it would result in a thread local leak and unexpected behavior in JMS transactionality.</p><p>One of the biggest advantages of propagating a JMS transaction over multiple threads is that it will optimize the usage of IO threads which initially polls the message for a JMS queue. Usually, IO thread has to execute all the logic related to JMS message until it is committed or rolled back. If the intermediate logic involves time-consuming computations, it will negatively affect the message throughput of the system. By propagating the transaction, we delegate the computation logic to a new thread while preserving the transactionality of the message flow.</p><p>It seems like a lot to digest at first, but the good thing is that AdroitLogic’s <a href="https://developer.adroitlogic.com/project-x/docs/17.07/">Project-X framework</a> provides all of this functionality out of the box and you can try it out by yourself using <a href="https://www.adroitlogic.com/products/ultrastudio/">UltraStudio</a> without writing a single line of code. There is a sample project which explains the <a href="https://developer.adroitlogic.com/samples/docs/17.07/advanced/jms_transactions.html">JMS transactionality</a> and you can <a href="https://developer.adroitlogic.com/ultrastudio/docs/17.07/project_creation.html#creating-a-sample-project">create</a> and <a href="https://developer.adroitlogic.com/ultrastudio/docs/17.07/creating_running_flow.html#running-the-project-with-flows">run</a> the project directly within UltraStudio.</p><h3>Call To Action</h3><ul><li><strong>Clap.</strong> Appreciate and let others find this article.</li><li><strong>Comment.</strong> Share your views on this article.</li><li><strong>Follow me. </strong><a href="https://medium.com/@sajithdilshan">Sajith Dilshan</a> to receive updates on articles like this.</li><li><strong>Keep in touch. </strong><a href="https://www.linkedin.com/in/sajithdilshan/">LinkedIn</a>, <a href="https://twitter.com/sajithdilshan">Twitter</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2d58be82002" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Looking for a sports watch? Never buy an Apple watch]]></title>
            <link>https://medium.com/@sajithdilshan/looking-for-a-sports-watch-never-buy-an-apple-watch-d65008fcb6da?source=rss-831e3f2ebc88------2</link>
            <guid isPermaLink="false">https://medium.com/p/d65008fcb6da</guid>
            <category><![CDATA[apple-watch]]></category>
            <category><![CDATA[sportswear]]></category>
            <category><![CDATA[apple-watch-sport]]></category>
            <dc:creator><![CDATA[Sajith Dilshan]]></dc:creator>
            <pubDate>Sat, 30 Sep 2017 11:30:31 GMT</pubDate>
            <atom:updated>2017-09-30T11:31:36.550Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7muAxqMoz_Q7WP2biKA9dg.png" /></figure><p>Dear Apple,</p><p>Couple of weeks ago I bought a Series 2, 38mm Apple watch from Singapore through a friend of mine. Don’t believe me? Here’s the receipt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B0Cp8a5BpLmQfgC538icYA.png" /></figure><p>Needless to say I was so excited to use the watch, and at first glance it was truly a beauty. I wanted to wear this watch while I was at the gym, while playing badminton, while I was swimming and even during my evening walks.</p><p>But my expectations were shattered into pieces during the first week. I was wearing the watch while playing badminton and accidently I hit the watch with my racket. Below is the proof and the current state of the watch. I hit the lower right corner of the watch and the screen cracked into two sections. The best part is that the touch doesn’t even works on the lower part.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BNiAZT_HIZnzv0il3XR8Jg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qxwF_5vL6VrQ9gMYlnH3ag.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JUgGYknKpuxm8eVQNjxmsg.jpeg" /></figure><p>After that I knew the water resistance is long gone and sadly I’ve never even got to wear the watch at least once while I was swimming. I was truly astounded and disappointed by the lack of durability of the watch, since it is highly marketed as a sports watch and the Apple watch even has a separate Nike model which is promoted for athletes.</p><p>For a company who pays a lot of attention to bring the best quality products at a premium price to their beloved customers, I have no explanation as to why you didn’t make the screen of the Apple watch with more hard and durable material which can endure rough usage and maintain structural integrity.</p><p>After this incident I wouldn’t recommend anyone to buy an apple watch, if they are looking for a sports model since clearly you need to get your shit together and come up with a solid, durable watch and please stop deceiving people by saying it is the <a href="https://www.apple.com/apple-watch-series-3/">ultimate sports watch</a>.</p><p>Kind Regards,</p><p>Sajith</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d65008fcb6da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Custom column names in Phoenix Models]]></title>
            <link>https://medium.com/@sajithdilshan/custom-column-names-in-ecto-models-698a12573a09?source=rss-831e3f2ebc88------2</link>
            <guid isPermaLink="false">https://medium.com/p/698a12573a09</guid>
            <category><![CDATA[custom-primary-key]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[ecto]]></category>
            <category><![CDATA[custom-column-names]]></category>
            <dc:creator><![CDATA[Sajith Dilshan]]></dc:creator>
            <pubDate>Sun, 03 Sep 2017 14:20:08 GMT</pubDate>
            <atom:updated>2017-09-03T14:29:58.186Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f3sij2e9AowEFovkGxP_TQ.png" /></figure><p>When working with <a href="http://phoenixframework.org/">Phoenix framework</a>, you need to create your data models and plug in your database schema to your app. If you are not starting your project from the scratch, there is a high probability that you want your data models to be adhered an existing database schema with unconventional column names. So without any further ado, let’s see how we can introduce custom column names in Ecto.</p><p>For this example, I am assuming that you have already set up your Phoenix app. If you haven’t yet, go through the awesome <a href="https://hexdocs.pm/phoenix/up_and_running.html#content">documentation of Phoenix Framework</a> and create your app.</p><p>In this example, I have four tables, and those are Tenant, Group, User and Permission. Further, the primary key of these tables are auto generated, sequentially increasing integers. But the primary key column name will be <strong>ID</strong> (not the usual<em> id</em>). Figure 1 depicts the ER diagram of the schema. In the ER diagram, what I would like to highlight is the relationships between each table. There are many_to_many, belongs_to, and has_many relationships among the tables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*OkRnPJSzr7yAMgSGGFHivw.png" /><figcaption>Figure 1: ER digaram of the table schema</figcaption></figure><p>First, let’s create the migration. Below command will create a new migration file.</p><p>$&gt;<strong> mix ecto.gen.migration sample_migration</strong></p><p>The content of the migration file is as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16d45408ed095fba5a675c879ef1152b/href">https://medium.com/media/16d45408ed095fba5a675c879ef1152b/href</a></iframe><p>Note that when creating tables, <strong><em>primary_key: false</em></strong><em> </em>has been specified so that we can specify our own primary key. <strong><em>add :ID, :serial, primary_key: true </em></strong>specifies our custom primary key column. Since we are specifying a custom primary key column name, we need to explicitly specify the column name when specifying foreign key constrains [<strong><em>references(:TEST_TENANTS, column: :ID)</em></strong>]</p><p>Now let’s see the table schemas. Below is the Tenant schema</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbae48804a4f9871eac3062b36275474/href">https://medium.com/media/bbae48804a4f9871eac3062b36275474/href</a></iframe><p>User schema</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19348a754bbc4a65c600a32327c4ed5d/href">https://medium.com/media/19348a754bbc4a65c600a32327c4ed5d/href</a></iframe><p>Permission schema</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f12c302d4a2b9551ffe0aec3ecceb20/href">https://medium.com/media/5f12c302d4a2b9551ffe0aec3ecceb20/href</a></iframe><p>And.. last but not least, the Group schema</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ee4ba8dcafca4a832790351e2d8ca2b/href">https://medium.com/media/9ee4ba8dcafca4a832790351e2d8ca2b/href</a></iframe><p>I will explain how the custom column names are supported using the <em>group</em> schema. First, we have specified <strong><em>@primary_key {:ID, :id, autogenerate: true}</em></strong> which denotes that the primary key column name of this table is <strong><em>ID</em></strong> and the type of the column is <a href="https://hexdocs.pm/ecto/Ecto.Schema.html#module-primitive-types"><strong><em>id</em></strong></a><strong><em>. </em></strong>After that, <strong><em>schema “TEST_GROUPS”</em></strong> specifies that this model represents the table with the name <em>TEST_GROUPS </em>in the database.</p><p>According the the ER diagram, a group belongs to a tenant. below line represent that constrain.</p><p><strong><em>belongs_to :TENANT, Test.Tenant, foreign_key: :TENANT_ID, references: :ID</em></strong></p><p>But in this case, we need to specify that the foreign key column name is TENAT_ID. and the corresponding primary key column name in the tenant table is <strong>ID</strong>. Otherwise, when we run an Ecto query, the query will try to find a column name <em>TENAT_id</em> in TEST_GROUPS table.</p><p>Next a particular group can have many permissions. Below line represents that constrain.</p><p><strong><em>has_many :PERMISSIONS, Test.Permission, foreign_key: :GROUP_ID, references: :ID</em></strong></p><p>In here, we need to specify the foreign key column name in the TEST_PERMISSIONS table explicitly. Otherwise, queries would look for a column named GROUP_id. Apart from that, we need to specify that the GROUP_ID column in TEST_PERMISSIONS table refers to the ID column of the TEST_GROUPS table.</p><p>Finally, there is a many_to_many relationship between TEST_USERS and TEST_GROUPS table. Below line denotes that relationship.</p><p><strong><em>many_to_many :USERS, Test.User, join_through: “TEST_GROUPS_USERS”, join_keys: [GROUP_ID: :ID, USER_ID: :ID]</em></strong></p><p>Note that the many_many_relationship is maintained via TEST_GROUPS_USERS table and we need to specify it using <strong><em>join_through</em></strong>. Further, we need to specify the primary key column names under <strong><em>join_keys</em></strong> since our primary key column names are unconventional.</p><p>That’s it. Now the Ecto models are mapped with our database schema. Next, let’s see how we can execute a query with all this custom column names. Suppose I want to obtain all the groups of an user with a particular Id (e.g: 6) , then below query would do the trick.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f716c836bd1a66fef675993092e9e12/href">https://medium.com/media/8f716c836bd1a66fef675993092e9e12/href</a></iframe><p>Usually, the <em>where</em> clause would be <strong><em>user.ID == 6</em></strong>. But this will not compile since ID is in uppercase. Hence, we need to specify it within single quotes. This goes for all the other column names as well since all of them are in upper case.</p><p>Hope this blog post has been useful to you. Happy coding with Elixir+ Phoenix.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=698a12573a09" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing UltraStudio]]></title>
            <link>https://medium.com/@sajithdilshan/introducing-ultrastudio-aa259a22438c?source=rss-831e3f2ebc88------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa259a22438c</guid>
            <category><![CDATA[ultrastudio]]></category>
            <category><![CDATA[ultraesb-x]]></category>
            <category><![CDATA[project-x]]></category>
            <category><![CDATA[ultraesb]]></category>
            <dc:creator><![CDATA[Sajith Dilshan]]></dc:creator>
            <pubDate>Thu, 12 Jan 2017 00:00:00 GMT</pubDate>
            <atom:updated>2017-01-19T15:54:51.135Z</atom:updated>
            <content:encoded><![CDATA[<p>For the past couple of months, we at AdroitLogic have been quite busy developing our brand new product stack. Now, after months of tiresome efforts, we are excited to present to you <strong>UltraStudio</strong>, the new Graphical Integration Development Environment we have built to make the UltraESB-X project developer’s life easier.</p><p>UltraStudio is a fully featured integration flow development environment for UltraESB-X. It is built on top of the world’s best IDE, IntelliJ IDEA, so you could have the best of both worlds. If you are already using IntelliJ IDEA, you can integrate UltraStudio into your IDE with just a few clicks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5KepAW7e7wMjkrH1.png" /></figure><p>With UltraStudio, developing a mediation sequence — or <strong>integration flow</strong>, as we call it now — is just a matter of playing around with building blocks — <strong>connectors</strong> and <strong>processors</strong>. Want to expose a HTTP endpoint? Just drag-and-drop a HTTP ingress connector from the palette and configure it. Want to convert the inbound XML message to JSON? Add an XML-to-JSON transformer and attach it to the connector. With the super-simple graphical UI, a complex flow that takes hours of design and hundreds of lines of code could come to life within a matter of minutes.</p><p>UltraStudio comes with hundreds of resources to kickstart your integration project, including a lot of sample projects to help you understand the new development flow. With a wide range of connectors for JMS, HTTP, FIX, AS2, SFTP, AMQP and other protocols, and processors to manipulate the messages flowing through the UltraESB-X, developing integration flows has become easier and more intuitive than ever.</p><p>Having said that, if you feel like writing your own processor or connector, you can simply go ahead and do it within your IDE itself since the underlying <a href="http://blog.ruwan.org/2017/01/the-x_10.html">Project-X</a> framework supports extensibility by nature. Further, you can run the flow you have created within the IDE, and trace the details of the message which went through each component to debug any possible issues in your flow as well.</p><p>Last but not the least, have a sneak peek at the following screencast on how to develop, run and test an integration project with the new UltraStudio by AdroitLogic!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FOSjfSwMv0Xo%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DOSjfSwMv0Xo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FOSjfSwMv0Xo%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6d4e5e0db6cb4cacdd4450eee18472e/href">https://medium.com/media/c6d4e5e0db6cb4cacdd4450eee18472e/href</a></iframe><p><em>Originally published at </em><a href="http://sajithdilshan.github.io/2017/01/12/introducing-ultrastudio/"><em>sajithdilshan.github.io</em></a><em> on January 12, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa259a22438c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>