001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.metamodel.adapter.version;
021
022import org.apache.isis.core.commons.exceptions.IsisException;
023import org.apache.isis.core.metamodel.adapter.oid.Oid;
024import org.apache.isis.core.metamodel.adapter.oid.OidMarshaller;
025
026public class ConcurrencyException extends IsisException {
027    
028    private static final long serialVersionUID = 1L;
029
030    private static String buildMessage(String currentUser, Oid oid, Version staleVersion, Version datastoreVersion) {
031        
032        final StringBuilder buf = new StringBuilder();
033        buf.append(currentUser != null? currentUser + " " : "");
034        buf.append(" attempted to update ").append(oid.enStringNoVersion(getOidMarshaller()));
035        buf.append(", however this object has since been modified");
036        if(datastoreVersion.getUser() != null) {
037            buf.append(" by ").append(datastoreVersion.getUser());
038        }
039        if(datastoreVersion.getTime() != null) {
040            buf.append(" at ").append(datastoreVersion.getTime());
041        }
042        buf.append(" [").append(staleVersion.getSequence()).append(" vs ").append(datastoreVersion.getSequence()).append("]");
043        
044        return buf.toString();
045    }
046
047    private final Oid oid;
048
049    public ConcurrencyException(final String currentUser, final Oid oid, final Version staleVersion, final Version datastoreVersion) {
050        this(buildMessage(currentUser, oid, staleVersion, datastoreVersion), oid);
051    }
052
053    public ConcurrencyException(final String message, final Oid oid) {
054        super(message);
055        this.oid = oid;
056    }
057
058    public Oid getOid() {
059        return oid;
060    }
061    
062    private static OidMarshaller getOidMarshaller() {
063        return new OidMarshaller();
064    }
065
066}