| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
package com.buckosoft.PicMan.web; |
| 9 | |
|
| 10 | |
import java.util.HashMap; |
| 11 | |
import java.util.Map; |
| 12 | |
|
| 13 | |
import javax.servlet.http.HttpServletRequest; |
| 14 | |
import javax.servlet.http.HttpServletResponse; |
| 15 | |
|
| 16 | |
import org.apache.commons.logging.Log; |
| 17 | |
import org.apache.commons.logging.LogFactory; |
| 18 | |
import org.springframework.validation.BindException; |
| 19 | |
import org.springframework.web.servlet.ModelAndView; |
| 20 | |
|
| 21 | |
import com.buckosoft.BSAccount.web.BSAccountSimpleFormController; |
| 22 | |
import com.buckosoft.BSAccount.web.BSAccountUserWebSession; |
| 23 | |
import com.buckosoft.PicMan.db.DatabaseFacade; |
| 24 | |
import com.buckosoft.PicMan.domain.Root; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class RootManFormController extends BSAccountSimpleFormController { |
| 32 | |
private final static boolean DEBUG = false; |
| 33 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 34 | |
|
| 35 | |
private DatabaseFacade dbf; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 0 | public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; } |
| 41 | |
|
| 42 | |
|
| 43 | 0 | public RootManFormController() { |
| 44 | 0 | setSessionForm(true); |
| 45 | 0 | setValidateOnBinding(false); |
| 46 | 0 | setCommandName("rootManForm"); |
| 47 | 0 | setFormView("RootManForm"); |
| 48 | 0 | } |
| 49 | |
|
| 50 | |
protected Object formBackingObject(HttpServletRequest request) throws Exception { |
| 51 | 0 | BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request); |
| 52 | |
if (DEBUG) |
| 53 | |
logger.info("formBackingObject RootupSystemForm with session " + userWebSession); |
| 54 | 0 | RootManForm rmf = new RootManForm(); |
| 55 | |
|
| 56 | 0 | rmf.setRoots(dbf.getRoots()); |
| 57 | 0 | rmf.setUserWebSession(userWebSession); |
| 58 | 0 | rmf.setEditRoot(null); |
| 59 | 0 | String v = request.getParameter("edit"); |
| 60 | 0 | if (v != null) { |
| 61 | 0 | int rid = Integer.parseInt(v); |
| 62 | 0 | rmf.setEditRoot(dbf.getRoot(rid)); |
| 63 | |
} |
| 64 | |
|
| 65 | 0 | return(rmf); |
| 66 | |
} |
| 67 | |
|
| 68 | |
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) |
| 69 | |
throws Exception { |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception { |
| 73 | 0 | BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request); |
| 74 | 0 | Map<String, Object> myModel = new HashMap<String, Object>(); |
| 75 | 0 | myModel.put("userWebSession", userWebSession); |
| 76 | |
if (DEBUG) |
| 77 | |
logger.info("referenceData RootupSystemForm with session " + userWebSession); |
| 78 | |
|
| 79 | 0 | return myModel; |
| 80 | |
} |
| 81 | |
|
| 82 | |
protected ModelAndView onSubmit( |
| 83 | |
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) |
| 84 | |
throws Exception { |
| 85 | |
|
| 86 | 0 | RootManForm rmf = (RootManForm) command; |
| 87 | |
|
| 88 | 0 | checkActiveFlags(request); |
| 89 | |
|
| 90 | 0 | boolean success = true; |
| 91 | 0 | logger.info("Root to delete = '" + rmf.getRootToDelete() + "'"); |
| 92 | 0 | if (rmf.getRootToDelete() != null && rmf.getRootToDelete().length() > 0) { |
| 93 | 0 | Root root = new Root(); |
| 94 | 0 | root.setName(rmf.getRootToDelete()); |
| 95 | 0 | dbf.deleteRoot(root); |
| 96 | |
} |
| 97 | 0 | if (rmf.getNewRoot() != null |
| 98 | 0 | && rmf.getNewRoot().getName() != null |
| 99 | 0 | && rmf.getNewRoot().getName().length() > 0) { |
| 100 | |
if (DEBUG) |
| 101 | |
logger.info("Adding new root '" + rmf.getNewRoot().getName() + "' desc: '" + rmf.getNewRoot().getPath() + "'"); |
| 102 | 0 | dbf.addRoot(rmf.getNewRoot()); |
| 103 | |
} |
| 104 | 0 | if (rmf.getEditRoot() != null) { |
| 105 | |
if (DEBUG) |
| 106 | |
logger.info("Updating root " + rmf.getEditRoot().getRid()); |
| 107 | 0 | dbf.storeRoot(rmf.getEditRoot()); |
| 108 | |
} |
| 109 | 0 | if (success) { |
| 110 | 0 | response.sendRedirect("home.do"); |
| 111 | 0 | return(null); |
| 112 | |
} |
| 113 | |
else |
| 114 | 0 | return super.onSubmit(request, response, command, errors); |
| 115 | |
} |
| 116 | |
|
| 117 | |
private void checkActiveFlags(HttpServletRequest request) { |
| 118 | 0 | int count = dbf.getRootCount(); |
| 119 | 0 | for (int i=1; i<=count; i++) { |
| 120 | 0 | Root root = dbf.getRoot(i); |
| 121 | 0 | boolean b = false; |
| 122 | 0 | if (request.getParameter("active_" + i) != null) |
| 123 | 0 | b = true; |
| 124 | 0 | if (root.isActive() != b) { |
| 125 | 0 | root.setActive(b); |
| 126 | 0 | dbf.storeRoot(root); |
| 127 | |
} |
| 128 | |
} |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
} |
| 132 | |
|