Skip to content
Merged

HW8 #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/com/urise/webapp/MainFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.urise.webapp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainFile {

public static List<File> getAllFiles(String filePath) {
Objects.requireNonNull(filePath, "The parameter 'filePath' must not be null.");
return getAllFiles(new File(filePath));
}

public static List<File> getAllFiles(File srcFile) {
Objects.requireNonNull(srcFile, "The parameter 'srcFile' must not be null.");
if (!srcFile.exists() || srcFile.isFile() || !srcFile.canRead()) {
throw new RuntimeException("IO error: " + srcFile.getAbsolutePath());
}
return doGetAllFiles(srcFile);
}

private static List<File> doGetAllFiles(File srcFile) {
List<File> fileList = new ArrayList<>();
doGetAllFilesRecursion(srcFile, fileList);
return fileList;
}

private static void doGetAllFilesRecursion(File srcFile, List<File> fileList) {
Objects.requireNonNull(srcFile, "The parameter 'srcFile' must not be null.");
if (!srcFile.exists() || srcFile.isFile() || !srcFile.canRead()) {
throw new RuntimeException("IO error: " + srcFile.getAbsolutePath());
}

File[] files = srcFile.listFiles();

if (Objects.isNull(files)) {
return;
}

for (File file : files) {
if (!file.exists()) {
continue;
}
if (file.isFile()) {
fileList.add(file);
} else {
doGetAllFilesRecursion(file, fileList);
}
}
}

public static void main(String[] args) {
File srcFile = new File(".//");
getAllFiles(srcFile).forEach(System.out::println);

System.out.println("---------------------------------------------------");

getAllFiles(".//").forEach(System.out::println);
}
}
87 changes: 87 additions & 0 deletions src/com/urise/webapp/ResumeTestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,93 @@
import java.time.LocalDate;

