⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileupload.jsp

📁 国外的一套开源CRM
💻 JSP
📖 第 1 页 / 共 2 页
字号:
<%@ page import="org.ofbiz.entity.*" %>
<%@ page import="org.ofbiz.entity.model.*" %>
<%@ page import="java.lang.reflect.Method" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.ofbiz.entity.util.SequenceUtil" %>
<%@ page import="http.utils.multipartrequest.ServletMultipartRequest"%>
<%@ page import="http.utils.multipartrequest.MultipartRequest"%>
<%@ page import="com.sourcetap.sfa.util.CsvConverter"%>
<%@ page import="com.sourcetap.sfa.replication.*" %>
<%@ page import="com.sourcetap.sfa.ui.*"%>
<%@ page import="com.sourcetap.sfa.event.*"%>
<%@ page import="com.sourcetap.sfa.util.*" %>
<%@ page import="java.net.*" %>

<%@ page import="javax.servlet.ServletException" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpServletResponse" %>
<%@ page import="javax.servlet.http.HttpServlet" %>

<%@ page import="com.infomata.data.CSVFormat" %>
<%@ page import="com.infomata.data.DataFile" %>
<%@ page import="com.infomata.data.DataRow" %>

<%@ include file="/includes/header.jsp" %>
<jsp:useBean id="leadEventProcessor" class="com.sourcetap.sfa.lead.LeadEventProcessor" scope="application" />
<jsp:useBean id="accountEventProcessor" class="com.sourcetap.sfa.account.AccountEventProcessor" scope="application" />
<jsp:useBean id="contactEventProcessor" class="com.sourcetap.sfa.contact.ContactEventProcessor" scope="application" />
<jsp:useBean id="importEventProcessor" class="com.sourcetap.sfa.event.GenericImportEventProcessor" scope="application" />


