View Javadoc
1   /******************************************************************************
2    * DatabaseConverterFormController.java - The Spring controller to alter my running production database.
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2006 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.web;
9   
10  import java.util.HashMap;
11  import java.util.Iterator;
12  import java.util.List;
13  import java.util.Map;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.springframework.validation.BindException;
21  import org.springframework.web.servlet.ModelAndView;
22  
23  import com.buckosoft.BSAccount.web.BSAccountSimpleFormController;
24  import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
25  import com.buckosoft.PicMan.business.PicManFacade;
26  import com.buckosoft.PicMan.db.DatabaseFacade;
27  import com.buckosoft.PicMan.domain.Pic;
28  import com.buckosoft.PicMan.domain.Root;
29  
30  /**
31   * The Spring controller to alter my running production database.
32   * Sometimes, it is easier to throw some code in here then it is to try to 
33   * fix the database with phpMyAdmin.
34   * @author Dick Balaska
35   * @since 2006/09/29
36   */
37  public class DatabaseConverterFormController extends BSAccountSimpleFormController {
38  
39  //	private static final boolean DEBUG = true;
40      protected final Log logger = LogFactory.getLog(getClass());
41  
42      private DatabaseFacade	dbf;
43      private PicManFacade	pmf;
44  
45  	public DatabaseConverterFormController() {
46  		setSessionForm(true);
47  		setValidateOnBinding(true);
48  		setCommandName("databaseConverterForm");
49  		setFormView("DatabaseConverterForm");
50  	}
51  
52  	public void setPicMan(PicManFacade pmf) {
53  		this.pmf = pmf;
54  		pmf = this.pmf;		// to remove an "unread locally" warning.
55  	}
56  
57  	public void setDatabase(DatabaseFacade dbf) {
58  		this.dbf = dbf;
59  	}
60  
61  	protected Object formBackingObject(HttpServletRequest request) throws Exception {
62      	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
63  		DatabaseConverterForm dcf = new DatabaseConverterForm();
64  
65  		dcf.setUserWebSession(userWebSession);
66  		dcf.setRoots(dbf.getRoots());
67  
68  		String v;
69  		v = request.getParameter("updSize");
70  		if (v != null) {
71  			dbf.getSystem().setUpdatePicSize(true);
72  			dcf.setMessage("Queued updating sizes");
73  		}
74  		v = request.getParameter("mlbFilters");
75  		if (v != null) {
76  			dbf.getSystem().setUpdateMlbFilters(true);
77  			dcf.setMessage("Queued MlbFilterFixer");
78  		}
79  		v = request.getParameter("md5sum");
80  		if (v != null) {
81  			dbf.getSystem().setUpdateMD5Sums(true);
82  			dcf.setMessage("Queued UpdateMD5Sums");
83  		}
84  		v = request.getParameter("mosaicVectors");
85  		if (v != null) {
86  			dbf.getSystem().setCalcMosaicVectors(true);
87  			dcf.setMessage("Queued calc mosaic vectors");
88  		}
89  		return(dcf);
90  	}
91  
92  	protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
93  			throws Exception {
94  
95  /*
96  		AccountForm accountForm = (AccountForm) command;
97  		Account account = accountForm.getAccount();
98  
99  		if (request.getParameter("account.listOption") == null) {
100 			account.setListOption(false);
101 		}
102 		if (request.getParameter("account.bannerOption") == null) {
103 			account.setBannerOption(false);
104 		}
105 
106 		errors.setNestedPath("account");
107 		getValidator().validate(account, errors);
108 		errors.setNestedPath("");
109 */
110 	}
111 
112 	protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
113     	BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
114 		Map<String, Object> myModel = new HashMap<String, Object>();
115 		myModel.put("userWebSession", userWebSession);
116 //		if (DEBUG)
117 //			logger.info("referenceData ChainManForm with session " + userWebSession);
118 
119 		return myModel;
120 	}
121 
122 	protected ModelAndView onSubmit(
123 			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
124 			throws Exception {
125 
126 		DatabaseConverterForm dcf = (DatabaseConverterForm) command;
127 
128 		boolean success = true;
129 		// boolean action = false;
130 
131  		logger.info("oldName= '" + dcf.getOldName() + "'");
132  		logger.info("newName= '" + dcf.getNewName() + "'");
133 
134 		if (dcf.getOldName().length() > 0 && dcf.getNewName().length() > 0) {
135 			dcf.setMessage("Moved '" + dcf.getOldName() + "' to root '" + dcf.getNewName() + "'");
136  			logger.info("oldName action");
137  			Root root = dbf.getRoot(dcf.getNewName());
138  			logger.info("rid = " + root.getRid());
139  			List<Pic> pics = dbf.getPics();
140  			Iterator<Pic> iter = pics.iterator();
141  			while (iter.hasNext()) {
142  				Pic pic = iter.next();
143  				logger.info("Pic = " + pic.getName() + " loc: " + pic.getLocation());
144  				if (pic.getLocation().startsWith(dcf.getOldName())) {
145  					pic.setLocation(pic.getLocation().substring(dcf.getOldName().length()));
146  					pic.setRid(root.getRid());
147  					logger.info("Upd = " + pic.getName() + " loc: " + pic.getLocation());
148  					dbf.updatePic(pic);
149  				}
150  			}
151  		}
152  		/*
153 		if (cmf.getChainToDelete().length() > 0) {
154 			Chain chain = new Chain();
155 			chain.setName(cmf.getChainToDelete());
156 			dbf.deleteChain(chain);
157 			action = true;
158 		}
159 		if (!action && cmf.getNewChainName().length() > 0) {
160 			if (DEBUG)
161 				logger.info("Adding new Chain '" + cmf.getNewChainName());
162 			Chain c = new Chain();
163 			c.setName(cmf.getNewChainName());
164 			dbf.storeChain(c);
165 			newChainName = c.getName();
166 			action = true;
167 			
168 		}
169 		if (!action && cmf.getActiveChain() != null) {
170 			Chain c = cmf.getActiveChain();
171 			LinkedList<SetSize> newSetsSizes = parseSetsSizes(request);
172 			c.setSetSizes(newSetsSizes);
173 			dbf.storeChain(c);
174 			action = true;
175 		}
176 		*/
177 		if (success) {
178 			response.sendRedirect("databaseConverter.do");
179 			return(null);
180 		}
181 		else
182 			return super.onSubmit(request, response, command, errors);
183 	}
184 }