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.client.console.panels;
20  
21  import java.io.Serializable;
22  import java.nio.charset.StandardCharsets;
23  import javax.ws.rs.core.MediaType;
24  import org.apache.commons.io.IOUtils;
25  import org.apache.syncope.client.console.SyncopeConsoleSession;
26  import org.apache.syncope.client.console.rest.BpmnProcessRestClient;
27  import org.apache.syncope.client.ui.commons.Constants;
28  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
29  import org.apache.wicket.PageReference;
30  import org.apache.wicket.ajax.AjaxRequestTarget;
31  import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
32  import org.apache.wicket.markup.html.WebMarkupContainer;
33  import org.apache.wicket.markup.html.form.Form;
34  import org.apache.wicket.markup.html.form.TextField;
35  import org.apache.wicket.model.Model;
36  import org.apache.wicket.spring.injection.annot.SpringBean;
37  
38  public class NewBpmnProcess extends TogglePanel<Serializable> {
39  
40      private static final long serialVersionUID = -4886361549305302161L;
41  
42      @SpringBean
43      protected BpmnProcessRestClient bpmnProcessRestClient;
44  
45      public NewBpmnProcess(final String id, final WebMarkupContainer container, final PageReference pageRef) {
46          super(id, pageRef);
47  
48          Form<?> form = new Form<>("form");
49          addInnerObject(form);
50  
51          TextField<String> key = new TextField<>("key", new Model<>());
52          key.setRequired(true);
53          form.add(key);
54  
55          form.add(new AjaxSubmitLink("submit", form) {
56  
57              private static final long serialVersionUID = 4947613489823025052L;
58  
59              @Override
60              protected void onSubmit(final AjaxRequestTarget target) {
61                  try {
62                      bpmnProcessRestClient.setDefinition(MediaType.APPLICATION_XML_TYPE, key.getModelObject(),
63                              IOUtils.toString(
64                                      NewBpmnProcess.class.getResourceAsStream("empty.bpmn20.xml"),
65                                      StandardCharsets.UTF_8).replaceAll("%KEY%", key.getModelObject()));
66  
67                      key.getModel().setObject(null);
68                      SyncopeConsoleSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
69                      toggle(target, false);
70                      target.add(container);
71                  } catch (Exception e) {
72                      LOG.error("While creating new BPMN process", e);
73                      SyncopeConsoleSession.get().onException(e);
74                  }
75                  ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
76              }
77  
78              @Override
79              protected void onError(final AjaxRequestTarget target) {
80                  ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
81              }
82          });
83      }
84  }