public class ResumeTestData {
public static Resume createResume(String uuid, String fullName) {
Resume r = new Resume(uuid, fullName);
fillContacts(r);
fillPersonalSection(r);
fillObjectiveSection(r);
fillAchievementSection(r);
fillQualificationSection(r);
fillExperienceSection(r);
fillEducationSection(r);
return r;
}

private static void fillContacts(Resume r) {
r.setContact(new Contact(ContactType.MOBILEPHONE, "+9-999-999-9999"));
r.setContact(new Contact(ContactType.WORKPHONE, "+7-777-777-7777"));
r.setContact(new Contact(ContactType.EMAIL, "[email protected]"));
r.setContact(new Contact(ContactType.GITHUB, "Your URL of GITHUB"));
r.setContact(new Contact(ContactType.LINKEDIN, "Your URL of LINKEDIN"));
r.setContact(new Contact(ContactType.SKYPE, "Your login of SKYPE"));
r.setContact(new Contact(ContactType.STACKOVERFLOW, "Your URL of STACKOVERFLOW"));
r.setContact(new Contact(ContactType.HOMEPAGE, "Your URL of HOMEPAGE"));
}

private static void fillPersonalSection(Resume r) {
Section section = new TextSection(SectionType.PERSONAL, "The text os personal section");
r.setSection(section);
}

private static void fillObjectiveSection(Resume r) {
Section section = new TextSection(SectionType.OBJECTIVE, "The text of objective section");
r.setSection(section);
}

private static void fillAchievementSection(Resume r) {
ListSection section = new ListSection(SectionType.ACHIEVEMENT);
section.addElement("The text of achievements 1");
section.addElement("The text of achievements 2");
section.addElement("The text of achievements 3");
r.setSection(section);
}

private static void fillQualificationSection(Resume r) {
ListSection section = new ListSection(SectionType.QUALIFICATIONS);
section.addElement("The text of qualification 1");
section.addElement("The text of qualification 2");
section.addElement("The text of qualification 3");
r.setSection(section);
}

private static void fillExperienceSection(Resume r) {
OrganizationSection section = new OrganizationSection(SectionType.EXPERIENCE);

Organization o = new Organization("Company 1");
o.setWebsite("https:\\company1.com\\");
o.addPeriod(new Period("Position 1", LocalDate.of(2000, 1, 1), LocalDate.of(2003, 3, 1)));
o.addPeriod(new Period("Position 2", LocalDate.of(2003, 3, 2), LocalDate.of(2005, 7, 1)));
section.addOrganization(o);

o = new Organization("Company 2");
o.setWebsite("https:\\company2.com\\");
o.addPeriod(new Period("Position 1", LocalDate.of(2005, 7, 2), LocalDate.of(2010, 3, 1)));
o.addPeriod(new Period("Position 2", LocalDate.of(2008, 3, 2)));
section.addOrganization(o);

r.setSection(section);
}

private static void fillEducationSection(Resume r) {
OrganizationSection section = new OrganizationSection(SectionType.EDUCATION);

Organization o = new Organization("Institute of higher education");
o.setWebsite("https:\\institute-of-higher-education.com\\");
Period p = new Period("Nameof faculty", LocalDate.of(1993, 9, 1), LocalDate.of(1998, 6, 1));
p.setDescription("Description of acquired knowledge / skills");
o.addPeriod(p);
section.addOrganization(o);

o = new Organization("Advanced training institute");
o.setWebsite("https:\\advanced-training-institute.com\\");
p = new Period("Profession name", LocalDate.of(2005, 8, 1), LocalDate.of(2006, 6, 1));
p.setDescription("Description of acquired knowledge / skills");
o.addPeriod(p);
section.addOrganization(o);

r.setSection(section);
}

public static void main(String[] args) {
Resume r1 = new Resume("Григорий Кислин");

Expand Down
5 changes: 5 additions & 0 deletions src/com/urise/webapp/exception/StorageException.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public StorageException(String message, String uuid) {
this.uuid = uuid;
}

public StorageException(String message, String uuid, Exception e) {
super(message, e);
this.uuid = uuid;
}

public String getUuid() {
return uuid;
}
Expand Down
9 changes: 9 additions & 0 deletions src/com/urise/webapp/model/resume/Resume.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class Resume implements Comparable<Resume> {
private final Map<ContactType, Contact> contacts = new EnumMap<>(ContactType.class);
private final Map<SectionType, Section> sections = new EnumMap<>(SectionType.class);

public static final uuidComparator UUID_COMPARATOR = new uuidComparator();

public Resume(String fullName) {
this(UUID.randomUUID().toString(), fullName);
}
Expand Down Expand Up @@ -99,4 +101,11 @@ public String toString() {
public int compareTo(Resume o) {
return uuid.compareTo(o.uuid);
}

private static final class uuidComparator implements Comparator<Resume> {
@Override
public int compare(Resume o1, Resume o2) {
return o1.uuid.compareTo(o2.uuid);
}
}
}
124 changes: 124 additions & 0 deletions src/com/urise/webapp/storage/AbstractFileStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.urise.webapp.storage;

import com.urise.webapp.exception.StorageException;
import com.urise.webapp.model.resume.Resume;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public abstract class AbstractFileStorage extends AbstractStorage<File> {
private final File directory;

public AbstractFileStorage(File directory) {
Objects.requireNonNull(directory, "The parameter 'directory' must not be null.");
if (!directory.isDirectory()) {
throw new IllegalArgumentException("'" + directory.getAbsolutePath() + "'" + " is not directory.");
}

if (!directory.canRead() || !directory.canWrite()) {
throw new IllegalArgumentException("'" + directory.getAbsolutePath() + "'" + " is not readable / writable.");
}
this.directory = directory;
}

public File getDirectory() {
return directory;
}

@Override
protected void doClear() {
File[] files = getAllFiles();

for (File file : files) {
doDelete(file);
}
}

@Override
protected void doDelete(File file) {
try {
file.delete();
} catch (Exception e) {
throw new StorageException("doDelete() error: " + file.getAbsolutePath(), "No uuid", e);
}
}

@Override
protected Resume doGet(File file) {
try {
return readFile(file);
} catch (IOException e) {
throw new StorageException("Resume read file error: " + file.getAbsolutePath(), "No uuid", e);
}
}

@Override
protected Resume[] doGetAll() {
File[] files = getAllFiles();
Resume[] resumes = new Resume[files.length];
for (int i = 0; i < resumes.length; i++) {
resumes[i] = doGet(files[i]);
}
return resumes;
}

@Override
protected List<Resume> doGetAllSorted() {
return Arrays.stream(getAll()).sorted(Resume.UUID_COMPARATOR)
.collect(Collectors.toList());
}

@Override
protected void doSave(File file, Resume r) {
Objects.requireNonNull(file, "The parameter 'file' must not be null!");
Objects.requireNonNull(r, "The parameter 'r' must not be null!");
try {
file.createNewFile();
doWrite(r, file);
} catch (IOException e) {
throw new StorageException("IO error", file.getName(), e);
}
}

@Override
protected int doSize() {
return getAllFiles().length;
}

@Override
protected void doUpdate(File file, Resume r) {
try {
doWrite(r, file);
} catch (IOException e) {
throw new StorageException("IO error", file.getName(), e);
}
}

@Override
protected File getSearchKey(String uuid) {
return new File(directory, uuid);
}

@Override
protected boolean isExist(File file) {
return file.exists();
}

private File[] getAllFiles() {
File[] files;
try {
files = directory.listFiles(File::isFile);
} catch (Exception e) {
throw new StorageException("I/O error: getAllFiles()", "No uuid", e);
}
return files;
}

protected abstract void doWrite(Resume r, File file) throws IOException;

protected abstract Resume readFile(File file) throws IOException;
}
9 changes: 5 additions & 4 deletions test/com/urise/webapp/storage/AbstractStorageTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.urise.webapp.storage;

import com.urise.webapp.ResumeTestData;
import com.urise.webapp.exception.ExistStorageException;
import com.urise.webapp.exception.NotExistStorageException;
import com.urise.webapp.model.resume.Resume;
Expand All @@ -13,14 +14,14 @@
public abstract class AbstractStorageTest<T extends Storage> {
protected final T storage;
protected static final String UUID_1 = "uuid1";
protected static final Resume RESUME_UUID_1 = new Resume(UUID_1, "FullName1");
protected static final Resume RESUME_UUID_1 = ResumeTestData.createResume(UUID_1, "FullName1");
protected static final String UUID_3 = "uuid3";
protected static final Resume RESUME_UUID_3 = new Resume(UUID_3, "FullName3");
protected static final Resume RESUME_UUID_3 = ResumeTestData.createResume(UUID_3, "FullName3");
protected static final String UUID_5 = "uuid5";
protected static final Resume RESUME_UUID_5 = new Resume(UUID_5, "FullName5");
protected static final Resume RESUME_UUID_5 = ResumeTestData.createResume(UUID_5, "FullName5");

protected static final String UUID_7 = "uuid7";
protected static final Resume RESUME_UUID_7 = new Resume(UUID_7, "FullName3");
protected static final Resume RESUME_UUID_7 = ResumeTestData.createResume(UUID_7, "FullName3");
protected static final String UUID_NOT_EXIST = "uuidNotExist";

public AbstractStorageTest(T storage) {
Expand Down