Showing posts with label Today Tech Stuff. Show all posts
Showing posts with label Today Tech Stuff. Show all posts

Friday, 9 August 2013

ZK upload PDF to server and show in the screen using MVVM

Simple example to upload PDF file in the server and show the content using iframe.

<zk>
<window id="test" border="normal" height="98%"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('org.zk.example.FileUploadVM')">
<label value="You are using: ${desktop.webApp.version}" />
<separator></separator>
<label
value="Example for File upload to the server and display it" />
<separator></separator>
<hbox>
<label value="Upload any PDF File" />
<button label="Upload" upload="true"
onUpload="@command('onUploadPDF',upEvent=event)">
</button>
<button label="Show PDF" visible="@load(vm.fileuploaded)"
onClick="@command('showPDF')">
</button>
</hbox>

<iframe height="100%" width="100%" id="reportframe"
content="@bind(vm.fileContent)">
</iframe>
</window>




</zk>

ViewModel


package org.zk.example;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;

import org.zkoss.bind.BindContext;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.io.Files;
import org.zkoss.util.media.AMedia;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Iframe;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class FileUploadVM {

private String filePath;
private boolean fileuploaded = false;
AMedia fileContent;

@Wire("#test")
private Window win;

public AMedia getFileContent() {
return fileContent;
}

public void setFileContent(AMedia fileContent) {
this.fileContent = fileContent;
}

public boolean isFileuploaded() {
return fileuploaded;
}

public void setFileuploaded(boolean fileuploaded) {
this.fileuploaded = fileuploaded;
}

@AfterCompose
public void initSetup(@ContextParam(ContextType.VIEW) Component view)
{
Selectors.wireComponents(view, this, false);

}

@Command
@NotifyChange("fileuploaded")
public void onUploadPDF(
@ContextParam(ContextType.BIND_CONTEXT) BindContext ctx)
throws IOException {

UploadEvent upEvent = null;
Object objUploadEvent = ctx.getTriggerEvent();
if (objUploadEvent != null && (objUploadEvent instanceof UploadEvent)) {
upEvent = (UploadEvent) objUploadEvent;
}
if (upEvent != null) {
Media media = upEvent.getMedia();
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH); // Note: zero based!
int day = now.get(Calendar.DAY_OF_MONTH);
filePath = Executions.getCurrent().getDesktop().getWebApp()
.getRealPath("/");
String yearPath = "\\" + "PDFs" + "\\" + year + "\\" + month + "\\"
+ day + "\\";
filePath = filePath + yearPath;
File baseDir = new File(filePath);
if (!baseDir.exists()) {
baseDir.mkdirs();
}

Files.copy(new File(filePath + media.getName()),
media.getStreamData());
Messagebox.show("File Sucessfully uploaded in the path [ ."
+ filePath + " ]");
fileuploaded = true;
filePath = filePath + media.getName();
}
}

@Command
@NotifyChange("fileContent")
public void showPDF() throws IOException {
File f = new File(filePath);
Messagebox.show(" dfdfdfdsfdsf" + filePath);
byte[] buffer = new byte[(int) f.length()];
FileInputStream fs = new FileInputStream(f);
fs.read(buffer);
fs.close();
ByteArrayInputStream is = new ByteArrayInputStream(buffer);
fileContent = new AMedia("report", "pdf", "application/pdf", is);



}
}

Wednesday, 7 August 2013

ZK Quick InputBox

Just to get String input before do some process. For example, we will normally ask the reason to delete any record.

<?xml version="1.0" encoding="UTF-8"?>
<zk>
<window id="inputstringbox" title="@load(vm.screenTitle)"
width="auto" height="auto" border="normal" minimizable="false"
sclass="mymodal" mode="modal" maximizable="false" closable="true"
action="hide: slideUp" apply="org.zkoss.bind.BindComposer"
onCancel="@command('closeThis')"
viewModel="@id('vm') @init('com.product.webapp.component.InputStringBoxVM')">
<ftextbox id="txtDescription" rows="4" cols="70"
value="@bind(vm.input)" />
<separator />
<div align="center">
<fbutton label="Ok" onClick="@command('onOk')" />
<fbutton label="Cancel" onClick="@command('closeThis')" />
</div>
</window>
</zk>

package com.product.webapp.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;

import com.product.webapp.utilities.GlobalCommandValues;
import com.product.webapp.utilities.Libs;
import com.product.webapp.utilities.ShowWindow;
import com.product.webapp.utilities.ValidationMessage;

