View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.dialog.registry;
35  
36  import info.magnolia.config.registry.AbstractRegistry;
37  import info.magnolia.config.registry.DefinitionMetadata;
38  import info.magnolia.config.registry.DefinitionMetadataBuilder;
39  import info.magnolia.config.registry.DefinitionProvider;
40  import info.magnolia.config.registry.DefinitionType;
41  import info.magnolia.config.registry.decoration.DefinitionDecorator;
42  import info.magnolia.config.registry.validator.DefinitionValidator;
43  import info.magnolia.module.ModuleRegistry;
44  import info.magnolia.objectfactory.Components;
45  import info.magnolia.registry.RegistrationException;
46  import info.magnolia.ui.dialog.definition.DialogDefinition;
47  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
48  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
49  import info.magnolia.ui.form.fieldtype.registry.FieldTypeDefinitionRegistry;
50  
51  import java.util.Collection;
52  import java.util.Set;
53  import java.util.stream.Collectors;
54  
55  import javax.inject.Inject;
56  import javax.inject.Singleton;
57  
58  /**
59   * Maintains a registry of dialog providers registered by id.
60   *
61   * @deprecated since 6.0. Use new framework and {@link info.magnolia.ui.dialog.DialogDefinitionRegistry} instead.
62   */
63  @Deprecated
64  @Singleton
65  public class DialogDefinitionRegistry extends AbstractRegistry<DialogDefinition> {
66  
67      private info.magnolia.ui.dialog.DialogDefinitionRegistry newRegistry;
68  
69      @Inject
70      public DialogDefinitionRegistry(ModuleRegistry moduleRegistry, info.magnolia.ui.dialog.DialogDefinitionRegistry newRegistry) {
71          super(moduleRegistry);
72          this.newRegistry = newRegistry;
73      }
74  
75      /**
76       * @deprecated since 6.0. Use {@link #DialogDefinitionRegistry(info.magnolia.module.ModuleRegistry, info.magnolia.ui.dialog.DialogDefinitionRegistry)} instead.
77       */
78      @Deprecated
79      public DialogDefinitionRegistry(ModuleRegistry moduleRegistry, FieldTypeDefinitionRegistry fieldTypeDefinitionRegistry) {
80          this(moduleRegistry, Components.getComponent(info.magnolia.ui.dialog.DialogDefinitionRegistry.class));
81      }
82  
83      @Override
84      public DefinitionType type() {
85          return DefinitionTypes.DIALOG;
86      }
87  
88      @Override
89      public DefinitionMetadataBuilder newMetadataBuilder() {
90          return DefinitionMetadataBuilder.usingModuleAndRelativePathAsId();
91      }
92  
93      @Override
94      public void register(DefinitionProvider provider) {
95          newRegistry.register(provider);
96      }
97  
98      @Override
99      public Collection<DefinitionProvider<DialogDefinition>> getAllProviders() {
100         return newRegistry.getAllMetadata().stream()
101                 .map(this::getProvider)
102                 .filter(provider -> provider.isValid() && provider.get() instanceof FormDialogDefinition)
103                 .collect(Collectors.toList());
104     }
105 
106     @Override
107     public Collection<DefinitionMetadata> getAllMetadata() {
108         return getAllProviders().stream()
109                 .map(DefinitionProvider::getMetadata)
110                 .collect(Collectors.toList());
111     }
112 
113     @Override
114     public void start() {
115         newRegistry.start();
116     }
117 
118     @Override
119     public Set<DefinitionMetadata> unregisterAndRegister(Collection registeredIds, Collection definitionProviders) {
120         return newRegistry.unregisterAndRegister(registeredIds, definitionProviders);
121     }
122 
123     @Override
124     public void removeDecorator(DefinitionDecorator definitionDecorator) {
125         newRegistry.removeDecorator(definitionDecorator);
126     }
127 
128     @Override
129     public DefinitionProvider<DialogDefinition> getProvider(DefinitionMetadata id) {
130         return (DefinitionProvider) newRegistry.getProvider(id);
131     }
132 
133     @Override
134     public DefinitionProvider<DialogDefinition> getProvider(String referenceId) {
135         return (DefinitionProvider) newRegistry.getProvider(referenceId);
136     }
137 
138     @Override
139     public void addDecorator(DefinitionDecorator definitionDecorator) {
140         newRegistry.addDecorator(definitionDecorator);
141     }
142 
143     @Override
144     public void addValidator(DefinitionValidator validator) {
145         newRegistry.addValidator(validator);
146     }
147 
148     /**
149      * @deprecated since 5.4 use {@link #getProvider(String)}
150      */
151     @Deprecated
152     public FormDialogDefinition getDialogDefinition(String id) throws RegistrationException {
153         try {
154             DefinitionProvider<DialogDefinition> dialogDefinitionProvider = getProvider(id);
155             DialogDefinition dialogDefinition = dialogDefinitionProvider.get();
156             return (FormDialogDefinition) dialogDefinition;
157         } catch (NoSuchDefinitionException | InvalidDefinitionException e) {
158             throw new RegistrationException(e.getMessage(), e);
159         }
160     }
161 
162     /**
163      * @deprecated since 5.4, get the {@link info.magnolia.ui.dialog.definition.DialogDefinition} first, and get the presenter class from it.
164      */
165     @Deprecated
166     public Class<? extends FormDialogPresenter> getPresenterClass(String id) throws RegistrationException {
167         return (Class<? extends FormDialogPresenter>) getProvider(id).get().getPresenterClass();
168     }
169 }