[Schmitzm-commits] r2363 - in trunk: schmitzm-core/src/main/java/de/schmitzm/swing/table schmitzm-core/src/test/java/de/schmitzm/lang schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3 schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Thu Jul 18 23:29:19 CEST 2013
Author: mojays
Date: 2013-07-18 23:29:18 +0200 (Thu, 18 Jul 2013)
New Revision: 2363
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/AbstractMutableListPanel.java
trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java
trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java
trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/ID3TagUtil.java
trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV2Panel.java
Log:
AbstractMutableListPanel: vertical growing behavior of table
DBUtil: getOrCreate for non-specific data types
ID3TagUtil: extraction of ID3v2 genre name improved
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/AbstractMutableListPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/AbstractMutableListPanel.java 2013-07-18 11:38:48 UTC (rev 2362)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/AbstractMutableListPanel.java 2013-07-18 21:29:18 UTC (rev 2363)
@@ -75,7 +75,7 @@
protected AbstractMutableListPanel(boolean readonly, boolean initGUI) {
super();
// setLayout( new MigLayout("wrap 2","[grow][]","top") );
- setLayout( new MigLayout("wrap 2, insets 0 0 0 0","[grow][]","top") );
+ setLayout( new MigLayout("wrap 2, insets 0 0 0 0","[grow][]","top, grow") );
this.readonly = readonly;
// ActionListener fuer Buttons
@@ -107,7 +107,7 @@
};
this.table = createListComponent();
- add(new JScrollPane(table),"growx");
+ add(new JScrollPane(table),"growx, growy");
this.buttonPanel = new JPanel();
this.buttonPanel.setLayout(new MigLayout("wrap 1","[grow]","[top]"));
Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java 2013-07-18 11:38:48 UTC (rev 2362)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java 2013-07-18 21:29:18 UTC (rev 2363)
@@ -6,12 +6,11 @@
import static org.junit.Assert.assertTrue;
import java.lang.management.ManagementFactory;
-import java.util.ArrayList;
import java.util.Enumeration;
-import java.util.List;
import java.util.StringTokenizer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
-import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Ignore;
@@ -149,7 +148,7 @@
System.out.println( LangUtil.formatInterval(ManagementFactory.getRuntimeMXBean().getUptime()) );
}
-// @Ignore
+ @Ignore
@Test
public void testQuotedStringTokenizer() {
String text = "Item1, \"Item2\", \"Item3, and, Item4\" bla, \"blub\" bla, \"Item5, item5a\",,\"\",";
@@ -254,9 +253,23 @@
// System.out.println("Plain [" + m.group(2) + "]");
// }
// }
+ }
+
+ @Ignore
+ @Test
+ public void testRegex() {
+ final String[] text = new String[] { "(18)Industrial", "(18)", "(18) ","Industrial","(18)Industrial(19)","()Industrial(19)" };
+ final Pattern p = Pattern.compile("(\\(([\\d]+)\\))?(.*)");
+
+ for (String t : text) {
+ System.out.println("=== '"+t+"' ===");
+ Matcher m = p.matcher(t);
+ if ( m.find() ) {
+ for (int i=1; i<=m.groupCount(); i++)
+ System.out.println("Group "+i+": '"+m.group(i)+"'");
+ }
+ System.out.println();
+ }
-
-
-
}
}
\ No newline at end of file
Modified: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java 2013-07-18 11:38:48 UTC (rev 2362)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java 2013-07-18 21:29:18 UTC (rev 2363)
@@ -242,6 +242,68 @@
}
/**
+ * Retrieves an entity instances which satisfies multiple criteria.
+ * The {@linkplain #getGlobalSession() global session} is used for database access.
+ * @param entityType entity type
+ * @param crit criteria to check as pair of property name (String) and value (Object)
+ */
+ public static <E> E determineEntity(Class<E> entityType, Object... crit) {
+ if ( crit.length % 2 != 0 )
+ throw new IllegalArgumentException("Number of criteria parameters must be even and pairs of property name (String) and value (Object).");
+
+ Criteria query = getGlobalSession().createCriteria(entityType);
+ for (int i=0; i<crit.length; i+=2) {
+ if ( !(crit[i] instanceof String) )
+ throw new IllegalArgumentException("Criteria parameter "+i+" must be a property name (String): "+LangUtil.getSimpleClassName(crit[i]));
+ String propName = (String)crit[i];
+ Object propValue = crit[i+1];
+ query.add(Restrictions.eq(propName, propValue));
+ }
+ List<E> resultList = (List<E>) query.list();
+
+ if (resultList.isEmpty())
+ return null;
+ if (resultList.size() > 1)
+ LOGGER.warn(LangUtil.getSimpleClassName(entityType) + " not unique for criterium");
+
+ return resultList.get(0);
+ }
+
+ /**
+ * Returns an entity instance for the given criteria or creates it if it does not exist.<br>
+ * <b>Note:</b>Because the attributes (specified by criteria) can not be set automatically
+ * new created instances will be "empty"! The attributes must be set afterwards in combination
+ * with an additional {@link Session#saveOrUpdate(String, Object)} call.
+ * @param entitiyType type to determine the instance for (must provide an parameter less
+ * constructor!)
+ * @param session session to use for update (if {@code null} the
+ * {@linkplain #getGlobalSession() global session} is used)
+ * @param saveOrUpdate
+ * if {@code false}, no {@code Session.saveOrUpdate(.)} is performed (on possible
+ * creation). This has to be done explicitly later, e.g. in case when
+ * additional {@link BasicTypeInterface} attributes has to be
+ * filled before insert.
+ * @param crit criteria to check as pair of property name (String) and value (Object)
+ * @see #getGlobalSession()
+ */
+ public static <E extends BasicTypeInterface> E getOrCreateEntity(Session session, Class<E> entitiyType, boolean saveOrUpdate, Object... crit) {
+ if ( session == null )
+ session = getGlobalSession();
+ E instance = determineEntity(entitiyType, crit);
+ if (instance == null) {
+ try {
+ instance = entitiyType.newInstance();
+ } catch (Exception err) {
+ LOGGER.error(err.getMessage(), err);
+ return null;
+ }
+ if ( saveOrUpdate )
+ session.saveOrUpdate(instance);
+ }
+ return instance;
+ }
+
+ /**
* Retrieves all instances of a {@link BasicType} from database. The
* {@linkplain #getGlobalSession() global session} is used for database access.
* @param basicType type to determine the instances for
@@ -289,7 +351,7 @@
return resultList.get(0);
}
- /**
+ /**
* Retrieves the description of all {@link BasicType} instances from database.
* The {@linkplain #getGlobalSession() global session} is used for database access.
*/
Modified: trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/ID3TagUtil.java
===================================================================
--- trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/ID3TagUtil.java 2013-07-18 11:38:48 UTC (rev 2362)
+++ trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/ID3TagUtil.java 2013-07-18 21:29:18 UTC (rev 2363)
@@ -33,6 +33,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.blinkenlights.jid3.ID3Exception;
@@ -43,7 +45,7 @@
import org.blinkenlights.jid3.v2.ContentType;
import org.blinkenlights.jid3.v2.ID3V2Tag;
import org.blinkenlights.jid3.v2.ID3V2_3_0Tag;
-import org.blinkenlights.jid3.v2.MCDIID3V2Frame;
+import org.blinkenlights.jid3.v2.TCONTextInformationID3V2Frame;
import org.blinkenlights.jid3.v2.TXXXTextInformationID3V2Frame;
import de.schmitzm.lang.LangUtil;
@@ -270,6 +272,25 @@
}
/**
+ * Returns the genre name from {@link ID3V2Tag}.
+ * @return {@code null} if there is no such information
+ */
+ public static String getGenreName(ID3V2Tag tag) {
+ String genreName = null;
+ // For ID3V2_3_0Tag getGenre() returns ContentType.toString() which may include
+ // some other information (besides the genre name)
+ if ( tag instanceof ID3V2_3_0Tag ) {
+ TCONTextInformationID3V2Frame oTCON = ((ID3V2_3_0Tag)tag).getTCONTextInformationFrame();
+ genreName = oTCON != null ? oTCON.getContentType().getRefinement() : null;
+ if (!StringUtils.isBlank(genreName))
+ return genreName;
+ // if genre name (refinement) is empty, use the parser
+ }
+
+ return parseGenreFromID3v2( tag.getGenre() );
+ }
+
+ /**
* Returns the total number of tracks from an {@link ID3V2Tag}.
* @return {@code null} if there is no such information (instead of
* {@link ID3Exception})
@@ -307,6 +328,35 @@
return null;
return audioCDFrame.getAudioCDInformation();
}
+ public static final Pattern GENRE_PATTERN = Pattern.compile("(\\(([\\d]+)\\))?(.*)");
+
+ /**
+ * Some applications (e.g. WinAmp) store the genre index in combination
+ * with the textual information (e.g. "(19)Industrial"). This method parses
+ * the genre string and returns the textual genre name.
+ * If no textual genre name is given, but genre index, the method returns
+ * the respective element from {@link #ALL_GENRE_TITLES_ID3V2}.
+ */
+ public static String parseGenreFromID3v2(String genreStr) {
+ if ( genreStr == null )
+ return null;
+
+ Matcher m = GENRE_PATTERN.matcher(genreStr);
+ if ( !m.find() )
+ return genreStr;
+
+ String genreIdxStr = m.group(2);
+ Integer genreIdx = genreIdxStr != null ? Integer.parseInt(genreIdxStr) : null;
+ String genreName = m.group(3);
+
+ // Prio 1: if present use specified genre name
+ if ( !StringUtils.isBlank(genreName) )
+ return genreName;
+ // Prio 2: if genre index is present, return respective genre name
+ if ( genreIdx == null || genreIdx >= ALL_GENRE_TITLES_ID3V2.length )
+ return null;
+ return ALL_GENRE_TITLES_ID3V2[genreIdx];
+ }
/**
* Returns a basic field from ID3-Tag regardless of the ID3Tag version.
@@ -359,8 +409,14 @@
Genre genre = ((ID3V1Tag)tag).getGenre();
return genre != null ? genre.toString() : null;
}
- if ( tag instanceof ID3V2Tag )
- return ((ID3V2Tag)tag).getGenre();
+ if ( tag instanceof ID3V2Tag ) {
+ String genreStr = ((ID3V2Tag)tag).getGenre();
+ // Some applications (e.g. WinAmp) store the Genre index in combination
+ // with the textual information, e.g. "(19)Industrial"
+ // -> extract the real genre name
+ String genre = parseGenreFromID3v2(genreStr);
+ return genre;
+ }
break;
case YEAR: Integer year = null;
if ( tag instanceof ID3V1Tag ) {
@@ -647,7 +703,7 @@
newTag.setAlbum( StringUtils.trimToEmpty(tag.getAlbum()) );
newTag.setArtist( StringUtils.trimToEmpty(tag.getArtist()) );
newTag.setComment( StringUtils.trimToEmpty(tag.getComment()) );
- newTag.setGenre( tag.getGenre() == null ? null : Genre.lookupGenre(tag.getGenre()) );
+ newTag.setGenre( getGenreName(tag) == null ? null : Genre.lookupGenre( getGenreName(tag)) );
newTag.setTitle( StringUtils.trimToEmpty(tag.getTitle()) );
Integer year = getYear(tag);
newTag.setYear( year == null ? "" : year.toString() );
Modified: trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV2Panel.java
===================================================================
--- trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV2Panel.java 2013-07-18 11:38:48 UTC (rev 2362)
+++ trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV2Panel.java 2013-07-18 21:29:18 UTC (rev 2363)
@@ -141,7 +141,7 @@
if ( force || yearCheckbox.getValue() )
year.setValue( ID3TagUtil.getYear(tag) );
if ( force || genreCheckbox.getValue() )
- genre.setSelectedDisplayItem( tag.getGenre() );
+ genre.setSelectedDisplayItem( ID3TagUtil.getGenreName(tag) );
if ( force || commentCheckbox.getValue() )
comment.setValue( tag.getComment() );
if ( force || audioCDInfoCheckbox.getValue() )
More information about the Schmitzm-commits
mailing list