|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2019 LabKey Corporation |
| 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 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 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.labkey.api.ldk.buttons; |
| 17 | + |
| 18 | +import org.labkey.api.data.TableInfo; |
| 19 | +import org.labkey.api.ldk.LDKService; |
| 20 | +import org.labkey.api.ldk.table.SimpleButtonConfigFactory; |
| 21 | +import org.labkey.api.module.Module; |
| 22 | +import org.labkey.api.security.permissions.Permission; |
| 23 | +import org.labkey.api.util.PageFlowUtil; |
| 24 | + |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +/** |
| 28 | + * User: bimber |
| 29 | + * Date: 7/14/13 |
| 30 | + * Time: 4:05 PM |
| 31 | + */ |
| 32 | +public class ShowEditUIButton extends SimpleButtonConfigFactory |
| 33 | +{ |
| 34 | + protected String _schemaName; |
| 35 | + protected String _queryName; |
| 36 | + private Map<String, String> _urlParamMap = null; |
| 37 | + private boolean _copyFilters = true; |
| 38 | + |
| 39 | + protected Class<? extends Permission>[] _perms; |
| 40 | + |
| 41 | + public ShowEditUIButton(Module owner, String schemaName, String queryName, Class<? extends Permission>... perms) |
| 42 | + { |
| 43 | + this(owner, schemaName, queryName, "Edit Records", perms); |
| 44 | + } |
| 45 | + |
| 46 | + public ShowEditUIButton(Module owner, String schemaName, String queryName, String label, Class<? extends Permission>... perms) |
| 47 | + { |
| 48 | + super(owner, label, ""); |
| 49 | + |
| 50 | + _schemaName = schemaName; |
| 51 | + _queryName = queryName; |
| 52 | + _perms = perms; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean isAvailable(TableInfo ti) |
| 57 | + { |
| 58 | + if (!super.isAvailable(ti)) |
| 59 | + return false; |
| 60 | + |
| 61 | + for (Class<? extends Permission> perm : _perms) |
| 62 | + { |
| 63 | + if (!ti.getUserSchema().getContainer().hasPermission(ti.getUserSchema().getUser(), perm)) |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + public void setCopyFilters(boolean copyFilters) |
| 71 | + { |
| 72 | + _copyFilters = copyFilters; |
| 73 | + } |
| 74 | + |
| 75 | + public void setUrlParamMap(Map<String, String> urlParamMap) |
| 76 | + { |
| 77 | + _urlParamMap = urlParamMap; |
| 78 | + } |
| 79 | + |
| 80 | + protected String getHandlerName() |
| 81 | + { |
| 82 | + return "LDK.Utils.editUIButtonHandler"; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected String getJsHandler(TableInfo ti) |
| 87 | + { |
| 88 | + String schema = _schemaName == null || LDKService.ALL_SCHEMAS.equals(_schemaName) ? ti.getPublicSchemaName() : _schemaName; |
| 89 | + String query = _queryName == null || LDKService.ALL_TABLES.equalsIgnoreCase(_queryName) ? ti.getPublicName() : _queryName; |
| 90 | + String ret = getHandlerName() + "(" + PageFlowUtil.jsString(schema) + "," + PageFlowUtil.jsString(query) + ",dataRegionName, {"; |
| 91 | + |
| 92 | + String delim = ""; |
| 93 | + if (_urlParamMap != null) |
| 94 | + { |
| 95 | + for (String key : _urlParamMap.keySet()) |
| 96 | + { |
| 97 | + ret += delim + PageFlowUtil.jsString(key) + ":" + PageFlowUtil.jsString(_urlParamMap.get(key)); |
| 98 | + delim = ","; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + ret += "}, " + _copyFilters + ");"; |
| 103 | + |
| 104 | + return ret; |
| 105 | + } |
| 106 | +} |
0 commit comments