001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.contact.server.graphql;
018
019import com.echothree.model.control.contact.common.ContactMechanismTypes;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
022import com.echothree.model.data.contact.server.entity.ContactMechanism;
023import com.echothree.model.data.contact.server.entity.ContactMechanismDetail;
024import com.echothree.util.server.persistence.Session;
025import graphql.annotations.annotationTypes.GraphQLDescription;
026import graphql.annotations.annotationTypes.GraphQLField;
027import graphql.annotations.annotationTypes.GraphQLName;
028import graphql.annotations.annotationTypes.GraphQLNonNull;
029import graphql.schema.DataFetchingEnvironment;
030
031@GraphQLDescription("contact mechanism object")
032@GraphQLName("ContactMechanism")
033public class ContactMechanismObject
034        extends BaseEntityInstanceObject {
035    
036    private final ContactMechanism contactMechanism; // Always Present
037    
038    public ContactMechanismObject(ContactMechanism contactMechanism) {
039        super(contactMechanism.getPrimaryKey());
040        
041        this.contactMechanism = contactMechanism;
042    }
043
044    private ContactMechanismDetail contactMechanismDetail; // Optional, use getContactMechanismDetail()
045    
046    private ContactMechanismDetail getContactMechanismDetail() {
047        if(contactMechanismDetail == null) {
048            contactMechanismDetail = contactMechanism.getLastDetail();
049        }
050        
051        return contactMechanismDetail;
052    }
053    
054    @GraphQLField
055    @GraphQLDescription("contact mechanism name")
056    @GraphQLNonNull
057    public String getContactMechanismName() {
058        return getContactMechanismDetail().getContactMechanismName();
059    }
060
061    @GraphQLField
062    @GraphQLDescription("contact mechanism type")
063    @GraphQLNonNull
064    public ContactMechanismTypeObject getContactMechanismType() {
065        return new ContactMechanismTypeObject(getContactMechanismDetail().getContactMechanismType());
066    }
067
068    @GraphQLField
069    @GraphQLDescription("allow solicitation")
070    @GraphQLNonNull
071    public boolean getAllowSolicitation() {
072        return getContactMechanismDetail().getAllowSolicitation();
073    }
074
075    private ContactMechanismTypes contactMechanismTypeEnum = null; // Optional, use getContactMechanismTypeEnum()
076
077    protected ContactMechanismTypes getContactMechanismTypeEnum() {
078        if(contactMechanismTypeEnum == null) {
079            contactMechanismTypeEnum = ContactMechanismTypes.valueOf(getContactMechanismDetail().getContactMechanismType().getContactMechanismTypeName());
080        }
081
082        return contactMechanismTypeEnum;
083    }
084
085    @GraphQLField
086    @GraphQLDescription("contact mechanism")
087    public ContactMechanismInterface getContactMechanism(final DataFetchingEnvironment env) {
088        var contactControl = Session.getModelController(ContactControl.class);
089
090        return switch(getContactMechanismTypeEnum()) {
091            case EMAIL_ADDRESS -> new ContactEmailAddressObject(basePrimaryKey, contactControl.getContactEmailAddress(contactMechanism));
092            case POSTAL_ADDRESS -> new ContactPostalAddressObject(basePrimaryKey, contactControl.getContactPostalAddress(contactMechanism));
093            case TELECOM_ADDRESS -> new ContactTelephoneObject(basePrimaryKey, contactControl.getContactTelephone(contactMechanism));
094            case WEB_ADDRESS -> new ContactWebAddressObject(basePrimaryKey, contactControl.getContactWebAddress(contactMechanism));
095            default -> null; // Leave contactMechanismInterface null
096        };
097    }
098
099}