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.progmodel.facets.members.disabled; 021 022import com.google.common.base.Strings; 023 024import org.apache.isis.applib.annotation.When; 025import org.apache.isis.applib.annotation.Where; 026import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 027import org.apache.isis.core.metamodel.facetapi.FacetHolder; 028 029public class DisabledFacetImpl extends DisabledFacetAbstract { 030 031 private final String reason; 032 033 public DisabledFacetImpl(final When when, final Where where, final FacetHolder holder) { 034 this(when, where, null, holder); 035 } 036 037 public DisabledFacetImpl(final When when, final Where where, final String reason, final FacetHolder holder) { 038 super(when, where, holder); 039 this.reason = reason; 040 } 041 042 @Override 043 public String disabledReason(final ObjectAdapter targetAdapter) { 044 if (when() == When.ALWAYS) { 045 return disabledReasonElse("Always disabled"); 046 } else if (when() == When.NEVER) { 047 return null; 048 } 049 050 // remaining tests depend upon the actual target in question 051 if (targetAdapter == null) { 052 return null; 053 } 054 055 if (when() == When.UNTIL_PERSISTED) { 056 return targetAdapter.isTransient() ? disabledReasonElse("Disabled until persisted") : null; 057 } else if (when() == When.ONCE_PERSISTED) { 058 return targetAdapter.representsPersistent() ? disabledReasonElse("Disabled once persisted") : null; 059 } 060 return null; 061 } 062 063 private String disabledReasonElse(final String defaultReason) { 064 return !Strings.isNullOrEmpty(reason) ? reason : defaultReason; 065 } 066 067 /** 068 * Not API... the reason as defined in subclass 069 */ 070 public String getReason() { 071 return reason; 072 } 073 074}