Changeset 7853


Ignore:
Timestamp:
Oct 19, 2020, 9:33:29 AM (14 months ago)
Author:
Nicklas Nordborg
Message:

References #2222: Add support for manually adding any-to-any link column in table lists

Implemented a function for generating a list of presets from existing any-to-any links. The current table list context (including active filters) is used for sampling a random number items that the current user has access to and then those items are used for checking links.

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/AnyToAny.java

    r7381 r7853  
    2424
    2525import java.util.ArrayList;
     26import java.util.Collections;
    2627import java.util.List;
    2728
     
    3536import net.sf.basedb.core.query.Restrictions;
    3637import net.sf.basedb.core.signal.ThreadSignalHandler;
     38import net.sf.basedb.util.AnyToAnyLinkStatistics;
     39import net.sf.basedb.util.AnyToAnyLinkStatistics.Options;
    3740
    3841/**
     
    341344  }
    342345
     346  /**
     347    Get information about existing links from the items that are returned by
     348    the given query. If the query returns a lot of items a max number of samples
     349    to be used may be specified. The samples are randomly selected. See
     350    {@link Options} for more information.
     351    @return A list with statistics about links (can be empty)
     352    @since 3.17
     353  */
     354  public static List<AnyToAnyLinkStatistics> getExistingLinkStatistics(DbControl dc, ItemQuery<?> query, Options options)
     355  {
     356    List<Integer> idList = query.idList(dc);
     357    if (idList.size() == 0) return Collections.emptyList();
     358   
     359    if (options == null) options = new Options();
     360    if (options.getMaxRandomSamples() > 0 && idList.size() > options.getMaxRandomSamples())
     361    {
     362      // Shuffle the list and then use the first 'maxRandomSamples' in it.
     363      Collections.shuffle(idList);
     364      idList = idList.subList(0, options.getMaxRandomSamples());
     365    }
     366    int minCount = Math.max(1, Math.min(options.getMinCount(), idList.size() / options.getMinFraction()));
     367    int sampleSize = idList.size();
     368   
     369    String hql = "SELECT t.name, t.toType, COUNT(*)";
     370    hql += " FROM AnyToAnyData t ";
     371    hql += " WHERE t.fromType="+query.getItemType().getValue();
     372    hql += " AND t.fromId IN (" + net.sf.basedb.util.Values.getString(idList, ",", true) + ")";
     373    hql += " GROUP BY t.name, t.toType";
     374    hql += " HAVING COUNT(*) >= " + minCount;
     375    hql += " ORDER BY COUNT(*), t.name";
     376   
     377    org.hibernate.query.Query<Object[]> ataquery = HibernateUtil.createQuery(dc.getHibernateSession(), hql);
     378    List<Object[]> list = HibernateUtil.loadList(ataquery, dc.getSessionControl());   
     379    List<AnyToAnyLinkStatistics> stats = new ArrayList<>(list.size());
     380    for (Object[] row : list)
     381    {
     382      AnyToAnyLinkStatistics stat = new AnyToAnyLinkStatistics();
     383      stat.setSampleSize(sampleSize);
     384      stat.setLinkName((String)row[0]);
     385      stat.setTargetType(Item.fromValue((Integer)row[1]));
     386      stat.setCount(((Long)row[2]).intValue());
     387      stats.add(stat);
     388    }
     389    return stats;
     390  }
     391 
    343392  /**
    344393    Delete all links that are linking to non-existing items. This method
  • trunk/src/core/net/sf/basedb/core/HibernateUtil.java

    r7729 r7853  
    18891889  */
    18901890  @SuppressWarnings({ "unchecked", "rawtypes" })
    1891   static Query<?> createQuery(Session session, String hql)
     1891  static <T> Query<T> createQuery(Session session, String hql)
    18921892    throws BaseException
    18931893  {
  • trunk/www/common/columns/add_linkeditem_column.js

    r7852 r7853  
    3333  configure.initPage = function()
    3434  {
     35    Events.addEventHandler('presets', 'change', configure.presetSelected);
    3536    Events.addEventHandler(document.body, 'click', configure.hideMessage);
    3637   
     
    3839    Buttons.addClickHandler('close', App.closeWindow);
    3940    Buttons.addClickHandler('btnAdd', configure.addColumn);
     41  }
     42 
     43  configure.presetSelected = function(event)
     44  {
     45    var frm = document.forms['linkedItems'];
     46   
     47    if (frm.presets.selectedIndex > 0)
     48    {
     49      var selected = frm.presets[frm.presets.selectedIndex];
     50      frm.linkName.value = Data.get(selected, 'linkname');
     51      Forms.selectListOption(frm.targetItemType, Data.get(selected, 'targettype'));
     52      frm.presets.selectedIndex = 0;
     53    }
    4054  }
    4155 
  • trunk/www/common/columns/add_linkeditem_column.jsp

    r7852 r7853  
    2222--%>
    2323<%@ page pageEncoding="UTF-8" session="false"
     24  import="net.sf.basedb.core.AnyToAny"
     25  import="net.sf.basedb.core.DbControl"
    2426  import="net.sf.basedb.core.SessionControl"
    2527  import="net.sf.basedb.core.Item"
     28  import="net.sf.basedb.core.ItemQuery"
    2629  import="net.sf.basedb.core.ItemContext"
    27   import="net.sf.basedb.core.Metadata" 
     30  import="net.sf.basedb.core.Metadata"
     31  import="net.sf.basedb.util.AnyToAnyLinkStatistics"
    2832  import="net.sf.basedb.clients.web.Base"
    2933  import="net.sf.basedb.clients.web.util.HTML"
     
    3741final String ID = sc.getId();
    3842final Item itemType = Item.valueOf(request.getParameter("item_type"));
     43final String subContext = Values.getString(request.getParameter("subcontext"), "");
    3944final ItemContext cc = Base.getAndSetCurrentContext(sc, Item.ANYTOANY, null, null);
    40 %>
    41 <base:page type="popup" title="Add linked item column">
    42 <base:head scripts="~add_linkeditem_column.js" />
    43 <base:body>
    44   <h1>Add linked item column <base:help helpid="columns.configure.add_linkeditem_column" /></h1>
    45 
    46   <form name="linkedItems" method="post">
    47   <input type="hidden" name="ID" value="<%=ID%>">
    48   <input type="hidden" name="item_type" value="<%=itemType.name()%>">
     45final DbControl dc = sc.newDbControl();
     46try
     47{
     48  ItemQuery<?> statQuery = itemType.getQuery();
     49  ItemContext ccList = Base.getAndSetCurrentContext(sc, itemType, subContext, null, null);
     50  ccList.configureQuery(dc, statQuery, true);
     51  statQuery.setMaxResults(-1);
     52  statQuery.setFirstResult(0);
     53  final List<AnyToAnyLinkStatistics> stats = AnyToAny.getExistingLinkStatistics(dc, statQuery, new AnyToAnyLinkStatistics.Options());
     54  %>
     55  <base:page type="popup" title="Add linked item column">
     56  <base:head scripts="~add_linkeditem_column.js" />
     57  <base:body>
     58    <h1>Add linked item column <base:help helpid="columns.configure.add_linkeditem_column" /></h1>
    4959 
    50   <div class="content">
    51     <table class="fullform input100 bottomborder">
    52     <tr>
    53       <th>Link name</th>
    54       <td>
    55         <input class="text required auto-init" data-auto-init="focus"
    56           type="text" name="linkName" id="linkName">
    57       </td>
    58     </tr>
    59     <tr>
    60       <th>Target item type</th>
    61       <td>
    62         <select name="targetItemType" id="targetItemType" style="min-width: 15em;">
    63         <option value="">- any -
    64         <%
    65         List<String> recentTypes = cc.getRecent("toTypes");
    66         if (recentTypes.size() == 0)
    67         {
    68           recentTypes = Arrays.asList("FILE");
    69         }
    70         %>
    71         <option value="0" disabled class="recentheader">- recently used -
    72         <%
    73         for (String r : recentTypes)
    74         {
     60    <form name="linkedItems" method="post">
     61    <input type="hidden" name="ID" value="<%=ID%>">
     62    <input type="hidden" name="item_type" value="<%=itemType.name()%>">
     63   
     64    <div class="content">
     65      <table class="fullform input100 bottomborder">
     66      <tr>
     67        <th></th>
     68        <td>
     69          <select name="presets" id="presets" style="width: 25em;">
     70          <option value="">- presets -
     71          <%
     72          for (AnyToAnyLinkStatistics stat : stats)
     73          {
     74            String name = HTML.encodeTags(stat.getLinkName());
     75            String targetType = stat.getTargetType().name();
     76            %>
     77            <option data-linkname="<%=name%>" data-targettype="<%=targetType%>"><%=name%> [<%=targetType%>]
     78            <%
     79          }
    7580          %>
    76           <option value="<%=r%>"><%=r%>
     81          </select>
     82        </td>
     83      </tr>
     84      <tr>
     85        <th style="border-top-width: 0;">Link name</th>
     86        <td>
     87          <input class="text required auto-init" data-auto-init="focus"
     88            type="text" name="linkName" id="linkName" style="width: 25em;">
     89        </td>
     90      </tr>
     91      <tr>
     92        <th>Target item type</th>
     93        <td>
     94          <select name="targetItemType" id="targetItemType" style="min-width: 25em;">
     95          <option value="">- any -
    7796          <%
    78         }
    79         %>
    80         <option value="0" disabled class="recentheader">- all -
    81         <%
    82         for (Item targetItem : Metadata.getNameableItems())
    83         {
    84           if (targetItem.getDefinedPermissions() != null)
     97          List<String> recentTypes = cc.getRecent("toTypes");
     98          if (recentTypes.size() == 0)
     99          {
     100            recentTypes = Arrays.asList("FILE");
     101          }
     102          %>
     103          <option value="0" disabled class="recentheader">- recently used -
     104          <%
     105          for (String r : recentTypes)
    85106          {
    86107            %>
    87             <option value="<%=targetItem.name() %>"><%=targetItem.name() %>
     108            <option value="<%=r%>"><%=r%>
    88109            <%
    89110          }
    90         }
    91         %>
    92         </select>
    93       </td>
    94     </tr>
    95     <tr class="dynamic">
    96       <th></th>
    97       <td><div id="added-column-msg" class="messagecontainer note" style="display:none; margin: 0.5em 0;"></div></td>
    98     </tr>
    99     </table>
    100   </div>
    101   </form>
    102 
    103   <base:buttongroup subclass="dialogbuttons">
    104     <base:button id="btnAdd" title="Add" />
    105     <base:button id="close" title="Close" />
    106   </base:buttongroup>
    107 
    108 </base:body>
    109 </base:page>
     111          %>
     112          <option value="0" disabled class="recentheader">- all -
     113          <%
     114          for (Item targetItem : Metadata.getNameableItems())
     115          {
     116            if (targetItem.getDefinedPermissions() != null)
     117            {
     118              %>
     119              <option value="<%=targetItem.name() %>"><%=targetItem.name() %>
     120              <%
     121            }
     122          }
     123          %>
     124          </select>
     125        </td>
     126      </tr>
     127      <tr class="dynamic">
     128        <th></th>
     129        <td><div id="added-column-msg" class="messagecontainer note" style="display:none; margin: 0.5em 0;"></div></td>
     130      </tr>
     131      </table>
     132    </div>
     133    </form>
     134 
     135    <base:buttongroup subclass="dialogbuttons">
     136      <base:button id="btnAdd" title="Add" />
     137      <base:button id="close" title="Close" />
     138    </base:buttongroup>
     139 
     140  </base:body>
     141  </base:page>
     142  <%
     143}
     144finally
     145{
     146  if (dc != null) dc.close();
     147}
     148%>
    110149
    111150
  • trunk/www/common/columns/configure.js

    r7851 r7853  
    248248    var url = 'add_linkeditem_column.jsp?ID='+App.getSessionId();
    249249    url += '&item_type='+Data.get('page-data', 'item-type');
     250    url += '&subcontext='+encodeURIComponent(Data.get('page-data', 'subcontext') || '');
    250251    Dialogs.openPopup(url, 'SelectLinkedItemColumn', 600, 400);
    251252  }
Note: See TracChangeset for help on using the changeset viewer.