Changeset 3454


Ignore:
Timestamp:
Jun 6, 2007, 11:24:02 PM (16 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #616

Location:
trunk/src
Files:
2 edited

Legend:

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

    r3049 r3454  
    282282  }
    283283 
     284 
     285  /**
     286    Check if an item has a link with a given name.
     287 
     288    @param dc The <code>DbControl</code> which will be used for
     289      permission checking and database access.
     290    @param from The item the link is linking from
     291    @param name The name of the link
     292    @return TRUE if the link exists, FALSE otherwise
     293    @throws InvalidDataException If the from or name parameter is null
     294    @throws BaseException If there is another error
     295    @since 2.4
     296  */
     297  public static boolean exists(DbControl dc, BasicItem from, String name)
     298    throws InvalidDataException, BaseException
     299  {
     300    if (from == null) throw new InvalidUseOfNullException("from");
     301    if (name == null) throw new InvalidUseOfNullException("name");
     302    org.hibernate.Query query = HibernateUtil.getPredefinedQuery(dc.getHibernateSession(),
     303      "GET_ANYTOANY_FOR_NAME");
     304    /*
     305      SELECT ata
     306      FROM AnyToAnyData ata
     307      WHERE ata.fromId = :fromId AND ata.fromType = :fromType AND ata.name = :name
     308    */
     309    query.setInteger("fromId", from.getId());
     310    query.setInteger("fromType", from.getType().getValue());
     311    query.setString("name", name);
     312    AnyToAny ata = dc.getItem(AnyToAny.class, HibernateUtil.loadData(AnyToAnyData.class, query));
     313    return ata != null;
     314  }
     315 
    284316  /**
    285317    Delete all links that are linking to non-existing applications. This method
  • trunk/src/test/TestAnyToAny.java

    r2382 r3454  
    5656    test_list_from(sampleId, 2);
    5757    test_list_to(fileId1, 1);
     58    test_exists(sampleId, "first", true);
     59    test_exists(sampleId, "third", false);
     60   
    5861
    5962    // Extra tests:
     
    259262    }
    260263  }
     264 
     265  static void test_exists(int sampleId, String name, boolean expected)
     266  {
     267    if (sampleId == 0) return;
     268    DbControl dc = null;
     269    try
     270    {
     271      dc = TestUtil.getDbControl();
     272      Sample s = Sample.getById(dc, sampleId);
     273      if (AnyToAny.exists(dc, s, name) != expected)
     274      {
     275        throw new BaseException("Expected exists=" + expected + " for link=" + name);
     276      }
     277      write("--Exists any-to-any (" + name + "=" + expected + ") OK");
     278    }
     279    catch (Throwable ex)
     280    {
     281      write("--Exists any-to-any FAILED");
     282      ex.printStackTrace();
     283      ok = false;
     284    }
     285    finally
     286    {
     287      if (dc != null) dc.close();
     288    }
     289  }
    261290
    262291 
Note: See TracChangeset for help on using the changeset viewer.