8 require File.join(File.dirname(__FILE__), '../../WEB-INF/lib/ruby/web', 'web_core')
\r
10 include_class 'org.spirit.form.BotListPostListingForm'
\r
11 include_class 'org.spirit.bean.impl.BotListPostListing'
\r
12 include_class 'org.spirit.bean.impl.BotListCityListing'
\r
13 include_class 'org.spirit.bean.impl.BotListPostImageMetadata'
\r
14 include_class 'org.spirit.bean.impl.BotListCalculatorVerification'
\r
16 include_class 'org.spirit.util.BotListSessionManager'
\r
17 include_class 'org.spirit.util.BotListUniqueId'
\r
19 include_class 'org.spirit.spring.validate.BotListPostListingValidator'
\r
21 include_class('java.io.File') { 'JFile' }
\r
22 include_class('java.util.ArrayList') { 'JArrayList' }
\r
23 include_class 'org.apache.commons.logging.Log'
\r
24 include_class 'org.apache.commons.logging.LogFactory'
\r
26 include_class 'org.spirit.util.markdown.BotListMarkUtil'
\r
28 class CreateListingController
\r
30 def initialize(controller)
\r
31 @controller = controller
\r
32 @daohelpercity = controller.cityListingDao
\r
33 @daohelperpost = controller.postListingDao
\r
34 @log = LogFactory::getLog("org.jruby")
\r
37 def getCitySessionInfo(request)
\r
38 cur_session = request.getSession(false)
\r
39 citybean = cur_session.getAttribute(BotListSessionManager::CURRENT_CITY_SETTING)
\r
40 sectionbean = cur_session.getAttribute(BotListSessionManager::CURRENT_SECTION_SETTING)
\r
41 return [citybean, sectionbean]
\r
45 def getModel(request)
\r
46 postListing = BotListPostListingForm.new()
\r
47 postListing.viewName = nil
\r
49 # Also, check the username for cookie setting
\r
50 cookieManager = BotListWebCore::WebCookieManager.new(request)
\r
51 cookieManager.getCreateEmail(postListing, nil)
\r
53 # Get the previous session objects
\r
54 sessionTuple = getCitySessionInfo(request)
\r
55 if not sessionTuple[0] or not sessionTuple[1]
\r
57 'viewName' => 'errorInvalidView'
\r
61 postListing.cityListing = sessionTuple[0]
\r
62 postListing.postSection = sessionTuple[1]
\r
64 calc = generateCalcVerification()
\r
65 postListing.firstInput = calc.firstInput
\r
66 postListing.secondInput = calc.secondInput
\r
67 postListing.solution = calc.solution
\r
69 cur_session = request.getSession(false)
\r
70 # Check the previous session object for the previous value
\r
71 # used in conjunction with onSubmit()
\r
72 prev_calc = cur_session.getAttribute(BotListSessionManager::CALC_VERIFY_OBJECT)
\r
74 postListing.prevSolution = prev_calc.solution
\r
76 cur_session.setAttribute(BotListSessionManager::CALC_VERIFY_OBJECT, calc)
\r
78 # Also set the validator
\r
79 @controller.setValidator(BotListPostListingValidator.new())
\r
85 # Transform the form data to the
\r
87 def transformFormData(form)
\r
88 listing = BotListPostListing.new
\r
89 listing.email = form.email
\r
91 # Perform markdown html conversion on form message
\r
92 markdown = BotListMarkUtil.new
\r
93 listing.message = markdown.convert(form.message)
\r
94 listing.title = form.title
\r
95 listing.age = form.age
\r
96 listing.category = 'general'
\r
98 listing.mainUrl = form.mainUrl
\r
101 listing.keywords = form.keywords
\r
107 # Generate the calculator verification, input and solution
\r
108 def generateCalcVerification()
\r
109 calc = BotListCalculatorVerification.new
\r
110 calc.firstInput = rand 30
\r
111 calc.secondInput = rand 10
\r
112 calc.solution = calc.firstInput + calc.secondInput
\r
117 # Perform the download to the server and create the
\r
118 # data bean object.
\r
119 def uploadCurFile(request, form, entity)
\r
120 uploadDir = @controller.fileUploadUtil.uploadDir
\r
121 curFilename = entity.originalFilename
\r
123 uid = BotListUniqueId::getUniqueId()
\r
124 newFilename = "img#{uid}.jpg"
\r
126 fileUpload = JFile.new("#{uploadDir}/#{newFilename}")
\r
127 entity.transferTo(fileUpload)
\r
129 # Create the image metadata bean
\r
130 metadata = BotListPostImageMetadata.new
\r
131 metadata.imageFilesize = entity.size
\r
132 metadata.imageFilename = newFilename
\r
133 metadata.imageOriginalname = curFilename
\r
138 # Create the image hibernate record
\r
139 def createListingImage(listing, imageTuple)
\r
142 for img in imageTuple
\r
144 if not listing.images
\r
145 listing.images = JArrayList.new
\r
147 img.adlistId = listing.get_id
\r
148 listing.images.add(img)
\r
152 # End of if - image
\r
157 # Upload an image file
\r
158 def uploadImageFile(request, form)
\r
161 imageTuple[0] = nil
\r
162 imageTuple[1] = nil
\r
163 # Attempt to upload the first file
\r
164 if form.uploadFilenameFirst and not form.uploadFilenameFirst.isEmpty()
\r
165 entity = form.uploadFilenameFirst
\r
166 curFilename = entity.originalFilename
\r
167 if curFilename.downcase =~ /\.jpg$/
\r
168 metadata = uploadCurFile(request, form, entity)
\r
169 imageTuple[0] = metadata
\r
171 @log.info("User attempting to upload non-jpeg file #{curFilename}")
\r
175 if form.uploadFilenameSecond and not form.uploadFilenameSecond.isEmpty()
\r
176 entity = form.uploadFilenameSecond
\r
177 curFilename = entity.originalFilename
\r
178 if curFilename.downcase =~ /\.jpg$/
\r
179 metadata = uploadCurFile(request, form, entity)
\r
180 imageTuple[1] = metadata
\r
182 @log.info("User attempting to upload non-jpeg file #{curFilename}")
\r
185 # Create the image bean
\r
186 imageTuple[1] = metadata
\r
193 # Processed when the form is submitted,
\r
194 # see the controller 'processFormSubmission()' method
\r
195 def onSubmit(request, response, form, errors)
\r
197 # Also, check the username for cookie setting
\r
198 cookieManager = BotListWebCore::WebCookieManager.new(request)
\r
199 cookieManager.getCreateEmail(form, response)
\r
201 # Check the errors first.
\r
202 if errors.getErrorCount() > 0
\r
203 # Revert back to the entry screen
\r
204 form.viewName = "create_listing"
\r
208 # Get the city object from the session
\r
209 cur_session = request.getSession(false)
\r
210 citybean = cur_session.getAttribute(BotListSessionManager::CURRENT_CITY_SETTING)
\r
211 cityId = citybean.get_id
\r
213 # Upload image file
\r
214 imageTuple = uploadImageFile(request, form)
\r
216 # Transfer data object bean
\r
217 listing = transformFormData(form)
\r
219 # Get the bean from the DB as opposed to off the session table
\r
220 sessionFactory = @daohelpercity.getSessionFactory()
\r
221 hbm_session = sessionFactory.openSession()
\r
222 tx = hbm_session.beginTransaction()
\r
223 city = hbm_session.load("org.spirit.bean.impl.BotListCityListing", cityId, nil)
\r
224 listing.cityId = cityId
\r
225 listing.sectionId = form.postSection.get_id
\r
226 city.listings.add(listing)
\r
229 tx = hbm_session.beginTransaction()
\r
230 createListingImage(listing, imageTuple)
\r
232 hbm_session.close()
\r
234 # Navigate to the confirmation page
\r
235 form.viewName = "createconfirm"
\r
241 CreateListingController.new($controller)
\r
243 ## End of Script ##
\r