View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.flowable.support;
20  
21  import java.util.List;
22  import java.util.Optional;
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.syncope.core.flowable.api.DropdownValueProvider;
25  import org.flowable.bpmn.model.FormProperty;
26  import org.flowable.common.engine.api.delegate.Expression;
27  import org.flowable.common.engine.impl.el.ExpressionManager;
28  import org.flowable.engine.form.AbstractFormType;
29  import org.flowable.engine.impl.form.DefaultTaskFormHandler;
30  import org.flowable.engine.impl.form.FormPropertyHandler;
31  import org.flowable.engine.impl.form.FormTypes;
32  import org.flowable.engine.impl.persistence.entity.DeploymentEntity;
33  import org.flowable.engine.impl.util.CommandContextUtil;
34  import org.flowable.engine.repository.ProcessDefinition;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * Extends {@link DefaultTaskFormHandler} with purpose of supporting more form types than Flowable's default.
40   */
41  public class SyncopeTaskFormHandler extends DefaultTaskFormHandler {
42  
43      private static final long serialVersionUID = -5271243544388455797L;
44  
45      protected static final Logger LOG = LoggerFactory.getLogger(SyncopeTaskFormHandler.class);
46  
47      protected static Optional<AbstractFormType> parseFormPropertyType(
48          final FormProperty formProperty, final ExpressionManager expressionManager) {
49  
50          AbstractFormType formType = null;
51  
52          switch (formProperty.getType()) {
53              case "dropdown":
54                  if (formProperty.getFormValues().isEmpty()
55                          || !DropdownValueProvider.NAME.equals(formProperty.getFormValues().get(0).getId())) {
56  
57                      LOG.warn("A single value with id '" + DropdownValueProvider.NAME + "' was expected, ignoring");
58                  } else {
59                      formType = new DropdownFormType(formProperty.getFormValues().get(0).getName());
60                  }
61                  break;
62  
63              case "password":
64                  formType = new PasswordFormType();
65                  break;
66  
67              default:
68          }
69  
70          return Optional.ofNullable(formType);
71      }
72  
73      @Override
74      public void parseConfiguration(
75              final List<FormProperty> formProperties,
76              final String formKey,
77              final DeploymentEntity deployment,
78              final ProcessDefinition processDefinition) {
79  
80          this.deploymentId = deployment.getId();
81  
82          ExpressionManager expressionManager = CommandContextUtil.getProcessEngineConfiguration().getExpressionManager();
83  
84          if (StringUtils.isNotEmpty(formKey)) {
85              this.formKey = expressionManager.createExpression(formKey);
86          }
87  
88          FormTypes formTypes = CommandContextUtil.getProcessEngineConfiguration().getFormTypes();
89  
90          formProperties.forEach(formProperty -> {
91              FormPropertyHandler formPropertyHandler = new FormPropertyHandler();
92              formPropertyHandler.setId(formProperty.getId());
93              formPropertyHandler.setName(formProperty.getName());
94  
95              AbstractFormType type = parseFormPropertyType(formProperty, expressionManager).
96                  orElseGet(() -> formTypes.parseFormPropertyType(formProperty));
97              formPropertyHandler.setType(type);
98              formPropertyHandler.setRequired(formProperty.isRequired());
99              formPropertyHandler.setReadable(formProperty.isReadable());
100             formPropertyHandler.setWritable(formProperty.isWriteable());
101             formPropertyHandler.setVariableName(formProperty.getVariable());
102 
103             if (StringUtils.isNotEmpty(formProperty.getExpression())) {
104                 Expression expression = expressionManager.createExpression(formProperty.getExpression());
105                 formPropertyHandler.setVariableExpression(expression);
106             }
107 
108             if (StringUtils.isNotEmpty(formProperty.getDefaultExpression())) {
109                 Expression defaultExpression = expressionManager.createExpression(formProperty.getDefaultExpression());
110                 formPropertyHandler.setDefaultExpression(defaultExpression);
111             }
112 
113             formPropertyHandlers.add(formPropertyHandler);
114         });
115     }
116 }