001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * *** 008 * 009 * Community License: GPL 3.0 010 * 011 * This file is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as published 013 * by the Free Software Foundation, either version 3 of the License, 014 * or (at your option) any later version. 015 * 016 * This file is distributed in the hope that it will be useful, but 017 * WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * 024 * *** 025 * 026 * Available Commercial License: GraniteDS SLA 1.0 027 * 028 * This is the appropriate option if you are creating proprietary 029 * applications and you are not prepared to distribute and share the 030 * source code of your application under the GPL v3 license. 031 * 032 * Please visit http://www.granitedataservices.com/license for more 033 * details. 034 */ 035package org.granite.client.tide.data; 036 037import org.granite.client.messaging.messages.responses.FaultMessage; 038import org.granite.client.messaging.messages.responses.FaultMessage.Code; 039import org.granite.client.tide.Context; 040import org.granite.client.tide.server.ExceptionHandler; 041import org.granite.client.tide.server.TideFaultEvent; 042import org.granite.logging.Logger; 043 044/** 045 * @author William DRAI 046 */ 047public class OptimisticLockExceptionHandler implements ExceptionHandler { 048 049 private static Logger log = Logger.getLogger("org.granite.tide.data.OptimisticLockExceptionHandler"); 050 051 052 @Override 053 public boolean accepts(FaultMessage emsg) { 054 return emsg.getCode().equals(Code.OPTIMISTIC_LOCK); 055 } 056 057 @Override 058 public void handle(Context context, FaultMessage emsg, TideFaultEvent faultEvent) { 059 log.debug("optimistic lock error received %s", emsg.toString()); 060 061 String receivedSessionId = faultEvent.getServerSession().getSessionId() + "_error"; 062 Object entity = (emsg.getExtended() != null && emsg.getExtended().get("entity") != null) ? emsg.getExtended().get("entity") : null; 063 064 // Received entity should be the correct version from the database 065 if (entity != null) 066 context.getEntityManager().mergeExternalData(faultEvent.getServerSession(), entity, null, receivedSessionId, null, null); 067 } 068 }