|
From: Michael K. <ko...@us...> - 2005-11-22 07:42:35
|
Update of /cvsroot/cobricks/cobricks2/src/org/cobricks/item In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18969/src/org/cobricks/item Modified Files: ItemServlet.java Log Message: Index: ItemServlet.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/item/ItemServlet.java,v retrieving revision 1.36 retrieving revision 1.37 diff -u -d -r1.36 -r1.37 --- ItemServlet.java 22 Aug 2005 13:52:07 -0000 1.36 +++ ItemServlet.java 22 Nov 2005 07:42:27 -0000 1.37 @@ -12,6 +12,7 @@ package org.cobricks.item; +import java.io.File; import java.io.PrintWriter; import java.util.Date; import java.util.ArrayList; @@ -310,11 +311,34 @@ prequest.setReturnCode(2003); return "error"; } + + // check if there are uploaded files and store them in + // attributes or in the context + String tmps = prequest.getRequestParameter("uploadfileattrs"); + String tmpnames = prequest.getRequestParameter("uploadfilenames"); + Map attrsmap = new HashMap(); + if (tmps != null && tmps.length()>0) { + HttpServletRequest request = prequest.getHttpServletRequest(); + StringTokenizer st = new StringTokenizer(tmps, ","); + if (tmpnames == null) tmpnames = ""; + StringTokenizer st2 = new StringTokenizer(tmpnames, ","); + while (st.hasMoreTokens()) { + String aname = st.nextToken(); + String tmpname = null; + if (st2.hasMoreTokens()) tmpname = st2.nextToken(); + Object o = request.getAttribute(aname); + if (o != null && o instanceof FileItem) { + String uri = uploadFile((FileItem)o, tmpname); + attrsmap.put(aname, uri); + } + } + } + String reqICName = prequest.getRequestParameter("itemclass"); if (reqICName == null) reqICName = item.getOntologyClassName(); - Map changedAttrs = buildItemAttrsMap(prequest); + Map changedAttrs = buildItemAttrsMap(prequest, attrsmap); if (changedAttrs.containsKey("errors")) { logger.info("Errors encountered"); prequest.setContextObject("errors", @@ -621,10 +645,12 @@ * otherwise a Map with only one set containing DisplayError * objects is returned. */ - private Map buildItemAttrsMap(PortalRequest prequest) + private Map buildItemAttrsMap(PortalRequest prequest, Map attrsmap) { Set errors = new HashSet(); // All errors are stored here Set parameterNames = prequest.getRequestParameters().keySet(); + parameterNames.remove("uploadfileattrs"); + parameterNames.remove("uploadfilenames"); Map result = new HashMap(); // handle the item class attribute @@ -660,77 +686,24 @@ input = input + ";" + inputs[i]; } } - if (input == null) input = ""; - if (shouldProcessAttribute(currentParam, input, prequest)) { - // this attribute should be processed - Object attr = - OntologyHelper. - instantiateAttrExternal(input, - currentParam, - itemManager.getOntology(). - getClass(itemClassName, "item"), - coreManager, - dateTimeFormatter, dateFormatter); - if (attr instanceof DisplayError) { // attr is a single error - ((DisplayError)attr).setEnvironment(currentParam); - errors.add(attr); - } else if (attr instanceof Set - && ((Set) attr).iterator().next() - instanceof DisplayError) { - // attr has possibly multiple errors - Iterator it2 = ((Set) attr).iterator(); - while (it2.hasNext()) { - DisplayError next = (DisplayError) it2.next(); - next.setEnvironment(currentParam); - errors.add(next); - } - } else if (attr != null) { // Item was correctly instantiated - if (prequest.getRequestParameter(currentParam + "_lang") - == null) { // No multiple language - result.put(currentParam, attr); - } else { // Multiple language - result.put(currentParam + "_" - + prequest.getRequestParameter(currentParam - + "_lang"), attr); - } - } else { - // Something went wrong which is not covered with an - // error message. Looks like a greater problem. - logger.warn("cannot handle attribute: " + currentParam); - } - } else if (currentParam.matches("_customAname.*")) { - // Process custom attributes - String number = - currentParam.substring(currentParam.lastIndexOf("_"), - currentParam.length()); - String aname = prequest.getRequestParameter(currentParam); - String avalue = prequest. - getRequestParameter("_customAvalue" + number); - if (avalue != null && avalue.trim().length() > 0) { - Object attr = OntologyHelper. - instantiateAttrExternal(avalue, aname, - itemManager.getOntology(). - getClass(itemClassName, "item"), - coreManager, - dateTimeFormatter, - dateFormatter); - if (attr instanceof DisplayError) { - ((DisplayError) attr). - setEnvironment(aname - + ("CUSTOM")); - errors.add(attr); - } else if (attr != null) { - logger.debug("Instantiatiated custom attribute: " - + aname + attr.toString()); - result.put(aname, attr); - } else { - logger.info("Null returned while instantiating custom " - + "attribute: " + aname); - } - } - } // End of handling custom attributes + buildItemAttrsMap(currentParam, input, prequest, itemClassName, + dateTimeFormatter, dateFormatter, + result, errors); } // End of going through all attributes - // Do additional checks + + // go through all additional parameters (if any) + if (attrsmap != null) { + it = attrsmap.keySet().iterator(); + while (it.hasNext()) { + String aname = (String)it.next(); + String avalue = (String)attrsmap.get(aname); + buildItemAttrsMap(aname, avalue, prequest, itemClassName, + dateTimeFormatter, dateFormatter, + result, errors); + } + } + + // do additional checks Map additionalCheck = OntologyHelper. checkAttrsAgainstOntology(result, itemManager.getOntology(). getClass(itemClassName, "item"), @@ -748,6 +721,92 @@ } } + private Map buildItemAttrsMap(PortalRequest prequest) + { + return buildItemAttrsMap(prequest, null); + } + + private void buildItemAttrsMap(String currentParam, String input, + PortalRequest prequest, + String itemClassName, + DateFormat dateTimeFormatter, + DateFormat dateFormatter, + Map result, + Set errors) + { + if (input == null) input = ""; + if (shouldProcessAttribute(currentParam, input, prequest)) { + // this attribute should be processed + Object attr = + OntologyHelper. + instantiateAttrExternal(input, + currentParam, + itemManager.getOntology(). + getClass(itemClassName, "item"), + coreManager, + dateTimeFormatter, dateFormatter); + if (attr instanceof DisplayError) { // attr is a single error + ((DisplayError)attr).setEnvironment(currentParam); + errors.add(attr); + } else if (attr instanceof Set + && ((Set) attr).iterator().next() + instanceof DisplayError) { + // attr has possibly multiple errors + Iterator it2 = ((Set) attr).iterator(); + while (it2.hasNext()) { + DisplayError next = (DisplayError) it2.next(); + next.setEnvironment(currentParam); + errors.add(next); + } + } else if (attr != null) { // Item was correctly instantiated + if (prequest.getRequestParameter(currentParam + "_lang") + == null) { // No multiple language + result.put(currentParam, attr); + } else { // Multiple language + result.put(currentParam + "_" + + prequest. + getRequestParameter(currentParam + + "_lang"), attr); + } + } else { + // Something went wrong which is not covered with an + // error message. Looks like a greater problem. + logger.warn("cannot handle attribute: " + currentParam); + } + } else if (currentParam.matches("_customAname.*")) { + // Process custom attributes + String number = + currentParam.substring(currentParam.lastIndexOf("_"), + currentParam.length()); + String aname = prequest.getRequestParameter(currentParam); + String avalue = prequest. + getRequestParameter("_customAvalue" + number); + if (avalue != null && avalue.trim().length() > 0) { + Object attr = OntologyHelper. + instantiateAttrExternal(avalue, aname, + itemManager.getOntology(). + getClass(itemClassName, "item"), + coreManager, + dateTimeFormatter, + dateFormatter); + if (attr instanceof DisplayError) { + ((DisplayError) attr). + setEnvironment(aname + + ("CUSTOM")); + errors.add(attr); + } else if (attr != null) { + logger.debug("Instantiatiated custom attribute: " + + aname + attr.toString()); + result.put(aname, attr); + } else { + logger.info("Null returned while instantiating custom " + + "attribute: " + aname); + } + } + } // End of handling custom attributes + } + + /** * This method decides whether a request parameter is used for further * processing (buildItemAttrsMap) or not. @@ -895,4 +954,54 @@ this.itemManager=im; } + + /** + * Get an uploaded file and store it in the upload directory + */ + protected String uploadFile(FileItem uploadfile, String fname) + { + String ufilename = uploadfile.getName(); + if (ufilename == null || ufilename.length()<1) return null; + String filesuffix = ufilename; + if (filesuffix!=null && filesuffix.length()>0) { + int pos = filesuffix.lastIndexOf("."); + if (pos >= 0) { + filesuffix = filesuffix.substring(pos); + } else { + filesuffix = ""; + } + } + + // fname is relative to webspace.dir ! + + String fnamefull = fname; + if (fnamefull.endsWith(File.separator)) { + fname += ufilename; + fnamefull += ufilename; + } + String pdirname = coreManager.getProperty("portal.webspace.dir"); + if (pdirname!=null && pdirname.length()>0) + fnamefull = pdirname + File.separator + fnamefull; + else + fnamefull = "." + File.separator + fnamefull; + int pos = fnamefull.lastIndexOf("."); + if (!(pos > -1 && pos > fnamefull.lastIndexOf(File.separator))) { + fnamefull = fnamefull + filesuffix; + fname = fname + filesuffix; + } + + logger.info("Saving uploaded file to "+fnamefull); + + File file = new File(fnamefull); + try { + uploadfile.write(file); + } catch (Exception e) { + logger.debug("Failed uploading file ... "+e.toString()); + return null; + } + + String uri = "/" + fname; + return uri; + } + } |