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  
57  /**
58   * Maintains a registry of dialog providers registered by id.
59   *
60   * @deprecated since 6.0. Use new framework and {@link info.magnolia.ui.dialog.DialogDefinitionRegistry} instead.
61   */
62  @Deprecated
63  public class DialogDefinitionRegistry extends AbstractRegistry<DialogDefinition> {
64  
65      private info.magnolia.ui.dialog.DialogDefinitionRegistry newRegistry;
66  
67      @Inject
68      public DialogDefinitionRegistry(ModuleRegistry moduleRegistry, info.magnolia.ui.dialog.DialogDefinitionRegistry newRegistry) {
69          super(moduleRegistry);
70          this.newRegistry = newRegistry;
71      }
72  
73      /**
74       * @deprecated since 6.0. Use {@link #DialogDefinitionRegistry(info.magnolia.module.ModuleRegistry, info.magnolia.ui.dialog.DialogDefinitionRegistry)} instead.
75       */
76      @Deprecated
77      public DialogDefinitionRegistry(ModuleRegistry moduleRegistry, FieldTypeDefinitionRegistry fieldTypeDefinitionRegistry) {
78          this(moduleRegistry, Components.getComponent(info.magnolia.ui.dialog.DialogDefinitionRegistry.class));
79      }
80  
81      @Override
82      public DefinitionType type() {
83          return DefinitionTypes.DIALOG;
84      }
85  
86      @Override
87      public DefinitionMetadataBuilder newMetadataBuilder() {
88          return DefinitionMetadataBuilder.usingModuleAndRelativePathAsId();
89      }
90  
91      @Override
92      public void register(DefinitionProvider provider) {
93          newRegistry.register(provider);
94      }
95  
96      @Override
97      public Collection<DefinitionProvider<DialogDefinition>> getAllProviders() {
98          return newRegistry.getAllMetadata().stream()
99                  .map(this::getProvider)
100                 .filter(provider -> provider.isValid() && provider.get() instanceof FormDialogDefinition)
101                 .collect(Collectors.toList());
102     }
103 
104     @Override
105     public Collection<DefinitionMetadata> getAllMetadata() {
106         return getAllProviders().stream()
107                 .map(DefinitionProvider::getMetadata)
108                 .collect(Collectors.toList());
109     }
110 
111     @Override
112     public void start() {
113         newRegistry.start();
114     }
115 
116     @Override
117     public Set<DefinitionMetadata> unregisterAndRegister(Collection registeredIds, Collection definitionProviders) {
118         return newRegistry.unregisterAndRegister(registeredIds, definitionProviders);
119     }
120 
121     @Override
122     public void removeDecorator(DefinitionDecorator definitionDecorator) {
123         newRegistry.removeDecorator(definitionDecorator);
124     }
125 
126     @Override
127     public DefinitionProvider<DialogDefinition> getProvider(DefinitionMetadata id) {
128         return (DefinitionProvider) newRegistry.getProvider(id);
129     }
130 
131     @Override
132     public DefinitionProvider<DialogDefinition> getProvider(String referenceId) {
133         return (DefinitionProvider) newRegistry.getProvider(referenceId);
134     }
135 
136     @Override
137     public void addDecorator(DefinitionDecorator definitionDecorator) {
138         newRegistry.addDecorator(definitionDecorator);
139     }
140 
141     @Override
142     public void addValidator(DefinitionValidator validator) {
143         newRegistry.addValidator(validator);
144     }
145 
146     /**
147      * @deprecated since 5.4 use {@link #getProvider(String)}
148      */
149     @Deprecated
150     public FormDialogDefinition getDialogDefinition(String id) throws RegistrationException {
151         try {
152             DefinitionProvider<DialogDefinition> dialogDefinitionProvider = getProvider(id);
153             DialogDefinition dialogDefinition = dialogDefinitionProvider.get();
154             return (FormDialogDefinition) dialogDefinition;
155         } catch (NoSuchDefinitionException | InvalidDefinitionException e) {
156             throw new RegistrationException(e.getMessage(), e);
157         }
158     }
159 
160     /**
161      * @deprecated since 5.4, get the {@link info.magnolia.ui.dialog.definition.DialogDefinition} first, and get the presenter class from it.
162      */
163     @Deprecated
164     public Class<? extends FormDialogPresenter> getPresenterClass(String id) throws RegistrationException {
165         return (Class<? extends FormDialogPresenter>) getProvider(id).get().getPresenterClass();
166     }
167 }