View Javadoc
1   /**
2    * Copyright 2014-2024 Bloomreach B.V. (<a href="https://www.bloomreach.com">https://www.bloomreach.com</a>)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.onehippo.forge.exdocpicker.impl.field;
17  
18  import java.io.Serializable;
19  import java.util.Iterator;
20  import java.util.LinkedHashSet;
21  import java.util.Set;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.wicket.model.IModel;
25  import org.apache.wicket.util.value.IValueMap;
26  import org.apache.wicket.util.value.ValueMap;
27  import org.hippoecm.frontend.dialog.AbstractDialog;
28  import org.hippoecm.frontend.plugin.IPluginContext;
29  import org.hippoecm.frontend.plugin.config.IPluginConfig;
30  import org.onehippo.forge.exdocpicker.api.ExternalDocumentCollection;
31  import org.onehippo.forge.exdocpicker.api.ExternalDocumentServiceContext;
32  import org.onehippo.forge.exdocpicker.api.ExternalDocumentServiceFacade;
33  import org.onehippo.forge.exdocpicker.api.PluginConstants;
34  import org.onehippo.forge.exdocpicker.impl.SimpleExternalDocumentCollection;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * Abstract external document(s) picker dialog class.
40   */
41  public abstract class AbstractExternalDocumentFieldBrowserDialog
42          extends AbstractDialog<ExternalDocumentCollection<Serializable>> {
43  
44      private static final long serialVersionUID = 1L;
45  
46      private static Logger log = LoggerFactory.getLogger(AbstractExternalDocumentFieldBrowserDialog.class);
47  
48      /**
49       * Dialog title model.
50       */
51      private final IModel<String> titleModel;
52  
53      /**
54       * {@link ExternalDocumentServiceFacade} instance.
55       */
56      private final ExternalDocumentServiceFacade<Serializable> exdocService;
57  
58      /**
59       * Currently picked external documents in the list view UI by end user.
60       */
61      private final Set<Serializable> pickedExtDocsInUI = new LinkedHashSet<Serializable>();
62  
63      /**
64       * Currently selected external documents in the document variant node data.
65       */
66      private ExternalDocumentCollection<Serializable> selectedExtDocsInVariantNode;
67  
68      /**
69       * Searched external documents to show in the list view UI.
70       */
71      private ExternalDocumentCollection<Serializable> searchedExtDocs = new SimpleExternalDocumentCollection<Serializable>();
72  
73      /**
74       * Page size.
75       */
76      private int pageSize;
77  
78      /**
79       * Dialog size.
80       */
81      private final IValueMap dialogSize;
82  
83      /**
84       * {@link ExternalDocumentServiceContext} instance.
85       */
86      private final ExternalDocumentServiceContext extDocServiceContext;
87  
88      /**
89       * Constructs external document(s) picker dialog.
90       * @param titleModel title model
91       * @param extDocServiceContext {@link ExternalDocumentServiceContext} instance
92       * @param exdocService {@link ExternalDocumentServiceFacade} instance
93       * @param model the model containing the currently selected external documents in the document node data
94       */
95      public AbstractExternalDocumentFieldBrowserDialog(IModel<String> titleModel,
96              final ExternalDocumentServiceContext extDocServiceContext,
97              final ExternalDocumentServiceFacade<Serializable> exdocService,
98              IModel<ExternalDocumentCollection<Serializable>> model) {
99          super(model);
100         setOutputMarkupId(true);
101 
102         this.titleModel = titleModel;
103         this.extDocServiceContext = extDocServiceContext;
104         this.exdocService = exdocService;
105 
106         final String dialogSizeParam = getPluginConfig().getString(PluginConstants.PARAM_DIALOG_SIZE,
107                 PluginConstants.DEFAULT_DIALOG_SIZE);
108         dialogSize = new ValueMap(dialogSizeParam).makeImmutable();
109 
110         pageSize = getPluginConfig().getInt(PluginConstants.PARAM_PAGE_SIZE, PluginConstants.DEFAULT_PAGE_SIZE);
111 
112         selectedExtDocsInVariantNode = getModelObject();
113 
114         pickedExtDocsInUI.clear();
115 
116         for (Iterator<? extends Serializable> it = selectedExtDocsInVariantNode.iterator(); it.hasNext();) {
117             pickedExtDocsInUI.add(it.next());
118         }
119 
120         if (getModel().getObject() == null) {
121             setOkVisible(false);
122             setOkEnabled(false);
123         }
124 
125         initializeSearchedExternalDocuments();
126         initializeDataListView();
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     protected void onOk() {
134         if (pickedExtDocsInUI != null) {
135             if (isSingleSelectionMode()) {
136                 selectedExtDocsInVariantNode.clear();
137                 Serializable curDoc = null;
138                 // when single selection mode, let's add the last added item only.
139                 for (Serializable serializable : pickedExtDocsInUI) {
140                     curDoc = serializable;
141                 }
142                 if (curDoc != null) {
143                     selectedExtDocsInVariantNode.add(curDoc);
144                 }
145                 exdocService.setFieldExternalDocuments(extDocServiceContext, selectedExtDocsInVariantNode);
146             } else {
147                 boolean added = false;
148 
149                 for (Serializable doc : pickedExtDocsInUI) {
150                     if (!selectedExtDocsInVariantNode.contains(doc)) {
151                         selectedExtDocsInVariantNode.add(doc);
152                         added = true;
153                     }
154                 }
155 
156                 if (added) {
157                     exdocService.setFieldExternalDocuments(extDocServiceContext, selectedExtDocsInVariantNode);
158                 }
159             }
160         }
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
167     public IModel<String> getTitle() {
168         return titleModel;
169     }
170 
171     /**
172      * {@inheritDoc}
173      */
174     @Override
175     public IValueMap getProperties() {
176         return dialogSize;
177     }
178 
179     /**
180      * Returns the plugin config.
181      * @return the plugin config
182      */
183     protected IPluginConfig getPluginConfig() {
184         return extDocServiceContext.getPluginConfig();
185     }
186 
187     /**
188      * Returns the plugin context.
189      * @return the plugin context
190      */
191     protected IPluginContext getPluginContext() {
192         return extDocServiceContext.getPluginContext();
193     }
194 
195     /**
196      * Returns {@link ExternalDocumentServiceFacade} instance.
197      * @return {@link ExternalDocumentServiceFacade} instance
198      */
199     protected ExternalDocumentServiceFacade<Serializable> getExternalDocumentServiceFacade() {
200         return exdocService;
201     }
202 
203     /**
204      * Returns {@link ExternalDocumentServiceContext} instance.
205      * @return {@link ExternalDocumentServiceContext} instance
206      */
207     protected ExternalDocumentServiceContext getExternalDocumentServiceContext() {
208         return extDocServiceContext;
209     }
210 
211     /**
212      * Returns currently picked external documents in the list view UI.
213      * @return currently picked external documents in the list view UI
214      */
215     protected Set<Serializable> getPickedExternalDocuments() {
216         return pickedExtDocsInUI;
217     }
218 
219     /**
220      * Returns currently selected external documents in the document variant data node.
221      * @return currently selected external documents in the document variant data node
222      */
223     protected ExternalDocumentCollection<Serializable> getSelectedExternalDocuments() {
224         return selectedExtDocsInVariantNode;
225     }
226 
227     /**
228      * Returns searched external documents to show in the list view UI.
229      * @return searched external documents to show in the list view UI
230      */
231     protected ExternalDocumentCollection<Serializable> getSearchedExternalDocuments() {
232         return searchedExtDocs;
233     }
234 
235     /**
236      * Returns true if the selection mode in UI is 'single'.
237      * @return true if the selection mode in UI is 'single'
238      */
239     protected boolean isSingleSelectionMode() {
240         return StringUtils.equalsIgnoreCase(PluginConstants.SELECTION_MODE_SINGLE, getPluginConfig()
241                 .getString(PluginConstants.PARAM_SELECTION_MODE, PluginConstants.SELECTION_MODE_MULTIPLE));
242     }
243 
244     /**
245      * Returns the page size.
246      * @return the page size
247      */
248     protected int getPageSize() {
249         return pageSize;
250     }
251 
252     /**
253      * Returns the dialog size.
254      * @return the dialog size
255      */
256     protected IValueMap getDialogSize() {
257         return dialogSize;
258     }
259 
260     /**
261      * Initializes the {@link #searchedExtDocs} on construction.
262      * Invoked during the construction of this dialog instance.
263      */
264     abstract protected void initializeSearchedExternalDocuments();
265 
266     /**
267      * Initializes the data list view UI.
268      * Invoked during the construction of this dialog instance.
269      */
270     abstract protected void initializeDataListView();
271 
272 }