001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.control.user.selector.server.command;
018
019import com.echothree.control.user.selector.common.form.CreateSelectorNodeForm;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.core.server.control.CoreControl;
022import com.echothree.model.control.employee.server.control.EmployeeControl;
023import com.echothree.model.control.geo.server.control.GeoControl;
024import com.echothree.model.control.item.server.control.ItemControl;
025import com.echothree.model.control.payment.server.control.PaymentMethodControl;
026import com.echothree.model.control.payment.server.control.PaymentProcessorControl;
027import com.echothree.model.control.selector.common.SelectorNodeTypes;
028import com.echothree.model.control.selector.server.control.SelectorControl;
029import com.echothree.model.control.selector.server.logic.SelectorNodeTypeLogic;
030import com.echothree.model.control.training.server.control.TrainingControl;
031import com.echothree.model.control.vendor.server.control.VendorControl;
032import com.echothree.model.control.workflow.server.control.WorkflowControl;
033import com.echothree.model.data.accounting.server.entity.ItemAccountingCategory;
034import com.echothree.model.data.core.server.entity.ComponentVendor;
035import com.echothree.model.data.core.server.entity.EntityAttribute;
036import com.echothree.model.data.core.server.entity.EntityListItem;
037import com.echothree.model.data.core.server.entity.EntityType;
038import com.echothree.model.data.employee.server.entity.ResponsibilityType;
039import com.echothree.model.data.employee.server.entity.SkillType;
040import com.echothree.model.data.geo.server.entity.GeoCode;
041import com.echothree.model.data.item.server.entity.ItemCategory;
042import com.echothree.model.data.party.common.pk.PartyPK;
043import com.echothree.model.data.party.server.entity.Language;
044import com.echothree.model.data.payment.server.entity.PaymentMethod;
045import com.echothree.model.data.payment.server.entity.PaymentProcessor;
046import com.echothree.model.data.selector.server.entity.Selector;
047import com.echothree.model.data.selector.server.entity.SelectorBooleanType;
048import com.echothree.model.data.selector.server.entity.SelectorKind;
049import com.echothree.model.data.selector.server.entity.SelectorNode;
050import com.echothree.model.data.selector.server.entity.SelectorNodeType;
051import com.echothree.model.data.selector.server.entity.SelectorType;
052import com.echothree.model.data.training.server.entity.TrainingClass;
053import com.echothree.model.data.user.common.pk.UserVisitPK;
054import com.echothree.model.data.vendor.server.entity.ItemPurchasingCategory;
055import com.echothree.model.data.workflow.server.entity.Workflow;
056import com.echothree.model.data.workflow.server.entity.WorkflowStep;
057import com.echothree.util.common.command.BaseResult;
058import com.echothree.util.common.form.ValidationResult;
059import com.echothree.util.common.message.ExecutionErrors;
060import com.echothree.util.common.validation.FieldDefinition;
061import com.echothree.util.common.validation.FieldType;
062import com.echothree.util.server.control.BaseSimpleCommand;
063import com.echothree.util.server.persistence.Session;
064import com.echothree.util.server.validation.Validator;
065import java.util.Arrays;
066import java.util.Collections;
067import java.util.List;
068
069public class CreateSelectorNodeCommand
070        extends BaseSimpleCommand<CreateSelectorNodeForm> {
071    
072    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
073    private final static List<FieldDefinition> booleanFormFieldDefinitions;
074    private final static List<FieldDefinition> entityListItemFormFieldDefinitions;
075    private final static List<FieldDefinition> responsibilityTypeFormFieldDefinitions;
076    private final static List<FieldDefinition> skillTypeFormFieldDefinitions;
077    private final static List<FieldDefinition> trainingClassFormFieldDefinitions;
078    private final static List<FieldDefinition> workflowStepFormFieldDefinitions;
079    private final static List<FieldDefinition> itemCategoryFormFieldDefinitions;
080    private final static List<FieldDefinition> itemAccountingCategoryFormFieldDefinitions;
081    private final static List<FieldDefinition> itemPurchasingCategoryFormFieldDefinitions;
082    private final static List<FieldDefinition> paymentMethodFormFieldDefinitions;
083    private final static List<FieldDefinition> paymentProcessorFormFieldDefinitions;
084    private final static List<FieldDefinition> geoCodeFormFieldDefinitions;
085    
086    static {
087        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
088                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
089                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
090                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
091                new FieldDefinition("SelectorNodeName", FieldType.ENTITY_NAME, true, null, null),
092                new FieldDefinition("IsRootSelectorNode", FieldType.BOOLEAN, true, null, null),
093                new FieldDefinition("SelectorNodeTypeName", FieldType.ENTITY_NAME, true, null, null),
094                new FieldDefinition("Negate", FieldType.BOOLEAN, true, null, null),
095                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
096                ));
097        
098        booleanFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
099                new FieldDefinition("SelectorBooleanTypeName", FieldType.ENTITY_NAME, true, null, null),
100                new FieldDefinition("LeftSelectorNodeName", FieldType.ENTITY_NAME, true, null, null),
101                new FieldDefinition("RightSelectorNodeName", FieldType.ENTITY_NAME, true, null, null)
102                ));
103        
104        entityListItemFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
105                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
106                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
107                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
108                new FieldDefinition("EntityListItemName", FieldType.ENTITY_NAME, true, null, null)
109                ));
110        
111        responsibilityTypeFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
112                new FieldDefinition("ResponsibilityTypeName", FieldType.ENTITY_NAME, true, null, null)
113                ));
114        
115        skillTypeFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
116                new FieldDefinition("SkillTypeName", FieldType.ENTITY_NAME, true, null, null)
117                ));
118        
119        trainingClassFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
120                new FieldDefinition("TrainingClassName", FieldType.ENTITY_NAME, true, null, null)
121                ));
122        
123        workflowStepFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
124                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null),
125                new FieldDefinition("WorkflowStepName", FieldType.ENTITY_NAME, true, null, null)
126                ));
127        
128        itemCategoryFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
129                new FieldDefinition("ItemCategoryName", FieldType.ENTITY_NAME, true, null, null),
130                new FieldDefinition("CheckParents", FieldType.BOOLEAN, true, null, null)
131                ));
132        
133        itemAccountingCategoryFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
134                new FieldDefinition("ItemAccountingCategoryName", FieldType.ENTITY_NAME, true, null, null),
135                new FieldDefinition("CheckParents", FieldType.BOOLEAN, true, null, null)
136                ));
137        
138        itemPurchasingCategoryFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
139                new FieldDefinition("ItemPurchasingCategoryName", FieldType.ENTITY_NAME, true, null, null),
140                new FieldDefinition("CheckParents", FieldType.BOOLEAN, true, null, null)
141                ));
142        
143        paymentMethodFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
144                new FieldDefinition("PaymentMethodName", FieldType.ENTITY_NAME, true, null, null)
145                ));
146        
147        paymentProcessorFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
148                new FieldDefinition("PaymentProcessorName", FieldType.ENTITY_NAME, true, null, null)
149                ));
150        
151        geoCodeFormFieldDefinitions = Collections.unmodifiableList(Arrays.asList(
152                new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, false, null, null),
153                new FieldDefinition("CountryName", FieldType.ENTITY_NAME, false, null, null)
154                ));
155    }
156    
157    /** Creates a new instance of CreateSelectorNodeCommand */
158    public CreateSelectorNodeCommand(UserVisitPK userVisitPK, CreateSelectorNodeForm form) {
159        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
160    }
161    
162    @Override
163    protected ValidationResult validate() {
164        Validator validator = new Validator(this);
165        ValidationResult validationResult = validator.validate(form, FORM_FIELD_DEFINITIONS);
166        
167        if(!validationResult.getHasErrors()) {
168            SelectorNodeType selectorNodeType = SelectorNodeTypeLogic.getInstance().getSelectorNodeTypeByName(this, form.getSelectorNodeTypeName());
169            
170            if(!hasExecutionErrors()) {
171                var selectorNodeTypeEnum = SelectorNodeTypes.valueOf(selectorNodeType.getSelectorNodeTypeName());
172                
173                switch (selectorNodeTypeEnum) {
174                    case BOOLEAN:
175                        validationResult = validator.validate(form, booleanFormFieldDefinitions);
176                        break;
177                    case ENTITY_LIST_ITEM:
178                        validationResult = validator.validate(form, entityListItemFormFieldDefinitions);
179                        break;
180                    case RESPONSIBILITY_TYPE:
181                        validationResult = validator.validate(form, responsibilityTypeFormFieldDefinitions);
182                        break;
183                    case SKILL_TYPE:
184                        validationResult = validator.validate(form, skillTypeFormFieldDefinitions);
185                        break;
186                    case TRAINING_CLASS:
187                        validationResult = validator.validate(form, trainingClassFormFieldDefinitions);
188                        break;
189                    case WORKFLOW_STEP:
190                        validationResult = validator.validate(form, workflowStepFormFieldDefinitions);
191                        break;
192                    case ITEM_CATEGORY:
193                        validationResult = validator.validate(form, itemCategoryFormFieldDefinitions);
194                        break;
195                    case ITEM_ACCOUNTING_CATEGORY:
196                        validationResult = validator.validate(form, itemAccountingCategoryFormFieldDefinitions);
197                        break;
198                    case ITEM_PURCHASING_CATEGORY:
199                        validationResult = validator.validate(form, itemPurchasingCategoryFormFieldDefinitions);
200                        break;
201                    case PAYMENT_METHOD:
202                        validationResult = validator.validate(form, paymentMethodFormFieldDefinitions);
203                        break;
204                    case PAYMENT_PROCESSOR:
205                        validationResult = validator.validate(form, paymentProcessorFormFieldDefinitions);
206                        break;
207                    case GEO_CODE:
208                        validationResult = validator.validate(form, geoCodeFormFieldDefinitions);
209                        break;
210                    default:
211                        break;
212                }
213            }
214        }
215        
216        return validationResult;
217    }
218    
219    private abstract class BaseSelectorNodeType {
220        SelectorControl selectorControl = null;
221        SelectorNodeType selectorNodeType = null;
222        
223        protected BaseSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
224            this.selectorControl = selectorControl;
225            selectorNodeType = selectorControl.getSelectorNodeTypeByName(selectorNodeTypeName);
226            
227            if(selectorNodeType == null) {
228                addExecutionError(ExecutionErrors.UnknownSelectorNodeTypeName.name(), selectorNodeTypeName);
229            }
230        }
231        
232        public abstract void execute(SelectorNode selectorNode, PartyPK partyPK);
233    }
234    
235    private abstract class AccountingSelectorNodeType
236        extends BaseSelectorNodeType {
237        protected AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
238
239        protected AccountingSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
240            super(selectorControl, selectorNodeTypeName);
241        }
242    }
243    
244    private abstract class CoreSelectorNodeType
245        extends BaseSelectorNodeType {
246        protected CoreControl coreControl = getCoreControl();
247
248        protected CoreSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
249            super(selectorControl, selectorNodeTypeName);
250        }
251    }
252    
253    private abstract class EmployeeSelectorNodeType
254        extends BaseSelectorNodeType {
255        protected EmployeeControl employeeControl = Session.getModelController(EmployeeControl.class);
256
257        protected EmployeeSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
258            super(selectorControl, selectorNodeTypeName);
259        }
260    }
261    
262    private abstract class GeoSelectorNodeType
263        extends BaseSelectorNodeType {
264        protected GeoControl geoControl = Session.getModelController(GeoControl.class);
265
266        protected GeoSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
267            super(selectorControl, selectorNodeTypeName);
268        }
269    }
270    
271    private abstract class ItemSelectorNodeType
272        extends BaseSelectorNodeType {
273        protected ItemControl itemControl = Session.getModelController(ItemControl.class);
274
275        protected ItemSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
276            super(selectorControl, selectorNodeTypeName);
277        }
278    }
279
280    private abstract class PaymentProcessorSelectorNodeType
281            extends BaseSelectorNodeType {
282        protected PaymentProcessorControl paymentProcessorControl = Session.getModelController(PaymentProcessorControl.class);
283
284        protected PaymentProcessorSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
285            super(selectorControl, selectorNodeTypeName);
286        }
287    }
288
289    private abstract class PaymentMethodSelectorNodeType
290            extends BaseSelectorNodeType {
291        protected PaymentMethodControl paymentMethodControl = Session.getModelController(PaymentMethodControl.class);
292
293        protected PaymentMethodSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
294            super(selectorControl, selectorNodeTypeName);
295        }
296    }
297
298    private abstract class TrainingSelectorNodeType
299        extends BaseSelectorNodeType {
300        protected TrainingControl trainingControl = Session.getModelController(TrainingControl.class);
301
302        protected TrainingSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
303            super(selectorControl, selectorNodeTypeName);
304        }
305    }
306    
307    private abstract class VendorSelectorNodeType
308        extends BaseSelectorNodeType {
309        protected VendorControl vendorControl = Session.getModelController(VendorControl.class);
310
311        protected VendorSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
312            super(selectorControl, selectorNodeTypeName);
313        }
314    }
315    
316    private abstract class WorkSelectorNodeType
317        extends BaseSelectorNodeType {
318        protected WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
319
320        protected WorkSelectorNodeType(SelectorControl selectorControl, String selectorNodeTypeName) {
321            super(selectorControl, selectorNodeTypeName);
322        }
323    }
324    
325    private class BooleanNodeType
326        extends BaseSelectorNodeType {
327        SelectorBooleanType selectorBooleanType = null;
328        SelectorNode leftSelectorNode = null;
329        SelectorNode rightSelectorNode = null;
330        
331        public BooleanNodeType(SelectorControl selectorControl, Selector selector) {
332            super(selectorControl, SelectorNodeTypes.BOOLEAN.name());
333            
334            if(!hasExecutionErrors()) {
335                String selectorBooleanTypeName = form.getSelectorBooleanTypeName();
336                selectorBooleanType = selectorControl.getSelectorBooleanTypeByName(selectorBooleanTypeName);
337                
338                if(selectorBooleanType != null) {
339                    String leftSelectorNodeName = form.getLeftSelectorNodeName();
340                    leftSelectorNode = selectorControl.getSelectorNodeByName(selector, leftSelectorNodeName);
341                    
342                    if(leftSelectorNode != null) {
343                        String rightSelectorNodeName = form.getRightSelectorNodeName();
344                        rightSelectorNode = selectorControl.getSelectorNodeByName(selector, rightSelectorNodeName);
345                        
346                        if(rightSelectorNode != null) {
347                            if(rightSelectorNode.equals(leftSelectorNode)) {
348                                addExecutionError(ExecutionErrors.IdenticalLeftAndRightSelectorNodes.name(), leftSelectorNodeName, rightSelectorNodeName);
349                            }
350                            
351                            // TODO: Circular Node Check
352                            // TODO: Sensible Root Node Check
353                        } else {
354                            addExecutionError(ExecutionErrors.UnknownRightSelectorNodeName.name(), rightSelectorNodeName);
355                        }
356                    } else {
357                        addExecutionError(ExecutionErrors.UnknownLeftSelectorNodeName.name(), leftSelectorNodeName);
358                    }
359                } else {
360                    addExecutionError(ExecutionErrors.UnknownSelectorBooleanTypeName.name(), selectorBooleanTypeName);
361                }
362            }
363        }
364        
365        @Override
366        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
367            selectorControl.createSelectorNodeBoolean(selectorNode, selectorBooleanType, leftSelectorNode, rightSelectorNode,
368                    partyPK);
369        }
370    }
371    
372    private class EntityListItemNodeType
373        extends CoreSelectorNodeType {
374        EntityListItem entityListItem = null;
375        
376        public EntityListItemNodeType(SelectorControl selectorControl) {
377            super(selectorControl, SelectorNodeTypes.ENTITY_LIST_ITEM.name());
378            
379            if(!hasExecutionErrors()) {
380                String componentVendorName = form.getComponentVendorName();
381                ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName);
382                
383                if(componentVendor != null) {
384                    String entityTypeName = form.getEntityTypeName();
385                    EntityType entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
386                    
387                    if(entityType != null) {
388                        String entityAttributeName = form.getEntityAttributeName();
389                        EntityAttribute entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
390                        
391                        if(entityAttribute != null) {
392                            String entityListItemName = form.getEntityListItemName();
393                            entityListItem = coreControl.getEntityListItemByName(entityAttribute, entityListItemName);
394                            
395                            if(entityListItem == null) {
396                                addExecutionError(ExecutionErrors.UnknownEntityListItemName.name(), entityListItemName);
397                            }
398                        } else {
399                            addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), entityAttributeName);
400                        }
401                    } else {
402                        addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName);
403                    }
404                } else {
405                    addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
406                }
407            }
408        }
409        
410        @Override
411        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
412            selectorControl.createSelectorNodeEntityListItem(selectorNode, entityListItem, partyPK);
413        }
414    }
415    
416    private class ResponsibilityNodeType
417        extends EmployeeSelectorNodeType {
418        private ResponsibilityType responsiblityType = null;
419        
420        public ResponsibilityNodeType(SelectorControl selectorControl) {
421            super(selectorControl, SelectorNodeTypes.RESPONSIBILITY_TYPE.name());
422            
423            if(!hasExecutionErrors()) {
424                String responsiblityTypeName = form.getResponsibilityTypeName();
425                
426                responsiblityType = employeeControl.getResponsibilityTypeByName(responsiblityTypeName);
427                
428                if(responsiblityType == null) {
429                    addExecutionError(ExecutionErrors.UnknownResponsibilityTypeName.name(), responsiblityTypeName);
430                }
431            }
432        }
433        
434        @Override
435        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
436            selectorControl.createSelectorNodeResponsibilityType(selectorNode, responsiblityType, partyPK);
437        }
438    }
439    
440    private class SkillNodeType
441        extends EmployeeSelectorNodeType {
442        private SkillType skillType = null;
443        
444        public SkillNodeType(SelectorControl selectorControl) {
445            super(selectorControl, SelectorNodeTypes.SKILL_TYPE.name());
446            
447            if(!hasExecutionErrors()) {
448                String skillTypeName = form.getSkillTypeName();
449                
450                skillType = employeeControl.getSkillTypeByName(skillTypeName);
451                
452                if(skillType == null) {
453                    addExecutionError(ExecutionErrors.UnknownSkillTypeName.name(), skillTypeName);
454                }
455            }
456        }
457        
458        @Override
459        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
460            selectorControl.createSelectorNodeSkillType(selectorNode, skillType, partyPK);
461        }
462    }
463    
464    private class TrainingClassNodeType
465        extends TrainingSelectorNodeType {
466        private TrainingClass trainingClass = null;
467        
468        public TrainingClassNodeType(SelectorControl selectorControl) {
469            super(selectorControl, SelectorNodeTypes.TRAINING_CLASS.name());
470            
471            if(!hasExecutionErrors()) {
472                String trainingClassName = form.getTrainingClassName();
473                
474                trainingClass = trainingControl.getTrainingClassByName(trainingClassName);
475                
476                if(trainingClass == null) {
477                    addExecutionError(ExecutionErrors.UnknownTrainingClassName.name(), trainingClassName);
478                }
479            }
480        }
481        
482        @Override
483        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
484            selectorControl.createSelectorNodeTrainingClass(selectorNode, trainingClass, partyPK);
485        }
486    }
487    
488    private class WorkflowStepNodeType
489        extends WorkSelectorNodeType {
490        private WorkflowStep workflowStep = null;
491        
492        public WorkflowStepNodeType(SelectorControl selectorControl) {
493            super(selectorControl, SelectorNodeTypes.TRAINING_CLASS.name());
494            
495            if(!hasExecutionErrors()) {
496                String workflowName = form.getWorkflowName();
497                var workflow = workflowControl.getWorkflowByName(workflowName);
498                
499                if(workflow != null) {
500                    String workflowStepName = form.getWorkflowStepName();
501                    
502                    workflowStep = workflowControl.getWorkflowStepByName(workflow, workflowStepName);
503                    
504                    if(workflowStep == null) {
505                        addExecutionError(ExecutionErrors.UnknownWorkflowStepName.name(), workflowStepName);
506                    }
507                } else {
508                    addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName);
509                }
510            }
511        }
512        
513        @Override
514        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
515            selectorControl.createSelectorNodeWorkflowStep(selectorNode, workflowStep, partyPK);
516        }
517    }
518    
519    private class ItemCategoryNodeType
520        extends ItemSelectorNodeType {
521        private ItemCategory itemCategory = null;
522        
523        public ItemCategoryNodeType(SelectorControl selectorControl) {
524            super(selectorControl, SelectorNodeTypes.ITEM_CATEGORY.name());
525            
526            if(!hasExecutionErrors()) {
527                String itemCategoryName = form.getItemCategoryName();
528                
529                itemCategory = itemControl.getItemCategoryByName(itemCategoryName);
530                
531                if(itemCategory == null) {
532                    addExecutionError(ExecutionErrors.UnknownItemCategoryName.name(), itemCategoryName);
533                }
534            }
535        }
536        
537        @Override
538        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
539            Boolean checkParents = Boolean.valueOf(form.getCheckParents());
540            
541            selectorControl.createSelectorNodeItemCategory(selectorNode, itemCategory, checkParents, partyPK);
542        }
543    }
544    
545    private class ItemAccountingCategoryNodeType
546        extends AccountingSelectorNodeType {
547        private ItemAccountingCategory itemAccountingCategory = null;
548        
549        public ItemAccountingCategoryNodeType(SelectorControl selectorControl) {
550            super(selectorControl, SelectorNodeTypes.ITEM_ACCOUNTING_CATEGORY.name());
551            
552            if(!hasExecutionErrors()) {
553                String itemAccountingCategoryName = form.getItemAccountingCategoryName();
554                
555                itemAccountingCategory = accountingControl.getItemAccountingCategoryByName(itemAccountingCategoryName);
556                
557                if(itemAccountingCategory == null) {
558                    addExecutionError(ExecutionErrors.UnknownItemAccountingCategoryName.name(), itemAccountingCategoryName);
559                }
560            }
561        }
562        
563        @Override
564        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
565            Boolean checkParents = Boolean.valueOf(form.getCheckParents());
566            
567            selectorControl.createSelectorNodeItemAccountingCategory(selectorNode, itemAccountingCategory, checkParents, partyPK);
568        }
569    }
570    
571    private class ItemPurchasingCategoryNodeType
572        extends VendorSelectorNodeType {
573        private ItemPurchasingCategory itemPurchasingCategory = null;
574        
575        public ItemPurchasingCategoryNodeType(SelectorControl selectorControl) {
576            super(selectorControl, SelectorNodeTypes.ITEM_PURCHASING_CATEGORY.name());
577            
578            if(!hasExecutionErrors()) {
579                String itemPurchasingCategoryName = form.getItemPurchasingCategoryName();
580                
581                itemPurchasingCategory = vendorControl.getItemPurchasingCategoryByName(itemPurchasingCategoryName);
582                
583                if(itemPurchasingCategory == null) {
584                    addExecutionError(ExecutionErrors.UnknownItemPurchasingCategoryName.name(), itemPurchasingCategoryName);
585                }
586            }
587        }
588        
589        @Override
590        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
591            Boolean checkParents = Boolean.valueOf(form.getCheckParents());
592            
593            selectorControl.createSelectorNodeItemPurchasingCategory(selectorNode, itemPurchasingCategory, checkParents, partyPK);
594        }
595    }
596    
597    private class PaymentMethodNodeType
598        extends PaymentMethodSelectorNodeType {
599        private PaymentMethod paymentMethod = null;
600        
601        public PaymentMethodNodeType(SelectorControl selectorControl) {
602            super(selectorControl, SelectorNodeTypes.PAYMENT_METHOD.name());
603            
604            if(!hasExecutionErrors()) {
605                String paymentMethodName = form.getPaymentMethodName();
606                
607                paymentMethod = paymentMethodControl.getPaymentMethodByName(paymentMethodName);
608                
609                if(paymentMethod == null) {
610                    addExecutionError(ExecutionErrors.UnknownPaymentMethodName.name(), paymentMethodName);
611                }
612            }
613        }
614        
615        @Override
616        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
617            selectorControl.createSelectorNodePaymentMethod(selectorNode, paymentMethod, partyPK);
618        }
619    }
620    
621    private class PaymentProcessorNodeType
622        extends PaymentProcessorSelectorNodeType {
623        private PaymentProcessor paymentProcessor = null;
624        
625        public PaymentProcessorNodeType(SelectorControl selectorControl) {
626            super(selectorControl, SelectorNodeTypes.PAYMENT_PROCESSOR.name());
627            
628            if(!hasExecutionErrors()) {
629                String paymentProcessorName = form.getPaymentProcessorName();
630                
631                paymentProcessor = paymentProcessorControl.getPaymentProcessorByName(paymentProcessorName);
632                
633                if(paymentProcessor == null) {
634                    addExecutionError(ExecutionErrors.UnknownPaymentProcessorName.name(), paymentProcessorName);
635                }
636            }
637        }
638        
639        @Override
640        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
641            selectorControl.createSelectorNodePaymentProcessor(selectorNode, paymentProcessor, partyPK);
642        }
643    }
644    
645    private class GeoCodeNodeType
646        extends GeoSelectorNodeType {
647        private GeoCode geoCode = null;
648        
649        public GeoCodeNodeType(SelectorControl selectorControl) {
650            super(selectorControl, SelectorNodeTypes.PAYMENT_PROCESSOR.name());
651            
652            if(!hasExecutionErrors()) {
653                String geoCodeName = form.getGeoCodeName();
654                String countryName = form.getCountryName();
655                var parameterCount = (geoCodeName == null ? 0 : 1) + (countryName == null ? 0 : 1);
656                
657                if(parameterCount == 1) {
658                    if(countryName != null) {
659                        geoCode = geoControl.getCountryByAlias(countryName);
660                        
661                        if(geoCode == null) {
662                            addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName);
663                        }
664                    } else {
665                        geoCode = geoControl.getGeoCodeByName(geoCodeName);
666                        
667                        if(geoCode == null) {
668                            addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName);
669                        }
670                    }
671                } else {
672                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
673                }
674            }
675        }
676        
677        @Override
678        public void execute(SelectorNode selectorNode, PartyPK partyPK) {
679            selectorControl.createSelectorNodeGeoCode(selectorNode, geoCode, partyPK);
680        }
681    }
682    
683    @Override
684    protected BaseResult execute() {
685        if(!hasExecutionErrors()) {
686            var selectorControl = Session.getModelController(SelectorControl.class);
687            String selectorKindName = form.getSelectorKindName();
688            SelectorKind selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
689
690            if(selectorKind != null) {
691                String selectorTypeName = form.getSelectorTypeName();
692                SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
693
694                if(selectorType != null) {
695                    String selectorName = form.getSelectorName();
696                    Selector selector = selectorControl.getSelectorByName(selectorType, selectorName);
697
698                    if(selector != null) {
699                        String selectorNodeName = form.getSelectorNodeName();
700                        SelectorNode selectorNode = selectorControl.getSelectorNodeByName(selector, selectorNodeName);
701
702                        if(selectorNode == null) {
703                            String selectorNodeTypeName = form.getSelectorNodeTypeName();
704                            SelectorNodeType selectorNodeType = selectorControl.getSelectorNodeTypeByName(selectorNodeTypeName);
705
706                            if(selectorNodeType != null) {
707                                selectorNodeTypeName = selectorNodeType.getSelectorNodeTypeName();
708                                
709                                if(selectorControl.getSelectorNodeTypeUse(selectorKind, selectorNodeType) != null) {
710                                    var selectorNodeTypeEnum = SelectorNodeTypes.valueOf(selectorNodeTypeName);
711                                    BaseSelectorNodeType baseSelectorNodeType = null;
712
713                                    switch (selectorNodeTypeEnum) {
714                                        case BOOLEAN:
715                                            baseSelectorNodeType = new BooleanNodeType(selectorControl, selector);
716                                            break;
717                                        case ENTITY_LIST_ITEM:
718                                            baseSelectorNodeType = new EntityListItemNodeType(selectorControl);
719                                            break;
720                                        case RESPONSIBILITY_TYPE:
721                                            baseSelectorNodeType = new ResponsibilityNodeType(selectorControl);
722                                            break;
723                                        case SKILL_TYPE:
724                                            baseSelectorNodeType = new SkillNodeType(selectorControl);
725                                            break;
726                                        case TRAINING_CLASS:
727                                            baseSelectorNodeType = new TrainingClassNodeType(selectorControl);
728                                            break;
729                                        case WORKFLOW_STEP:
730                                            baseSelectorNodeType = new WorkflowStepNodeType(selectorControl);
731                                            break;
732                                        case ITEM_CATEGORY:
733                                            baseSelectorNodeType = new ItemCategoryNodeType(selectorControl);
734                                            break;
735                                        case ITEM_ACCOUNTING_CATEGORY:
736                                            baseSelectorNodeType = new ItemAccountingCategoryNodeType(selectorControl);
737                                            break;
738                                        case ITEM_PURCHASING_CATEGORY:
739                                            baseSelectorNodeType = new ItemPurchasingCategoryNodeType(selectorControl);
740                                            break;
741                                        case PAYMENT_METHOD:
742                                            baseSelectorNodeType = new PaymentMethodNodeType(selectorControl);
743                                            break;
744                                        case PAYMENT_PROCESSOR:
745                                            baseSelectorNodeType = new PaymentProcessorNodeType(selectorControl);
746                                            break;
747                                        case GEO_CODE:
748                                            baseSelectorNodeType = new GeoCodeNodeType(selectorControl);
749                                            break;
750                                        default:
751                                            break;
752                                    }
753
754                                    if(!hasExecutionErrors()) {
755                                        var partyPK = getPartyPK();
756                                        Boolean isRootSelectorNode = Boolean.valueOf(form.getIsRootSelectorNode());
757                                        Boolean negate = Boolean.valueOf(form.getNegate());
758                                        var description = form.getDescription();
759
760                                        selectorNode = selectorControl.createSelectorNode(selector, selectorNodeName, isRootSelectorNode,
761                                                selectorNodeType, negate, partyPK);
762
763                                        baseSelectorNodeType.execute(selectorNode, partyPK);
764
765                                        if(description != null) {
766                                            Language language = getPreferredLanguage();
767
768                                            selectorControl.createSelectorNodeDescription(selectorNode, language, description, partyPK);
769                                        }
770                                    }
771                                } else {
772                                    addExecutionError(ExecutionErrors.UnknownSelectorNodeTypeUse.name(), selectorKindName, selectorNodeTypeName);
773                                }
774                            } else {
775                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
776                            }
777                        } else {
778                            addExecutionError(ExecutionErrors.DuplicateSelectorNodeName.name(), selectorNodeName);
779                        }
780                    } else {
781                        addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
782                    }
783                } else {
784                    addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
785                }
786            } else {
787                addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
788            }
789        }
790        
791        return null;
792    }
793    
794}