Skip to content

Commit 55c68e1

Browse files
committed
Add abstract implementation for actions
This commit adds the interface `IAction` and the abstract class `AbstractAction` as a base for supporting multiple actions without much effort for implemeting new ones. The current baked-in possibility of moving files from the source to the target directory has been migrated. See e4b3872 See d9b7b2d
1 parent d9b7b2d commit 55c68e1

File tree

4 files changed

+231
-49
lines changed

4 files changed

+231
-49
lines changed

PatternFileMover/Action/IAction.cs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Windows.Forms;
6+
7+
namespace PatternFileMover.Action
8+
{
9+
abstract class Action
10+
{
11+
public abstract bool ExecuteAction();
12+
13+
public abstract bool Matches(DataGridViewRow row);
14+
15+
public abstract void Prepare();
16+
}
17+
18+
abstract class AbstractAction : Action
19+
{
20+
private readonly List<NameAssociationsData_v3> associations;
21+
private NameAssociationsData_v3 currentAssociation;
22+
private DataGridViewRow currentDataGridViewRow { get; set; }
23+
private string sourcePath;
24+
25+
protected AbstractAction()
26+
{
27+
this.associations = new List<NameAssociationsData_v3>();
28+
this.associations = NameAssociations.LoadFromExistingConfigFile();
29+
}
30+
31+
public override bool ExecuteAction()
32+
{
33+
Type type = this.GetType();
34+
35+
MethodInfo methodInfoAction = type.GetMethod(
36+
"Do" + this.GetType().Name,
37+
BindingFlags.Public | BindingFlags.Instance
38+
);
39+
40+
MethodInfo methodInfoMatches = type.GetMethod(
41+
"Matches",
42+
BindingFlags.Public | BindingFlags.Instance
43+
);
44+
45+
MethodInfo methodInfoPrepare = type.GetMethod(
46+
"Prepare",
47+
BindingFlags.Public | BindingFlags.Instance
48+
);
49+
50+
if (methodInfoAction == null || methodInfoMatches == null || methodInfoPrepare == null)
51+
{
52+
throw new NotImplementedException();
53+
}
54+
55+
foreach (NameAssociationsData_v3 _association in this.associations)
56+
{
57+
this.currentAssociation = _association;
58+
59+
var matches = (bool) methodInfoMatches.Invoke(
60+
this,
61+
new object[] { this.currentDataGridViewRow }
62+
);
63+
64+
if (matches)
65+
{
66+
methodInfoPrepare.Invoke(this, new object[] { });
67+
return (bool) methodInfoAction.Invoke(this, new object[] { });
68+
}
69+
}
70+
71+
return false;
72+
}
73+
74+
public override bool Matches(DataGridViewRow row)
75+
{
76+
this.currentDataGridViewRow = row;
77+
78+
if (this.currentAssociation.Action.ToString() == this.GetType().Name)
79+
{
80+
return true;
81+
}
82+
83+
return false;
84+
}
85+
86+
public override void Prepare()
87+
{
88+
this.sourcePath = this.currentDataGridViewRow.Cells[0].Value.ToString();
89+
}
90+
91+
protected DataGridViewRow GetCurrent()
92+
{
93+
return this.currentDataGridViewRow;
94+
}
95+
96+
protected string GetFileExtension()
97+
{
98+
return this.currentAssociation.FileExtension;
99+
}
100+
101+
protected string GetSearchPattern()
102+
{
103+
return this.currentAssociation.SearchPattern;
104+
}
105+
106+
protected string GetSourcePath()
107+
{
108+
return this.sourcePath;
109+
}
110+
111+
protected string GetTargetPath()
112+
{
113+
return this.currentAssociation.TargetDirectory;
114+
}
115+
116+
public void SetCurrent(DataGridViewRow row)
117+
{
118+
this.currentDataGridViewRow = row;
119+
}
120+
121+
public static List<Type> GetActions()
122+
{
123+
Type[] availableActions = Assembly.GetExecutingAssembly().GetTypes().Where(
124+
t => String.Equals(t.Namespace, "PatternFileMover.Action", StringComparison.Ordinal)
125+
).ToArray();
126+
127+
var list = new List<Type>();
128+
foreach (var action in availableActions)
129+
{
130+
if (action.BaseType.ToString() == "PatternFileMover.Action.AbstractAction")
131+
{
132+
list.Add(action);
133+
}
134+
}
135+
136+
return list;
137+
}
138+
}
139+
}