<%
  String screen = "LEAD";
  String screenSection = "LeadHeader";
	String entityName = "";
  CsvConverter csvConverter = null;
	String action = "upload"; // default action.  since it is part of a multi-part form, the parameter won't be returned by request.getParameter()
  
	if ( request.getParameter("action") != null )
	{
		action = request.getParameter("action");
	}

    try{

		if(action.equals("upload")){
		
			URL sfaPropertiesUrl = application.getResource("/WEB-INF/sfa.properties");
			
			String filePath = UtilProperties.getPropertyValue(sfaPropertiesUrl, "fileImport.path", "imports");
			
			if ( (filePath.indexOf('\\') == - 1) && (filePath.indexOf('/') == -1) )
				filePath = getServletContext().getRealPath("/") + filePath;
			
			File directory = new File(filePath);
			if ( !directory.isDirectory() )
			{
				boolean created = false;
				try {
					created = directory.mkdir();
				} catch ( Exception e) {}
				if ( !created )
				{
					throw new Exception("Unable to create directory:" + filePath);
        }
      }
			
			String fileName = "imp" + userInfo.getPartyId() + "-" + screen + "." + screenSection + System.currentTimeMillis() + ".txt";
			
			MultipartRequest parser = new ServletMultipartRequest(request, filePath, fileName);
			
			File importFile = parser.getFile("xferFile");
			
			if ( importFile == null )
			{
				out.write("<P><B>No file specified<P>");
				return;
    }
			
			String importFilePath = importFile.getCanonicalPath();
			
			screen = parser.getURLParameter("screen");
			screenSection = parser.getURLParameter("screenSection");
			if ( screen.equals("LEAD") )
				entityName = "Lead";
			else if ( screen.equals("ACCOUNT") )
				entityName = "Account";
			else if ( screen.equals("CONTACT") )
				entityName = "Contact";
			else
			{
				out.write("Unable to import" + screen + " data at this time" );
				throw new Exception("Import not implemented");
  }
			ModelEntity entity = delegator.getModelEntity(entityName);

	 		UIWebScreenSection uiWebScreenSection = importEventProcessor.getUiWebScreenSection(userInfo, screen, screenSection, delegator, uiCache); 				
			List fields = uiWebScreenSection.getDisplayFields(uiCache);
			
			int numFields = fields.size();
%>
			<table width="100%" height="100%" class="freeFormSectionDisplayTable"><tr><td valign="top">
			      <!-- title table -->
					  <table class="freeFormSectionDisplayTable" cellspacing="0" cellpadding="2">
					  <form method="post" action="/sfa/control/fileUpload">
						   <input type=hidden name=action value="import">
						   <input type=hidden name=importFile value="<%=importFilePath%>">
						   <input type=hidden name=importTable value="<%=entityName%>">
						   <input type=hidden name=screen value="<%=screen%>">
						   <input type=hidden name=screenSection value="<%=screenSection%>">
						   <input type="hidden" name="numFields" value="<%=numFields%>">
 						<tr><td colspan=2><center><b>Step 2. Map fields in the File to the Table.</b></center></td></tr>
						<tr><td class=freeFormSectionLabel><center>Table Field<center></td>
						    <td class=freeFormSectionLabel><center>File Field</center></td>
						    <td class=freeFormSectionLabel><center>Fixed Value</center></td></tr>

<%			

			//List  fields = entity.getFieldsCopy();
			Iterator fieldIter = fields.iterator();
			
			DataFile read =  DataFile.createReader("8859_1");

			read.setDataFormat(new CSVFormat());
	
			// first line is column header
			read.containsHeader(false);
	
			read.open(importFile);
		    DataRow header = read.next();
		    if ( header == null )
		    {
		    	out.write("<BR>No data in file");
		    	return;
		    }

		    // copy header fiels to new array
		    HashMap availableFileFields = new HashMap();
		    for ( int i=0; i < header.size(); i++ )
		    	availableFileFields.put(new Integer(i), header.getString(i));

			while (fieldIter.hasNext())
			{
			
			     UIFieldInfo fieldInfo = (UIFieldInfo)fieldIter.next();
				//     if ( fieldInfo.getIsVisible() && !fieldInfo.getIsReadOnly() )

	  	    	String fieldName = UIWebUtility.getHtmlName(screenSection, fieldInfo, 0);
     			String displayName = fieldInfo.getDisplayLabel();
				String displayNameLower = displayName.toLowerCase();
				String attribNameLower = fieldInfo.getUiAttribute().getAttributeName().toLowerCase();
							
				if ( fieldName.equals("lastUpdatedStamp") || fieldName.equals("lastUpdatedTxStamp") ||
				     fieldName.equals("createdStamp") || fieldName.equals("createdTxStamp") )
				     continue;
%>
				<tr><td class=freeFormSectionLabelOptional><%=displayName%></td><td class=freeFormSectionField>
					<SELECT NAME="<%=fieldName%>" CLASS="selectBox">
					   <OPTION VALUE=-1>NOT MAPPED</OPTION>
    <%
					boolean found = false;
					for ( int i = 0; i < header.size(); i ++)
      {
						String selected = "";
						String fileFieldName = (String) header.getString(i);
						
						if ( !found && (availableFileFields.get(new Integer(i)) != null) )
						{
							String fileFieldNameLower = fileFieldName.toLowerCase();
													
							if ( (fileFieldNameLower.indexOf(displayNameLower) >= 0 ) || ( displayNameLower.indexOf(fileFieldNameLower) >= 0) ||
							     (fileFieldNameLower.indexOf(attribNameLower) >= 0) || ( attribNameLower.indexOf(fileFieldNameLower) >= 0) )
							{
								availableFileFields.remove( new Integer(i));
							selected = " selected";
								found = true;
							}	
						}
%>						<OPTION VALUE=<%=i + selected%>><%=fileFieldName%></OPTION>
<%
      }
%>					</SELECT>
				    </td>
				    <td><input name="<%=fieldName + "_Fixed"%>" size=20></td>
				</tr>
<%				
    }

			// allow the results to be added to an existing list
			// find all existing lists of the same entity Type
			List availableLists = delegator.findByAnd("UiList", UtilMisc.toMap("listType", entityName, "partyId", userInfo.getAccountId()), UtilMisc.toList("listName"));
			Iterator listIter = availableLists.iterator();
			String listOptions = "<option value='' selected></option>\n";
			while ( listIter.hasNext() )
			{
				GenericValue listGV = (GenericValue) listIter.next();
				listOptions += "<option value='" + listGV.getString("listId") + "'>" + listGV.getString("listName") + "</option>\n";
			}
%>
			<tr><td colspan=3><br></td></tr>
			<tr><td class=freeFormSectionLabelOptional>Add imported data to Existing List<br>or create a new List</td><td class=selectBox><select name=uiListId width='100%'><%=listOptions%></select></td><td><input name=uiListName size=20></td></tr>
<%
			
			// if it is a Contact, Lead, or Account allow an Identifier to be set
			if ( entityName.equals("Contact") || entityName.equals("Account") || entityName.equals("Lead") )
			{
				List availableIdentifiers = delegator.findByAnd("Code", UtilMisc.toMap("codeTypeId", "IDENTIFIER_TYPE"), UtilMisc.toList("codeValue"));
				Iterator identIter = availableIdentifiers.iterator();
				String identOptions = "<option value='' selected></option>\n";
				while ( identIter.hasNext() )
				{
					GenericValue identifierGV = (GenericValue) identIter.next();
					identOptions += "<option value='" + identifierGV.getString("codeId") + "'>" + identifierGV.getString("codeValue") + "</option>\n";
				}
%>
				<tr><td colspan=3><br></td></tr>
				<tr><td class=freeFormSectionLabelOptional>Set Identifier for imported data to Existing Identifier<br>or create a new Identifier</td><td class=selectBox><select name=identifierId width='100%'><%=identOptions%></select></td><td><input name=identifierName size=20></td></tr>
<%
			}
			
%>					<tr>
						<td colspan=3 align=center><center><input type="submit" value="import"></center></td>
					</tr>
  					</form>
					</table>
<%				if ( availableFileFields.size() > 0 )
				{
%>					</td>
					<td valign=top align=left><table class=freeFormSectionDisplayTable border=1><tr><td><b>File Fields Not Automatically Mapped</b></td></tr>
<%					Iterator iter = availableFileFields.values().iterator();
					while ( iter.hasNext())

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -