001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.operations.daemon.messages;
019
020import java.util.logging.Level;
021
022import org.syncany.operations.daemon.messages.api.ManagementRequest;
023import org.syncany.operations.daemon.messages.api.ManagementRequestHandler;
024import org.syncany.operations.daemon.messages.api.Response;
025import org.syncany.operations.update.UpdateOperation;
026import org.syncany.operations.update.UpdateOperationResult;
027
028public class UpdateManagementRequestHandler extends ManagementRequestHandler {
029        public UpdateManagementRequestHandler() {
030                // Nothing
031        }
032
033        @Override
034        public Response handleRequest(final ManagementRequest request) {
035                final UpdateManagementRequest concreteRequest = (UpdateManagementRequest) request;              
036                logger.log(Level.SEVERE, "Executing UpdateOperation for action " + concreteRequest.getOptions().getAction() + " ...");
037
038                Thread updateThread = new Thread(new Runnable() {
039                        @Override
040                        public void run() {
041                                try {                                   
042                                        UpdateOperation updateOperation = new UpdateOperation(null, concreteRequest.getOptions());
043                                        UpdateOperationResult operationResult = updateOperation.execute();
044                                        
045                                        switch (operationResult.getResultCode()) {
046                                        case OK:
047                                                eventBus.post(new UpdateManagementResponse(UpdateManagementResponse.OK, operationResult, request.getId()));
048                                                break;
049
050                                        case NOK:
051                                                eventBus.post(new UpdateManagementResponse(UpdateManagementResponse.NOK_FAILED_UNKNOWN, operationResult, request.getId()));
052                                                break;
053                                        }
054                                }
055                                catch (Exception e) {
056                                        logger.log(Level.WARNING, "Error executing update management request.", e);
057                                        eventBus.post(new UpdateManagementResponse(UpdateManagementResponse.NOK_OPERATION_FAILED, new UpdateOperationResult(), request.getId()));
058                                }
059                        }
060                }, "UpdRq/" + concreteRequest.getOptions().getAction());
061
062                updateThread.start();
063                
064                return null;
065        }
066}