<%@ page import="org.apache.axis.utils.ByteArrayOutputStream"%> <%@ page language="java" contentType="text/html; charset=UTF-8" %> <%@ page import="com.hof.util.*, java.util.*, java.text.*" %> <%@ page import="com.hof.web.form.*" %> <%@ page import="com.hof.mi.web.service.*" %> <%@ page import="java.io.*" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%! String hostname = "54.66.203.3"; int port = 8083; String address = "/services/AdministrationService"; String username = "admin@yellowfin.com.au"; String password = "test"; String clientOrgReferenceId = null; boolean SSL = false; private AdministrationServiceResponse postRequest(AdministrationServiceRequest request) throws Exception { request.setLoginId(username); request.setPassword(password); request.setOrgId(new Integer(1)); request.setOrgRef(clientOrgReferenceId); AdministrationServiceService ts = new AdministrationServiceServiceLocator(hostname, port, address, false); AdministrationServiceSoapBindingStub rssbs = (AdministrationServiceSoapBindingStub) ts.getAdministrationService(); AdministrationServiceResponse rs = rssbs.remoteAdministrationCall(request); return rs; } private byte[] handlePost(HttpServletRequest request) throws Exception { String contentType = request.getContentType(); if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) { DataInputStream in = new DataInputStream(request.getInputStream()); //we are taking the length of Content type data int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //this loop converting the uploaded file into byte code while (totalBytesRead < formDataLength) { byteRead = in.read(dataBytes, totalBytesRead,formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); //for saving the file name String saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1, contentType.length()); int pos; //extracting the index of file pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; ByteArrayOutputStream fileOut = new ByteArrayOutputStream(); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); return fileOut.toByteArray(); } return null; } private String generateReplacementList(String type, ContentResource[] contentResources, String selectedValue) { StringBuffer sb = new StringBuffer(1000); ContentResource cr = null; sb.append("\n\t"); for (int i =0; i < contentResources.length; i++) { cr = contentResources[i]; if (!type.equals(cr.getResourceType())) continue; sb.append("\n\t"); } return sb.toString(); } private String isOptionSelected(String text, Map optionValues, int i) { if (optionValues!=null && text.equals(optionValues.get("OPTION_" + i))) return " SELECTED"; return ""; } %>

Yellowfin Import Example

<% out.write("Loading Existing Content...
"); ContentResource[] existingContent = (ContentResource[]) session.getAttribute("ExistingContent"); ContentResource[] importContent = (ContentResource[]) session.getAttribute("ImportContent"); Map optionValues = (Map) session.getAttribute("OptionValues"); String action = request.getParameter("ACTION"); ImportIssue[] importIssueList = null; AdministrationServiceRequest rsr = null; byte[] data = handlePost(request); try { /// Load Content if (optionValues==null) { optionValues = new HashMap(); session.setAttribute("OptionValues", optionValues); } if (existingContent==null) { out.write("Fetching...
"); out.flush(); rsr = new AdministrationServiceRequest(); rsr.setFunction("GETCONTENT"); AdministrationServiceResponse asr = postRequest(rsr); existingContent = asr.getContentResources(); session.setAttribute("ExistingContent", existingContent); } out.write("Existing Content Available: " + existingContent.length + " Elements
"); if (data!=null) { action = "Upload"; } if (action!=null) { if (importContent!=null) for (int p = 0; p < importContent.length; p++) { String key = "OPTION_" + p; String value = request.getParameter(key); if (value!=null) optionValues.put(key, value); key = "REPLACE_" + p; value = request.getParameter(key); if (value!=null) optionValues.put(key, value); } if (action.equals("Start Over")) { session.removeAttribute("ExistingContent"); session.removeAttribute("ImportContent"); session.removeAttribute("OptionValues"); session.removeAttribute("Data"); optionValues = null; importContent = null; action = null; data = null; } else if (action.equals("Upload")) { rsr = new AdministrationServiceRequest(); rsr.setFunction("GETIMPORTCONTENT"); rsr.setParameters(new String[] { com.hof.util.Base64.encodeBytes(data) }); AdministrationServiceResponse asr = postRequest(rsr); importContent = asr.getContentResources(); session.setAttribute("ImportContent", importContent); session.setAttribute("Data", data); } else if ( action.equals("Add All")) { for (int p = 0; p < importContent.length; p++) { optionValues.put("OPTION_" + p, "IMPORT"); optionValues.put("REPLACE_" + p, "NOTHING"); } } else if ( action.equals("Skip All")) { for (int p = 0; p < importContent.length; p++) { optionValues.put("OPTION_" + p, "SKIP"); optionValues.put("REPLACE_" + p, "NOTHING"); } } else if ( action.equals("Validate") || action.equals("Import")) { ArrayList importOptions = new ArrayList(); rsr = new AdministrationServiceRequest(); if (action.equals("Validate")) rsr.setFunction("TESTIMPORTCONTENTNOVALIDATION"); if (action.equals("Import")) rsr.setFunction("IMPORTCONTENT"); for (int p = 0; p < importContent.length; p++) { Integer index = p; ImportOption io = new ImportOption(); io.setItemIndex(index); io.setOptionKey("SKIP"); String value = "false"; if ("SKIP".equals(optionValues.get("OPTION_" + p))) { value = "true"; } io.setOptionValue(value); importOptions.add(io); if ("IMPORT".equals(optionValues.get("OPTION_" + p))) { io = new ImportOption(); io.setItemIndex(index); io.setOptionKey("OPTION"); io.setOptionValue("ADD"); importOptions.add(io); } if ("REPLACE".equals(optionValues.get("OPTION_" + p))) { io = new ImportOption(); io.setItemIndex(index); io.setOptionKey("OPTION"); io.setOptionValue("REPLACE"); importOptions.add(io); String strReplaceIndex = optionValues.get("REPLACE_" + p); if (strReplaceIndex!=null && !strReplaceIndex.equals("NOTHING")) { io = new ImportOption(); io.setItemIndex(index); io.setOptionKey("EXISTING"); ContentResource cr = existingContent[Integer.parseInt(strReplaceIndex)]; if (cr!=null) { String replacementValue = null; if ("RPTCATEGORY".equals(cr.getResourceType()) || "RPTSUBCATEGORY".equals(cr.getResourceType())) { replacementValue = cr.getResourceCode(); } else { replacementValue = cr.getResourceId().toString(); } io.setOptionValue(replacementValue); importOptions.add(io); } } } } data = (byte[]) session.getAttribute("Data"); rsr.setImportOptions(importOptions.toArray(new ImportOption[0])); rsr.setParameters( new String[] { com.hof.util.Base64.encodeBytes(data) } ); AdministrationServiceResponse asr = postRequest(rsr); importIssueList = asr.getImportIssues(); } else if ( action.equals("Replace All")) { ContentResource thisResource = null; for (int p = 0; p < importContent.length; p++) { boolean matchFound = false; thisResource = importContent[p]; ContentResource cr = null; for (int i =0; i < existingContent.length; i++) { cr = existingContent[i]; if (!thisResource.getResourceType().equals(cr.getResourceType())) continue; if (thisResource.getResourceUUID()!=null && thisResource.getResourceUUID().equals(cr.getResourceUUID())) { matchFound = true; optionValues.put("REPLACE_" + p, Integer.toString(i)); System.out.println("Matched " + p + " to " + i); break; } if ("DATASOURCE".equals(thisResource.getResourceType()) && thisResource.getResourceName().equals(cr.getResourceName())) { matchFound = true; optionValues.put("REPLACE_" + p, Integer.toString(i)); System.out.println("Matched " + p + " to " + i); break; } } if (matchFound) { optionValues.put("OPTION_" + p, "REPLACE"); } else { optionValues.put("OPTION_" + p, "IMPORT"); } } } } } catch (Exception e) { e.printStackTrace(new PrintWriter(out)); } if (data!=null) out.write("Import Size: " + data.length + " bytes
"); if (action==null && importContent==null) { %>
Upload File:
<% } else { %>
<% if (importContent!=null) { out.write("
"); for (int i=0; i < importContent.length; i++) { ContentResource cr = importContent[i]; out.write(""); out.write(""); out.write(""); out.write(""); out.write(""); out.write("\n"); out.write(""); out.write(""); } out.write("
TypeNameCode / UUID / IDActionReplacement
" + cr.getResourceType() + "" + cr.getResourceName() + ""); if (cr.getResourceCode()!=null) out.write(cr.getResourceCode() + " "); if (cr.getResourceId()!=null) out.write(cr.getResourceId() + " "); if (cr.getResourceUUID()!=null) out.write(cr.getResourceUUID() + " "); out.write("\n\n\n\n
"); %>

<% } if (importIssueList!=null) { out.write("

Import Issues
"); for (int u=0; u < importIssueList.length; u++) { if (importIssueList[u].getResource()==null) continue; out.println(u + ") " + importIssueList[u].getResource().getResourceName() + " " ); out.write("
"); if (importIssueList[u].getIssueElements()!=null) { for (int v=0; v < importIssueList[u].getIssueElements().length; v++) { out.println("   " + v + ") " + importIssueList[u].getIssueElements()[v].getRenderedMessage()); out.write("
"); } } } if (importIssueList.length==0) { out.write("No Import Issues
"); } } if (importIssueList==null && "Validate".equals(action)) { out.write("

No Import Issues
"); } if ("Import".equals(action)) { out.write("

Imported
"); } %>

<% } %>