View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.rest.actions.resource;
14  
15  import com.eviware.soapui.impl.rest.RestResource;
16  import com.eviware.soapui.impl.rest.actions.support.NewRestResourceActionBase;
17  import com.eviware.soapui.support.MessageSupport;
18  import com.eviware.soapui.support.UISupport;
19  import com.eviware.x.form.XFormDialog;
20  
21  /***
22   * Actions for importing an existing soapUI project file into the current workspace
23   *
24   * @author Ole.Matzura
25   */
26  
27  public class NewRestChildResourceAction extends NewRestResourceActionBase<RestResource>
28  {
29     public static final String SOAPUI_ACTION_ID = "NewRestChildResourceAction";
30     public static final MessageSupport messages = MessageSupport.getMessages( NewRestChildResourceAction.class );
31     private XFormDialog dialog;
32  
33     public NewRestChildResourceAction()
34     {
35        super( messages.get( "title" ), messages.get( "description" ) );
36     }
37  
38     protected RestResource createRestResource( RestResource parentResource, String path, XFormDialog dialog )
39     {
40        RestResource possibleParent = null;
41        String p = parentResource.getFullPath() + path;
42  
43        for( RestResource resource : parentResource.getAllChildResources() )
44        {
45           if( p.startsWith( resource.getFullPath() ) )
46           {
47              int c = 0;
48              for( ; c < resource.getChildResourceCount(); c++ )
49              {
50                 if( p.startsWith( resource.getChildResourceAt( c ).getFullPath() ) )
51                    break;
52              }
53  
54              // found subresource?
55              if( c != resource.getChildResourceCount() )
56                 continue;
57  
58              possibleParent = resource;
59              break;
60           }
61        }
62  
63        RestResource resource;
64  
65        if( possibleParent != null && UISupport.confirm( "Create resource as child to [" + possibleParent.getName() + "]", "New Child Resource" ) )
66        {
67           // adjust path
68           path = path.substring( p.length() - possibleParent.getFullPath().length() - 1 );
69           resource = possibleParent.addNewChildResource( dialog.getValue( Form.RESOURCENAME ), path );
70        }
71        else
72        {
73           resource = parentResource.addNewChildResource( dialog.getValue( Form.RESOURCENAME ), path );
74        }
75  
76        return resource;
77     }
78  }