|
| 1 | +package org.roda.wui.client.planning; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Comparator; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.roda.core.data.common.RodaConstants; |
| 8 | +import org.roda.core.data.utils.RepresentationInformationUtils; |
| 9 | +import org.roda.core.data.v2.index.CountRequest; |
| 10 | +import org.roda.core.data.v2.index.IsIndexed; |
| 11 | +import org.roda.core.data.v2.index.filter.Filter; |
| 12 | +import org.roda.core.data.v2.index.filter.FilterParameter; |
| 13 | +import org.roda.core.data.v2.index.filter.OrFiltersParameters; |
| 14 | +import org.roda.core.data.v2.index.filter.SimpleFilterParameter; |
| 15 | +import org.roda.core.data.v2.ip.IndexedAIP; |
| 16 | +import org.roda.core.data.v2.ip.IndexedFile; |
| 17 | +import org.roda.core.data.v2.ip.IndexedRepresentation; |
| 18 | +import org.roda.core.data.v2.ri.RelationObjectType; |
| 19 | +import org.roda.core.data.v2.ri.RepresentationInformation; |
| 20 | +import org.roda.core.data.v2.ri.RepresentationInformationCreateRequest; |
| 21 | +import org.roda.core.data.v2.ri.RepresentationInformationRelation; |
| 22 | +import org.roda.wui.client.common.dialogs.RepresentationInformationDialogs; |
| 23 | +import org.roda.wui.client.common.utils.AsyncCallbackUtils; |
| 24 | +import org.roda.wui.client.services.Services; |
| 25 | +import org.roda.wui.common.client.tools.HistoryUtils; |
| 26 | +import org.roda.wui.common.client.tools.StringUtils; |
| 27 | + |
| 28 | +import com.google.gwt.core.client.GWT; |
| 29 | +import com.google.gwt.event.dom.client.ClickEvent; |
| 30 | +import com.google.gwt.event.dom.client.ClickHandler; |
| 31 | +import com.google.gwt.uibinder.client.UiBinder; |
| 32 | +import com.google.gwt.uibinder.client.UiField; |
| 33 | +import com.google.gwt.user.client.rpc.AsyncCallback; |
| 34 | +import com.google.gwt.user.client.ui.Anchor; |
| 35 | +import com.google.gwt.user.client.ui.Composite; |
| 36 | +import com.google.gwt.user.client.ui.FlowPanel; |
| 37 | +import com.google.gwt.user.client.ui.InlineHTML; |
| 38 | +import com.google.gwt.user.client.ui.Label; |
| 39 | +import com.google.gwt.user.client.ui.Widget; |
| 40 | + |
| 41 | +import config.i18n.client.ClientMessages; |
| 42 | + |
| 43 | +/** |
| 44 | + * |
| 45 | + * @author Eduardo Teixeira <eteixeira@keep.pt> |
| 46 | + */ |
| 47 | +public class AssociationsPanelRepresentationInformation extends Composite { |
| 48 | + private static final ClientMessages messages = GWT.create(ClientMessages.class); |
| 49 | + private static final MyUiBinder uiBinder = GWT.create(MyUiBinder.class); |
| 50 | + |
| 51 | + @UiField |
| 52 | + FlowPanel associationsPanel; |
| 53 | + @UiField |
| 54 | + FlowPanel relationsPanel; |
| 55 | + |
| 56 | + private final List<FilterParameter> aipParams = new ArrayList<>(); |
| 57 | + private final List<FilterParameter> representationParams = new ArrayList<>(); |
| 58 | + private final List<FilterParameter> fileParams = new ArrayList<>(); |
| 59 | + |
| 60 | + public AssociationsPanelRepresentationInformation(RepresentationInformation ri) { |
| 61 | + initWidget(uiBinder.createAndBindUi(this)); |
| 62 | + init(ri); |
| 63 | + } |
| 64 | + |
| 65 | + private void init(RepresentationInformation ri) { |
| 66 | + updateLists(ri); |
| 67 | + } |
| 68 | + |
| 69 | + public void updateLists(RepresentationInformation ri) { |
| 70 | + aipParams.clear(); |
| 71 | + representationParams.clear(); |
| 72 | + associationsPanel.clear(); |
| 73 | + relationsPanel.clear(); |
| 74 | + fileParams.clear(); |
| 75 | + |
| 76 | + initEntityFilters(ri); |
| 77 | + initRelations(ri); |
| 78 | + } |
| 79 | + |
| 80 | + private void initEntityFilters(RepresentationInformation ri) { |
| 81 | + for (String filter : ri.getFilters()) { |
| 82 | + String[] parts = filter.split(RepresentationInformationUtils.REPRESENTATION_INFORMATION_FILTER_SEPARATOR); |
| 83 | + if (parts.length < 3) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + if (RodaConstants.INDEX_AIP.equals(parts[0])) { |
| 88 | + aipParams.add(new SimpleFilterParameter(parts[1], parts[2])); |
| 89 | + } else if (RodaConstants.INDEX_REPRESENTATION.equals(parts[0])) { |
| 90 | + representationParams.add(new SimpleFilterParameter(parts[1], parts[2])); |
| 91 | + } else if (RodaConstants.INDEX_FILE.equals(parts[0])) { |
| 92 | + fileParams.add(new SimpleFilterParameter(parts[1], parts[2])); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (!aipParams.isEmpty()) { |
| 97 | + countAndRenderAssociations(ri, aipParams, IndexedAIP.class, IndexedAIP.class.getSimpleName()); |
| 98 | + } else if (!representationParams.isEmpty()) { |
| 99 | + countAndRenderAssociations(ri, representationParams, IndexedRepresentation.class, |
| 100 | + IndexedRepresentation.class.getSimpleName()); |
| 101 | + } else if (!fileParams.isEmpty()) { |
| 102 | + countAndRenderAssociations(ri, fileParams, IndexedFile.class, IndexedFile.class.getSimpleName()); |
| 103 | + } else { |
| 104 | + initEntityFiltersObjectPanel(ri, 0L, null, IndexedAIP.class.getSimpleName()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private <O extends IsIndexed> void countAndRenderAssociations(RepresentationInformation ri, |
| 109 | + List<FilterParameter> params, Class<O> entityClass, String searchType) { |
| 110 | + Filter filter = new Filter(); |
| 111 | + filter.add(new OrFiltersParameters(params)); |
| 112 | + |
| 113 | + Services services = new Services("Count RI associations", "count"); |
| 114 | + CountRequest countRequest = new CountRequest(filter, true); |
| 115 | + services.rodaEntityRestService(s -> s.count(countRequest), entityClass) |
| 116 | + .whenComplete((count, throwable) -> initEntityFiltersObjectPanel(ri, count.getResult(), throwable, searchType)); |
| 117 | + } |
| 118 | + |
| 119 | + private void initEntityFiltersObjectPanel(RepresentationInformation ri, final Long count, final Throwable throwable, |
| 120 | + final String searchType) { |
| 121 | + if (throwable != null) { |
| 122 | + AsyncCallbackUtils.defaultFailureTreatment(throwable); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + String url = HistoryUtils.getSearchHistoryByRepresentationInformationFilter(ri.getFilters(), searchType); |
| 127 | + |
| 128 | + InlineHTML label = new InlineHTML(); |
| 129 | + label.addStyleName("ri-form-label-inline"); |
| 130 | + if (IndexedAIP.class.getSimpleName().equals(searchType)) { |
| 131 | + label.setHTML(messages.representationInformationIntellectualEntities(count.intValue(), url)); |
| 132 | + } else if (IndexedRepresentation.class.getSimpleName().equals(searchType)) { |
| 133 | + label.setHTML(messages.representationInformationRepresentations(count.intValue(), url)); |
| 134 | + } else if (IndexedFile.class.getSimpleName().equals(searchType)) { |
| 135 | + label.setHTML(messages.representationInformationFiles(count.intValue(), url)); |
| 136 | + } |
| 137 | + associationsPanel.add(label); |
| 138 | + |
| 139 | + InlineHTML edit = new InlineHTML("<i class='fa fa-pencil' aria-hidden='true'></i>"); |
| 140 | + edit.setTitle("Edit association rules"); |
| 141 | + edit.addStyleName("ri-category link-color"); |
| 142 | + edit.addClickHandler(new ClickHandler() { |
| 143 | + @Override |
| 144 | + public void onClick(ClickEvent event) { |
| 145 | + RepresentationInformationDialogs.showPromptDialogRepresentationInformation( |
| 146 | + messages.representationInformationEditAssociations(), messages.cancelButton(), messages.confirmButton(), |
| 147 | + messages.searchButton(), ri, new AsyncCallback<RepresentationInformation>() { |
| 148 | + @Override |
| 149 | + public void onFailure(Throwable caught) { |
| 150 | + // no-op |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void onSuccess(RepresentationInformation result) { |
| 155 | + Services services = new Services("Update representation information", "update"); |
| 156 | + RepresentationInformationCreateRequest createRequest = new RepresentationInformationCreateRequest(); |
| 157 | + createRequest.setRepresentationInformation(result); |
| 158 | + services.representationInformationResource(s -> s.updateRepresentationInformation(createRequest)) |
| 159 | + .whenComplete((updated, t) -> { |
| 160 | + if (t == null && updated != null) { |
| 161 | + ri.setFilters(updated.getFilters()); // se não existir setter, atualiza manualmente a lista |
| 162 | + updateLists(ri); |
| 163 | + } |
| 164 | + }); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + associationsPanel.add(edit); |
| 171 | + } |
| 172 | + |
| 173 | + private void initRelations(RepresentationInformation ri) { |
| 174 | + if (ri.getRelations() == null) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + ri.getRelations().sort(Comparator.comparingInt(o -> o.getObjectType().getWeight())); |
| 179 | + for (RepresentationInformationRelation relation : ri.getRelations()) { |
| 180 | + relationsPanel.add(createRelationsLayout(relation)); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private FlowPanel createRelationsLayout(RepresentationInformationRelation relation) { |
| 185 | + FlowPanel panel = new FlowPanel(); |
| 186 | + |
| 187 | + Label entryLabel = new Label(relation.getRelationTypeI18n()); |
| 188 | + entryLabel.setStyleName("label"); |
| 189 | + panel.add(entryLabel); |
| 190 | + |
| 191 | + Widget w = createRelationViewer(relation); |
| 192 | + if (w != null) { |
| 193 | + w.addStyleName("ri-links-panel"); |
| 194 | + panel.add(w); |
| 195 | + } |
| 196 | + |
| 197 | + return panel; |
| 198 | + } |
| 199 | + |
| 200 | + private Widget createRelationViewer(RepresentationInformationRelation relation) { |
| 201 | + Widget widgetToAdd = null; |
| 202 | + String title = StringUtils.isNotBlank(relation.getTitle()) ? relation.getTitle() : relation.getLink(); |
| 203 | + |
| 204 | + if (relation.getObjectType().equals(RelationObjectType.TEXT)) { |
| 205 | + widgetToAdd = new Label(title); |
| 206 | + } else { |
| 207 | + Anchor anchor = null; |
| 208 | + |
| 209 | + if (relation.getObjectType().equals(RelationObjectType.AIP)) { |
| 210 | + anchor = new Anchor(title, |
| 211 | + HistoryUtils.createHistoryHashLink(HistoryUtils.getHistoryBrowse(relation.getLink()))); |
| 212 | + } else if (relation.getObjectType().equals(RelationObjectType.REPRESENTATION_INFORMATION)) { |
| 213 | + List<String> history = new ArrayList<>(); |
| 214 | + history.addAll(ShowRepresentationInformation.RESOLVER.getHistoryPath()); |
| 215 | + history.add(relation.getLink()); |
| 216 | + anchor = new Anchor(title, HistoryUtils.createHistoryHashLink(history)); |
| 217 | + } else if (relation.getObjectType().equals(RelationObjectType.WEB)) { |
| 218 | + anchor = new Anchor(title, relation.getLink()); |
| 219 | + anchor.getElement().setAttribute("target", "_blank"); |
| 220 | + } |
| 221 | + |
| 222 | + if (anchor != null) { |
| 223 | + widgetToAdd = anchor; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return widgetToAdd; |
| 228 | + } |
| 229 | + |
| 230 | + interface MyUiBinder extends UiBinder<Widget, AssociationsPanelRepresentationInformation> { |
| 231 | + Widget createAndBindUi(AssociationsPanelRepresentationInformation representationInformationAssociationPanel); |
| 232 | + } |
| 233 | +} |
0 commit comments