PatternFileMover/Action/Move.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.IO;
3+
using System.Windows.Forms;
4+
5+
namespace PatternFileMover.Action
6+
{
7+
internal class Move : AbstractAction
8+
{
9+
public bool DoMove() {
10+
File.Move(
11+
this.GetSourcePath(),
12+
this.GetTargetPath() +
13+
Path.DirectorySeparatorChar +
14+
Path.GetFileName(this.GetCurrent().Cells[0].Value.ToString()
15+
)
16+
);
17+
18+
return true;
19+
}
20+
21+
public override bool ExecuteAction()
22+
{
23+
return base.ExecuteAction();
24+
}
25+
26+
public override bool Matches(DataGridViewRow row)
27+
{
28+
base.Matches(row);
29+
30+
if (
31+
(
32+
this.GetFileExtension() == "*.*" &&
33+
Path.GetFileNameWithoutExtension(
34+
this.GetCurrent().Cells[(int)NameAssociationCellIndex.Name].Value.ToString()
35+
).Contains(this.GetSearchPattern())
36+
) ||
37+
(
38+
Path.GetFileNameWithoutExtension(
39+
this.GetCurrent().Cells[(int)NameAssociationCellIndex.Name].Value.ToString()
40+
).Contains(this.GetSearchPattern()) &&
41+
this.GetFileExtension() == Path.GetExtension(this.GetCurrent().Cells[(int)NameAssociationCellIndex.Name].Value.ToString())
42+
) &&
43+
(
44+
Directory.Exists(this.GetTargetPath() + Path.DirectorySeparatorChar)
45+
)
46+
)
47+
{
48+
return true;
49+
}
50+
51+
return false;
52+
}
53+
54+
public override void Prepare()
55+
{
56+
base.Prepare();
57+
58+
if (File.Exists(
59+
this.GetTargetPath() +
60+
Path.DirectorySeparatorChar +
61+
Path.GetFileName(this.GetCurrent().Cells[0].Value.ToString())
62+
)
63+
)
64+
{
65+
// delete the existing file before it gets replaced with the current
66+
// processed file
67+
File.Delete(
68+
this.GetTargetPath() +
69+
Path.DirectorySeparatorChar +
70+
Path.GetFileName(this.GetCurrent().Cells[0].Value.ToString())
71+
);
72+
}
73+
}
74+
}
75+
}

PatternFileMover/Form1.cs

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using PatternFileMover.Action;
2+
using System;
23
using System.Collections.Generic;
34
using System.ComponentModel;
45
using System.IO;
@@ -105,59 +106,25 @@ private void editNameAssociationsToolStripMenuItem_Click(object sender, EventArg
105106
}
106107

107108
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs eventArgs)
108-
{
109+
{
110+
var types = AbstractAction.GetActions();
111+
109112
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
110113
{
111-
foreach (NameAssociationsData_v3 data in this.nameAssociations)
112-
{
113-
if (
114-
(
115-
data.FileExtension == "*.*" &&
116-
Path.GetFileNameWithoutExtension(
117-
dataGridView1.Rows[i].Cells[(int)NameAssociationCellIndex.Name].Value.ToString()
118-
).Contains(data.SearchPattern)
119-
) ||
120-
(
121-
Path.GetFileNameWithoutExtension(
122-
dataGridView1.Rows[i].Cells[0].Value.ToString()
123-
).Contains(data.SearchPattern) &&
124-
data.FileExtension == Path.GetExtension(dataGridView1.Rows[i].Cells[(int)NameAssociationCellIndex.Name].Value.ToString())
125-
)
126-
)
127-
{
128-
if (!Directory.Exists(data.TargetDirectory + Path.DirectorySeparatorChar))
129-
{
130-
// the target directory does not existing
131-
// maybe a broken name association or a network drive is not available
132-
// skip this and go on
133-
continue;
134-
}
114+
var processed = false;
135115

136-
if (File.Exists(
137-
data.TargetDirectory +
138-
Path.DirectorySeparatorChar +
139-
Path.GetFileName(dataGridView1.Rows[i].Cells[(int)NameAssociationCellIndex.Name].Value.ToString())
140-
)
141-
)
142-
{
143-
// delete the existing file before it gets replaced with the current
144-
// processed file
145-
File.Delete(
146-
data.TargetDirectory +
147-
Path.DirectorySeparatorChar +
148-
Path.GetFileName(dataGridView1.Rows[i].Cells[(int)NameAssociationCellIndex.Name].Value.ToString())
149-
);
150-
}
151-
152-
File.Move(
153-
dataGridView1.Rows[i].Cells[0].Value.ToString(),
154-
data.TargetDirectory + Path.DirectorySeparatorChar + Path.GetFileName(dataGridView1.Rows[i].Cells[(int)NameAssociationCellIndex.Name].Value.ToString())
155-
);
116+
foreach (var type in types)
117+
{
118+
string[] classParts = type.FullName.Split('.');
119+
var action = Activator.CreateInstance(Type.GetType(classParts[0] + "." + classParts[1] + "." + classParts[2]));
156120

157-
processedFileCount++;
158-
}
121+
AbstractAction abstractAction = (AbstractAction)action;
122+
abstractAction.SetCurrent(dataGridView1.Rows[i]);
123+
processed = abstractAction.ExecuteAction();
159124
}
160125

126+
if (processed) processedFileCount++;
127+
161128
backgroundWorker1.ReportProgress(100 * i / dataGridView1.RowCount);
162129
}
163130
}
@@ -169,7 +136,6 @@ void backgroundWorker1_ProgressChanged (object sender, ProgressChangedEventArgs
169136

170137
private void button1_Click(object sender, EventArgs e)
171138
{
172-
// load configuration
173139
this.nameAssociations = NameAssociations.LoadFromExistingConfigFile();
174140

175141
if (this.nameAssociations.Count == 0)

PatternFileMover/PatternFileMover.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
</ItemGroup>
5151
<ItemGroup>
5252
<Compile Include="Actions.cs" />
53+
<Compile Include="Action\IAction.cs" />
54+
<Compile Include="Action\Move.cs" />
5355
<Compile Include="Form2.cs">
5456
<SubType>Form</SubType>
5557
</Compile>

0 commit comments

Comments
 (0)