public class InputStringBoxVM {

@Wire("#inputstringbox")
private Window win;
private String input;
private String validateMessage;
private String screenTitle;

public String getInput() {
return input;
}

public String getScreenTitle() {
return screenTitle;
}

public void setScreenTitle(String screenTitle) {
this.screenTitle = screenTitle;
}

public String getValidateMessage() {
return validateMessage;
}

public void setValidateMessage(String validateMessage) {
this.validateMessage = validateMessage;
}

public void setInput(String input) {
this.input = input;
}

@AfterCompose
public void initSetup(@ContextParam(ContextType.VIEW) Component view,
@ExecutionArgParam("validateMessage") String validateMessage,
@ExecutionArgParam("screenTitle") String screenTitle) {
Selectors.wireComponents(view, this, false);
this.validateMessage = validateMessage;
this.screenTitle = screenTitle;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Command
public void onOk() {
if (IsValidEntry() == false) {
return;
} else {
Map args = new HashMap();
args.put("input", this.input);
BindUtils.postGlobalCommand(null, null,
GlobalCommandValues.InputStringEntered, args);
win.detach();
}
}

public boolean IsValidEntry() {

List<ValidationMessage> vList = new ArrayList<ValidationMessage>();

if (Libs.isNotEmpty(this.input) == false) {
vList.add(new ValidationMessage(this.validateMessage));
}

if (vList.size() > 0) {
ShowWindow.ShowValidation(vList);
return false;
} else
return true;
}

@Command
public void closeThis() {
win.detach();
}
}

public static void showInputBox(String title, String validateMessage,
Window callingFrom) {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("screenTitle", title);
map.put("validateMessage", validateMessage);
if (callingFrom.hasFellow("inputstringbox") == false) {
Executions.createComponents("/ZK/components/inputstringbox.zul",
null, map);
}
}

@Command
public void onResetPassword() {
ShowWindow.showInputBox("Reason",
"Pleas Enter the reason to reset the password.", win);
}

@GlobalCommand(GlobalCommandValues.InputStringEntered)
public void resetPassword() {
// do something here
}

Friday, 2 August 2013

HL7 Parsing

I was working in parsing the HL7 ORU Message using HAPI. Found an interesting article by here by Ignacio Suay. Thanks mate for simple and quick example

Based on that, here is my example, But still finding a way to extract OBR, OBX and NTE Segments.

package com.product.HL7;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.util.Terser;
import ca.uhn.hl7v2.validation.impl.NoValidation;

import com.product.domain.LabResults;

public class ParseFile {

static final Logger LOG = LoggerFactory.getLogger(ParseFile.class);
private String HL7Message;
private List<HL7Data> MSH = new ArrayList<HL7Data>();
private List<HL7Data> orderInfo = new ArrayList<HL7Data>();
private ParsingEvent parseingEvent;

public List<HL7Data> getOrderInfo() {
return orderInfo;
}

public void setOrderInfo(List<HL7Data> orderInfo) {
this.orderInfo = orderInfo;
}

public ParseFile(ParsingEvent parseingEvent) {
this.parseingEvent = parseingEvent;
}

public String getHL7Message() {
return HL7Message;
}

public List<HL7Data> getMSH() {
return MSH;
}

public void setMSH(List<HL7Data> mSH) {
MSH = mSH;
}

public void setHL7Message(String hL7Message) {
HL7Message = hL7Message;
}

public void parseMessage() throws HL7Exception {

String element;
String value;
HapiContext context = new DefaultHapiContext();
context.setValidationContext(new NoValidation());
PipeParser pipeParser = context.getPipeParser();
/* pipeParser.setValidationContext(new NoValidation()); */
Message message = pipeParser.parse(HL7Message);
Terser terser = new Terser(message);

LabResults labResults = new LabResults();

element = "Receiving Facility";
value = terser.get("/.MSH-6");
LOG.debug(element + " : " + value);
labResults.setDocAccountNo(value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Date";
value = terser.get("/.MSH-7");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient No";
value = terser.get("/.PID-2");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Lab Accession Number";
value = terser.get("/.PID-4");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Last Name";
value = terser.get("/.PID-5-1");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient First Name";
value = terser.get("/.PID-5-2");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Middle Name";
value = terser.get("/.PID-5-3");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient DOB";
value = terser.get("/.PID-7");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Sex";
value = terser.get("/.PID-8");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Address";
value = terser.get("/.PID-11-1");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient City";
value = terser.get("/.PID-11-2");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient State";
value = terser.get("/.PID-11-3");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Zip";
value = terser.get("/.PID-11-4");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Phone";
value = terser.get("/.PID-13");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Maritial Status";
value = terser.get("/.PID-16");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient EHR Number";
value = terser.get("/.PID-18");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient SS";
value = terser.get("/.PID-19");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Care Number";
value = terser.get("/.PID-20");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Caid Number";
value = terser.get("/.PID-21");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

element = "Patient Age";
value = terser.get("/.PID-23");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;

/*********************** Order Informaton ************************************/

element = "Lab Accession Number";
value = terser.get("/.ORC-2");
LOG.debug(element + " : " + value);
orderInfo.add(new HL7Data(element, value));
value = null;
element = null;

parseingEvent.parsingCompleted();

}
}

Some important note.
1. Hapi always expect \r at the end of the each segment. So if you have manually append the character at the end of the each segment Smile as i did in my example

	// convert InputStream to String
private static String getStringFromInputStream(InputStream is) {

BufferedReader br = null;
StringBuilder sb = new StringBuilder();

String line;
try {

br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line + "\r");
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return sb.toString();

}


2. HAPI will also validate the Message while parsing. If you want to avoid that, see my example above.

3. But i don’t think so my job is going to easy to fetch OBR and OBX and NTE segments using HAPI. I am trying with that, latest will post if i am able to complete.

Sample File
MSH|^~\&|mas|My LAB|mas|3535|201305280003||ORU^R01||P|2.3|
PID|1|PAT393929|^^^MRN~^^^CID~^^^ACCN|000035100|MOOAFEFERE^SCOOPDSTT^||19690608|M|||^^FL^|||||U|||||||43|y|
ORC|1|000035100|||CM||||201302271648|PAH|201302271700|^^^^     ^^^^^|||||355^MY HOUSE|0011 NW 10 STR^MYCITY^NY^11111^111-371-1111|3|PAH|
OBR|1|000035100||BATH SALTS^BATH SALTS|||20130131----|201302271651||||||201302271648||^^^^     ^^^^^||D|DRUGS||||||F|
OBX|1|TX|BATH SALTS^BATH SALTS|  ^D^[|NEGATIVE||-||||F||7|201302271651||PAH||