How to upload a file for processing
June 16, 2009 in Maximo, Maximo 6, Maximo 7, Maximo: Programming by Michael Chrisman 15 Comments
Although the MIF/MEA is great for processing inbound files. However, sometimes they do not meet all your needs. For example, the MIF will require the user to have access to a folder on the server (a security problem). The MIF is also design for automated data import. Sometime you need to allow for the one-off, user managed data import. It is possible, through a dialog box on an app screen to import and process a file using a DataBean.
The first step is to create your dialog box (see How to Create a Dialog box). As it turns out, there is a Maximo control for the file upload control. The issue is that it is not one the control pallet. What you have to do it edit a screen that uses the control (like External System) and copy the XML for the control. Or you can just use the following code ![]()
1: <buttongroup id="load_2222">2: <uploadfile cancellabel="Cancel" event="loadData" id="importfile_file_2_1" label="Specify Import File" maxfilesize="200" oklabel="OK"/>3: buttongroup>
Add this code (by editing the XML) to you dialog box. You can just have this as the only thing on the dialog box if you want. You will notice that the event attribute is ‘loadData’. This is the name of the method you need to add to your DataBean. In your method, add the following code:
1: public int loadData() throws NumberFormatException, MXException, RemoteException2: {3: // setup some variables.4: ByteArrayInputStream btIn;5: javax.servlet.http.HttpServletRequest request;6: String fName;7: MPFormData mpData = null;8: btIn = null;9: request = clientSession.getRequest();10: fName = null;11: WebClientEvent wce = clientSession.getCurrentEvent();12: ControlInstance uploadfileControl = wce.getSourceControlInstance();13: String maxfilesize = uploadfileControl.getProperty("maxfilesize");14: mpData = new MPFormData(request, Integer.parseInt(maxfilesize));15: fName = mpData.getFileName();16:17: // check to make sure the file name exists18: if(fName == null || fName.toUpperCase().equals("UNKNOWN"))19: {20: // TODO: the user did not select a file, so throw a MXAppliationException to display such a messgae21: }22:23: byte fileBytes[] = mpData.getFileOutputStream().toByteArray();24: btIn = new ByteArrayInputStream(fileBytes);25:26: // This is where we process the file. We read the file one character at a time.27: int curIntChar; // this will be the current character we are processing28: String currentLine = ""; // this will be a full line of data from the file.29: int RetVal;30: for (int i = 0; i < fileBytes.length; i++)31: {32: curIntChar = btIn.read();33: if ((char) curIntChar == 'n') // check for a carrage return. You made need to modify this if you are loading a unix file.34: {35: // TODO: add code here to process the line that is stored in 'currentline'36: currentLine = "";37: }38: else39: {40: currentLine += (char) curIntChar;41: }42: }43: // we are all done with the file, but we may have a line to process if the last line did not end in a carrage return.44: if (currentLine.length()>0)45: {46: //TODO: process the line that is stored in 'currentline'47: }48: //ok, the file is processed49:50: // be kind and close the data stream.51: try52: {53: if(btIn != null)54: btIn.close();55: }56: catch(Exception e)57: {58: e.printStackTrace();59: }60: return 1;61: }
Now compile and test. This will allow you to upload and process any type of file you can read with Java.
Sadly, i am getting a resource not found after modify the app’s xml to include the buttongroup with the uploadfile tag.
Specifically:
<Include resource or file “/webclient/controls/uploadfile/control.jsp” not found from requested resource “/ui/maximo.jsp”.
Am I sunk? Is there anything I can do at this point?