Browse Source

implementacja wyszukiwania (wzorzec Strategy)

kilka zmian
master
Piotr Dergun 10 years ago
parent
commit
ce03bfe1eb
3 changed files with 81 additions and 34 deletions
  1. +37
    -20
      src/main/java/db/Cookbook.java
  2. +24
    -3
      src/main/java/ksiazka/kucharska/FrmMain.java
  3. +20
    -11
      src/main/java/ksiazka/kucharska/RetrieveData.java

+ 37
- 20
src/main/java/db/Cookbook.java View File

@ -54,18 +54,8 @@ public class Cookbook {
} }
/* funkcja przestarzała (deprecated) - do usunięcia w bliskiej przyszłości - PioDer */ /* funkcja przestarzała (deprecated) - do usunięcia w bliskiej przyszłości - PioDer */
public boolean insertRecipe(String name, String ingredients, String description, String tags, ImageIcon img) {
try {
//przekształcam ImageIcon -> BuforowanyImg
BufferedImage bImg = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
bImg.getGraphics().drawImage(img.getImage(), 0,0, null);
//tworzę strumień bajtowy
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
//zapisuję do strumienia bufor obrazka
ImageIO.write( bImg, "png", bStream );
bStream.flush();
public boolean insertRecipe(String name, String ingredients, String description, String tags, byte[] imgArr) {
try {
PreparedStatement prepStmt = conn.prepareStatement( PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?, ?);"); "INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?, ?);");
@ -73,20 +63,14 @@ public class Cookbook {
prepStmt.setString(2, ingredients); prepStmt.setString(2, ingredients);
prepStmt.setString(3, description); prepStmt.setString(3, description);
prepStmt.setString(4, tags); prepStmt.setString(4, tags);
prepStmt.setBytes(5, bStream.toByteArray());
prepStmt.setBytes(5, imgArr);
prepStmt.execute(); prepStmt.execute();
bStream.close();
} }
catch (SQLException e) { catch (SQLException e) {
System.err.println("Blad przy wstawianiu przepisu"); System.err.println("Blad przy wstawianiu przepisu");
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
catch (IOException e) {
System.err.println("Blad przy wstawianiu przepisu");
e.printStackTrace();
return false;
}
return true; return true;
} }
@ -147,7 +131,7 @@ public class Cookbook {
return true; return true;
} }
public List<String> selectNames(){
public List<String> obtainNames(){
List<String> names = new LinkedList<String>(); List<String> names = new LinkedList<String>();
try { try {
ResultSet result = stat.executeQuery("SELECT name FROM recipes"); ResultSet result = stat.executeQuery("SELECT name FROM recipes");
@ -163,6 +147,39 @@ public class Cookbook {
return names; return names;
} }
public List<String> obtainSearchedNames(String n, String t){
List<String> names = new LinkedList<String>();
try {
String whereClause = "";
if (n != "")
whereClause += "name LIKE ?";
if (t != "")
{
if (n != "")
whereClause += " OR ";
whereClause += "tags LIKE ?";
}
PreparedStatement query = conn.prepareStatement("SELECT name FROM recipes WHERE " + whereClause);
if (n != "")
query.setString(1, "%"+n+"%");
if (t != "")
query.setString(((n!="")? 2 : 1), "%"+t+"%");
ResultSet result = query.executeQuery();
String name;
while(result.next()){
name = result.getString("name");
names.add(name);
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return names;
}
public void closeConnection() { public void closeConnection() {
try { try {
conn.close(); conn.close();

+ 24
- 3
src/main/java/ksiazka/kucharska/FrmMain.java View File

@ -35,11 +35,16 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import ksiazka.kucharska.RetrieveData; import ksiazka.kucharska.RetrieveData;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.ScrollPaneConstants; import javax.swing.ScrollPaneConstants;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
public class FrmMain extends JFrame { public class FrmMain extends JFrame {
@ -48,7 +53,7 @@ public class FrmMain extends JFrame {
*/ */
private static final long serialVersionUID = 2634383304749703669L; private static final long serialVersionUID = 2634383304749703669L;
private JPanel contentPane; private JPanel contentPane;
private JTextField txtSearch;
public static JTextField txtSearch;
public static JTextField txtEdTitle; public static JTextField txtEdTitle;
public static JTextField txtTags; public static JTextField txtTags;
private JButton btnSave; private JButton btnSave;
@ -124,7 +129,7 @@ public class FrmMain extends JFrame {
setContentPane(contentPane); setContentPane(contentPane);
JSplitPane splitPane = new JSplitPane(); JSplitPane splitPane = new JSplitPane();
splitPane.setResizeWeight(0.18);
splitPane.setResizeWeight(0.1);
splitPane.setRequestFocusEnabled(false); splitPane.setRequestFocusEnabled(false);
splitPane.setPreferredSize(new Dimension(200, 27)); splitPane.setPreferredSize(new Dimension(200, 27));
splitPane.setDividerSize(5); splitPane.setDividerSize(5);
@ -142,7 +147,8 @@ public class FrmMain extends JFrame {
panel.setLayout(gbl_panel); panel.setLayout(gbl_panel);
txtSearch = new JTextField(); txtSearch = new JTextField();
txtSearch.setText("wpisz tytuł/tagi...");
txtSearch.setText("");
GridBagConstraints gbc_txtSearch = new GridBagConstraints(); GridBagConstraints gbc_txtSearch = new GridBagConstraints();
gbc_txtSearch.gridwidth = 2; gbc_txtSearch.gridwidth = 2;
gbc_txtSearch.insets = new Insets(5, 5, 5, 0); gbc_txtSearch.insets = new Insets(5, 5, 5, 0);
@ -352,5 +358,20 @@ public class FrmMain extends JFrame {
//RetrieveData.insertSampleData(); //RetrieveData.insertSampleData();
RetrieveData.fillList(); RetrieveData.fillList();
txtSearch.setText("wpisz tytuł/tagi...");
txtSearch.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
RetrieveData.fillList();
}
public void insertUpdate(DocumentEvent e) {
RetrieveData.fillList();
}
public void removeUpdate(DocumentEvent e) {
RetrieveData.fillList();
}
});
} }
} }

+ 20
- 11
src/main/java/ksiazka/kucharska/RetrieveData.java View File

@ -3,11 +3,12 @@ package ksiazka.kucharska;
import java.util.List; import java.util.List;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import db.AllRecipes;
import db.Cookbook; import db.Cookbook;
import db.Recipe; import db.Recipe;
import ksiazka.kucharska.FrmMain;
import db.SearchRecipe;
import db.SearchRecipeNameTag;
public class RetrieveData extends FrmMain{ public class RetrieveData extends FrmMain{
/** /**
@ -16,22 +17,30 @@ public class RetrieveData extends FrmMain{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static void insertSampleData(){ public static void insertSampleData(){
Cookbook myCb = new Cookbook(); Cookbook myCb = new Cookbook();
myCb.insertRecipe("Kanapka",
"Chleb, margaryna, szynka",
"Posmarować kanapke margaryną i położyć na niej plaster szynki",
"kanapka, szynka, chleb", imgDefaultRecipe);
myCb.insertRecipe("Kanapka z sererm",
"Chleb, margaryna, ser",
"Posmarować kanapke margaryną i położyć na niej plaster sera",
"kanapka, ser, chleb", imgDefaultRecipe);
RecipeManager rm = new RecipeManager();
rm.setRecipeBuilder(new ExampleRecipeBuilder());
rm.constructRecipe();
Recipe rc = rm.getRecipe();
myCb.insertRecipe(rc);
myCb.closeConnection(); myCb.closeConnection();
} }
public static void fillList(){ public static void fillList(){
Cookbook myCb = new Cookbook(); Cookbook myCb = new Cookbook();
List<String> names = myCb.selectNames();
SearchRecipe sr;
if (txtSearch.getText() != "") {
sr = new SearchRecipeNameTag();
}
else
{
sr = new AllRecipes();
}
List<String> names = sr.obtainRecipeNames();
DefaultListModel recipesList; DefaultListModel recipesList;
recipesList = getRecipesList(); recipesList = getRecipesList();
recipesList.clear();
for (String r : names) for (String r : names)
{ {

Loading…
Cancel
Save