View Javadoc
1   /*
2    * Copyright 2014 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.application.views.pageclicklistener;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
25  import com.hack23.cia.service.api.action.user.SearchDocumentRequest;
26  import com.hack23.cia.service.api.action.user.SearchDocumentResponse;
27  import com.vaadin.ui.Button.ClickEvent;
28  import com.vaadin.ui.Button.ClickListener;
29  import com.vaadin.ui.Notification;
30  
31  /**
32   * The Class SearchDocumentClickListener.
33   */
34  public final class SearchDocumentClickListener implements ClickListener {
35  
36  	/** The Constant ERROR_MESSAGE. */
37  	private static final String ERROR_MESSAGE = "Error message";
38  
39  	/** The Constant SEARCH_FAILED. */
40  	private static final String SEARCH_FAILED = "Search failed";
41  
42  	/** The Constant LOG_MSG_SEARCH_DOCUMENT_FAILURE. */
43  	private static final String LOG_MSG_SEARCH_DOCUMENT_FAILURE = "SearchDocument {} failure";
44  
45  	/** The Constant LOG_MSG_SEARCH_DOCUMENT. */
46  	private static final String LOG_MSG_SEARCH_DOCUMENT = "SearchDocument {}";
47  
48  	/** The Constant SEARCH_SUCCESS. */
49  	private static final String SEARCH_SUCCESS = "Search success";
50  
51  	/** The Constant serialVersionUID. */
52  	private static final long serialVersionUID = 1L;
53  
54  	/** The Constant LOGGER. */
55  	private static final Logger LOGGER = LoggerFactory.getLogger(SearchDocumentClickListener.class);
56  
57  	/** The reqister request. */
58  	private final SearchDocumentRequest reqisterRequest;
59  
60  	/** The response handler. */
61  	private final SearchDocumentResponseHandler responseHandler;
62  
63  	/**
64  	 * Instantiates a new search document click listener.
65  	 *
66  	 * @param reqisterRequest
67  	 *            the reqister request
68  	 * @param responseHandler
69  	 *            the response handler
70  	 */
71  	public SearchDocumentClickListener(final SearchDocumentRequest reqisterRequest, final SearchDocumentResponseHandler responseHandler) {
72  		this.reqisterRequest = reqisterRequest;
73  		this.responseHandler = responseHandler;
74  	}
75  
76  	@Override
77  	public void buttonClick(final ClickEvent event) {
78  		final SearchDocumentResponse response = (SearchDocumentResponse) ApplicationMangerAccess.getApplicationManager().service(reqisterRequest);
79  		if (ServiceResult.SUCCESS == response.getResult()) {
80  			LOGGER.info(LOG_MSG_SEARCH_DOCUMENT,reqisterRequest.getSearchExpression());
81  			Notification.show(SEARCH_SUCCESS,
82  	                  "Found :" + response.getResultElement().size(),
83  	                  Notification.Type.HUMANIZED_MESSAGE);
84  			responseHandler.handle(response);
85  
86  		} else {
87  			Notification.show(SEARCH_FAILED,
88  	                  ERROR_MESSAGE,
89  	                  Notification.Type.WARNING_MESSAGE);
90  			LOGGER.info(LOG_MSG_SEARCH_DOCUMENT_FAILURE,reqisterRequest.getSearchExpression());
91  		}
92  	}
93  }