ref #10334 adapt TaxEditor map parameters to includeUnpublished param
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / feature / FeatureDistributionDetailElement.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.ui.section.feature;
11
12 import java.net.URISyntaxException;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Label;
19
20 import eu.etaxonomy.cdm.api.service.geo.IDistributionService;
21 import eu.etaxonomy.cdm.common.URI;
22 import eu.etaxonomy.cdm.common.UriUtils;
23 import eu.etaxonomy.cdm.model.common.Language;
24 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
25 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
26 import eu.etaxonomy.cdm.model.description.TaxonDescription;
27 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
28 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31 import eu.etaxonomy.taxeditor.ui.element.BrowserElement;
32 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
33 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
34 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
35 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
36
37 /**
38 * <p>FeatureDistributionDetailElement class.</p>
39 *
40 * @author n.hoffmann
41 * @created Sep 23, 2010
42 */
43 public class FeatureDistributionDetailElement extends AbstractCdmDetailElement<FeatureNodeContainer> {
44
45 private BrowserElement image;
46
47 /**
48 * <p>Constructor for FeatureDistributionDetailElement.</p>
49 *
50 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
51 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
52 */
53 public FeatureDistributionDetailElement(CdmFormFactory formFactory,
54 ICdmFormElement formElement) {
55 super(formFactory, formElement);
56 }
57
58 @Override
59 protected void createControls(ICdmFormElement formElement, FeatureNodeContainer entity,
60 int style) {
61 String serviceUriString = PreferencesUtil.getStringValue(PreferencePredicate.EditMapServiceAccessPoint.getKey());
62 String message = null;
63 try {
64 URI serviceUri = new URI(serviceUriString);
65 if(UriUtils.isServiceAvailable(serviceUri, 500)){
66 image = formFactory.createBrowserElement(formElement, null, style);
67 // FIXME : This is a temporary workaround which ic
68 // waiting for #5357 to be fixed
69 if(!isUnsavedDistribution(getEntity())) {
70 String mapUriString = getMapUriString(getEntity());
71 image.setImageUriString(mapUriString);
72 message = mapUriString;
73 }
74 formElement.getLayoutComposite().layout();
75
76 return;
77 } else {
78 message = String.format("The service is not available: %s", serviceUriString);
79 }
80 } catch (URISyntaxException e) {
81 message = String.format("The URI has problems: %s", serviceUriString);
82 }
83
84 Label label = formFactory.createLabel(getLayoutComposite(), message, SWT.WRAP);
85 addControl(label);
86 label.setLayoutData(LayoutConstants.FILL(2, 1));
87 }
88
89 @Override
90 public void handleEvent(Object eventSource) {
91 //
92 }
93
94 private String getMapUriString(FeatureNodeContainer container){
95 String accessPoint = PreferencesUtil.getMapServiceAccessPoint();
96 Map<PresenceAbsenceTerm, java.awt.Color> presenceAbsenceTermColors = null;
97 // FIXME due to a bug in the rest map service we have to ensure that width will always be an even number
98 // image.calculateWidth() % 2 == 1 ? image.calculateWidth() + 1 :
99 int width = image.calculateWidth();
100
101 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
102 IDistributionService distributionService = CdmStore.getCurrentApplicationConfiguration().getDistributionService();
103 boolean includeUnpublished = true;
104
105 String parameter = distributionService.getDistributionServiceRequestParameterString(
106 getTaxonDescriptions(),
107 false,
108 false,
109 null,
110 presenceAbsenceTermColors,
111 languages,
112 includeUnpublished);
113
114 String mapUriString = String.format("%s?%s&ms=1000&bbox=-180,-90,180,90&l=earth", accessPoint, parameter);
115 return mapUriString;
116 }
117
118 private List<TaxonDescription> getTaxonDescriptions() {
119 return Arrays.asList(((TaxonDescription) getEntity().getDescription()).getTaxon().getDescriptions().toArray(new TaxonDescription[0]));
120 }
121
122 private boolean isUnsavedDistribution(FeatureNodeContainer container) {
123 for(DescriptionElementBase dist : container.getDescriptionElements()) {
124 if(dist.getId() == 0) {
125 return true;
126 }
127 }
128 return false;
129 }
130
131 @Override
132 public void fillFields() {
133 String serviceUriString = PreferencesUtil.getStringValue(PreferencePredicate.EditMapServiceAccessPoint.getKey());
134 String message = null;
135 try {
136 URI serviceUri = new URI(serviceUriString);
137 if(UriUtils.isServiceAvailable(serviceUri, 500)){
138 // image = formFactory.createBrowserElement(formElement, null, style);
139 // FIXME : This is a temporary workaround which ic
140 // waiting for #5357 to be fixed
141 if(!isUnsavedDistribution(getEntity())) {
142 String mapUriString = getMapUriString(getEntity());
143 image.setImageUriString(mapUriString);
144 message = mapUriString;
145 }
146 getParentElement().getLayoutComposite().layout();
147
148 return;
149 } else {
150 message = String.format("The service is not available: %s", serviceUriString);
151 }
152 } catch (URISyntaxException e) {
153 message = String.format("The URI has problems: %s", serviceUriString);
154 }
155
156 }
157
158 }