Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/ru/javawebinar/basejava/model/Organization.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
@XmlAccessorType(XmlAccessType.FIELD)
public class Organization implements Serializable {

public static final Organization EMPTY = new Organization("", "", Position.EMPTY);

private static final long serialVersionUID = 1L;

private Link homePage;
Expand Down Expand Up @@ -73,6 +75,8 @@ public String toString() {
@XmlAccessorType(XmlAccessType.FIELD)
public static class Position implements Serializable {

public static final Position EMPTY = new Position();

@XmlJavaTypeAdapter(LocalDateAdapter.class)
private LocalDate startDate;
@XmlJavaTypeAdapter(LocalDateAdapter.class)
Expand Down
17 changes: 12 additions & 5 deletions src/ru/javawebinar/basejava/model/Resume.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
* Initial resume class
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Resume implements Comparable<Resume>, Serializable {

private static final long serialVersionUID = 1L;
public static final Resume EMPTY = new Resume();

static {
EMPTY.setSection(SectionType.OBJECTIVE, SectionLine.EMPTY);
EMPTY.setSection(SectionType.PERSONAL, SectionLine.EMPTY);
EMPTY.setSection(SectionType.ACHIEVEMENT, SectionList.EMPTY);
EMPTY.setSection(SectionType.QUALIFICATION, SectionList.EMPTY);
EMPTY.setSection(SectionType.EXPERIENCE, new SectionOrganization(Organization.EMPTY));
EMPTY.setSection(SectionType.EDUCATION, new SectionOrganization(Organization.EMPTY));
}

private String uuid;
private String fullName;
Expand Down Expand Up @@ -65,11 +72,11 @@ public Section getSection(SectionType type) {
return sections.get(type);
}

public void addContact(ContactType type, String value) {
public void setContact(ContactType type, String value) {
contacts.put(type, value);
}

public void addSection(SectionType type, Section section) {
public void setSection(SectionType type, Section section) {
sections.put(type, section);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ru/javawebinar/basejava/model/SectionLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class SectionLine extends Section {

private static final long serialVersionUID = 1L;

public static final SectionLine EMPTY = new SectionLine("");
private String content;

public SectionLine() {
Expand Down
2 changes: 1 addition & 1 deletion src/ru/javawebinar/basejava/model/SectionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class SectionList extends Section {

private static final long serialVersionUID = 1L;

public static final SectionList EMPTY = new SectionList("");
private List<String> content;

public SectionList() {
Expand Down
2 changes: 1 addition & 1 deletion src/ru/javawebinar/basejava/model/SectionType.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ru.javawebinar.basejava.model;

public enum SectionType {
PERSONAL("Personal"),
OBJECTIVE("Objective"),
PERSONAL("Personal"),
ACHIEVEMENT("Achievement"),
QUALIFICATION("Qualification"),
EXPERIENCE("Experience"),
Expand Down
4 changes: 2 additions & 2 deletions src/ru/javawebinar/basejava/storage/SqlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@ private void insertSections(Connection conn, Resume r) throws SQLException {
private void addContact(ResultSet rs, Resume r) throws SQLException {
String value = rs.getString("value");
if (value != null) {
r.addContact(ContactType.valueOf(rs.getString("type")), value);
r.setContact(ContactType.valueOf(rs.getString("type")), value);
}
}

private void addSection(ResultSet rs, Resume r) throws SQLException {
String value = rs.getString("value");
if (value != null) {
SectionType type = SectionType.valueOf(rs.getString("type"));
r.addSection(type, JsonParser.read(value, Section.class));
r.setSection(type, JsonParser.read(value, Section.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public Resume doRead(InputStream is) throws IOException {
String uuid = dis.readUTF();
String fullName = dis.readUTF();
Resume resume = new Resume(uuid, fullName);
readItems(dis, () -> resume.addContact(ContactType.valueOf(dis.readUTF()), dis.readUTF()));
readItems(dis, () -> resume.setContact(ContactType.valueOf(dis.readUTF()), dis.readUTF()));
readItems(dis, () -> {
SectionType sectionType = SectionType.valueOf(dis.readUTF());
resume.addSection(sectionType, readSection(dis, sectionType));
resume.setSection(sectionType, readSection(dis, sectionType));
});
return resume;
}
Expand Down
19 changes: 19 additions & 0 deletions src/ru/javawebinar/basejava/util/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@

import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class DateUtil {

public static final LocalDate NOW = LocalDate.of(3000, 1, 1);
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("MM/yyyy");

public static LocalDate of(int year, Month month) {
return LocalDate.of(year, month, 1);
}

public static String format(LocalDate date) {
if (date == null) {
return "";
}
return date.equals(NOW) ? "Nowadays" : date.format(DATE_FORMATTER);
}

public static LocalDate parse(String date) {
if (HtmlUtil.isEmpty(date) || date.equals("Nowadays")) {
return NOW;
}
YearMonth yearMonth = YearMonth.parse(date, DATE_FORMATTER);
return LocalDate.of(yearMonth.getYear(), yearMonth.getMonth(), 1);
}
}
16 changes: 16 additions & 0 deletions src/ru/javawebinar/basejava/util/HtmlUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.javawebinar.basejava.util;

import ru.javawebinar.basejava.model.Organization;

public class HtmlUtil {

public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}

public static String formatDates(Organization.Position position) {
return DateUtil.format(position.getStartDate()) + " - " + DateUtil.format(
position.getEndDate());
}

}
117 changes: 107 additions & 10 deletions src/ru/javawebinar/basejava/web/ResumeServlet.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package ru.javawebinar.basejava.web;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import ru.javawebinar.basejava.Config;
import ru.javawebinar.basejava.model.ContactType;
import ru.javawebinar.basejava.model.Link;
import ru.javawebinar.basejava.model.Organization;
import ru.javawebinar.basejava.model.Resume;
import ru.javawebinar.basejava.model.Section;
import ru.javawebinar.basejava.model.SectionLine;
import ru.javawebinar.basejava.model.SectionList;
import ru.javawebinar.basejava.model.SectionOrganization;
import ru.javawebinar.basejava.model.SectionType;
import ru.javawebinar.basejava.storage.Storage;
import ru.javawebinar.basejava.util.DateUtil;
import ru.javawebinar.basejava.util.HtmlUtil;

public class ResumeServlet extends HttpServlet {

private final Storage storage = Config.get().getStorage();


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Expand All @@ -23,24 +33,59 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(request, response);
return;
}
Resume resume = null;
Resume resume;
switch (action) {
case "delete":
storage.delete(uuid);
response.sendRedirect("resume");
return;
case "view":
resume = storage.get(uuid);
break;
case "edit":
resume = storage.get(uuid);
for (SectionType type : SectionType.values()) {
Section section = resume.getSection(type);
switch (type) {
case OBJECTIVE:
case PERSONAL:
if (section == null) {
section = SectionLine.EMPTY;
}
break;
case ACHIEVEMENT:
case QUALIFICATION:
if (section == null) {
section = SectionList.EMPTY;
}
break;
case EXPERIENCE:
case EDUCATION:
SectionOrganization orgSection = (SectionOrganization) section;
List<Organization> emptyFirstOrganizations = new ArrayList<>();
emptyFirstOrganizations.add(Organization.EMPTY);
if (orgSection != null) {
for (Organization org : orgSection.getOrganizations()) {
List<Organization.Position> emptyFirstPositions = new ArrayList<>();
emptyFirstPositions.add(Organization.Position.EMPTY);
emptyFirstPositions.addAll(org.getPositions());
emptyFirstOrganizations.add(
new Organization(org.getHomePage(), emptyFirstPositions));
}
}
section = new SectionOrganization(emptyFirstOrganizations);
break;
}
resume.setSection(type, section);
}
break;
default:
throw new IllegalStateException("Action " + action + " is illegal!");
throw new IllegalArgumentException("Action " + action + " is illegal");
}
request.setAttribute("resume", resume);
request.getRequestDispatcher(
("view".equals(action) ? "/WEB-INF/jsp/view.jsp" : "/WEB-INF/jsp/edit.jsp")
).forward(request, response);

}

@Override
Expand All @@ -49,17 +94,69 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
request.setCharacterEncoding("UTF-8");
String uuid = request.getParameter("uuid");
String fullName = request.getParameter("fullName");
Resume resume = storage.get(uuid);
resume.setFullName(fullName);
Resume resume;
final boolean isNotCreate = (uuid == null || uuid.length() == 0);
if (isNotCreate) {
resume = new Resume(fullName);
} else {
resume = storage.get(uuid);
resume.setFullName(fullName);
}
for (ContactType type : ContactType.values()) {
String value = request.getParameter(type.name());
if (value != null && value.trim().length() != 0) {
resume.addContact(type, value);
} else {
if (HtmlUtil.isEmpty(value)) {
resume.getContacts().remove(type);
} else {
resume.setContact(type, value);
}
}
for (SectionType type : SectionType.values()) {
String value = request.getParameter(type.name());
String[] values = request.getParameterValues(type.name());
if (HtmlUtil.isEmpty(value) && values.length < 2) {
resume.getSections().remove(type);
} else {
switch (type) {
case OBJECTIVE:
case PERSONAL:
resume.setSection(type, new SectionLine(value));
break;
case ACHIEVEMENT:
case QUALIFICATION:
resume.setSection(type, new SectionList(value.split("\\n")));
break;
case EDUCATION:
case EXPERIENCE:
List<Organization> organizations = new ArrayList<>();
String[] urls = request.getParameterValues(type.name() + "url");
for (int i = 0; i < values.length; i++) {
String name = values[i];
if (!HtmlUtil.isEmpty(name)) {
List<Organization.Position> positions = new ArrayList<>();
String prefix = type.name() + i;
String[] startDates = request.getParameterValues(prefix + "startDate");
String[] endDates = request.getParameterValues(prefix + "endDate");
String[] titles = request.getParameterValues(prefix + "title");
String[] descriptions = request.getParameterValues(prefix + "description");
for (int j = 0; j < titles.length; j++) {
if (!HtmlUtil.isEmpty(titles[j])) {
positions.add(new Organization.Position(DateUtil.parse(startDates[j]),
DateUtil.parse(endDates[j]), titles[j], descriptions[j]));
}
}
organizations.add(new Organization(new Link(name, urls[i]), positions));
}
}
resume.setSection(type, new SectionOrganization(organizations));
break;
}
}
}
storage.update(resume);
if (isNotCreate) {
storage.save(resume);
} else {
storage.update(resume);
}
response.sendRedirect("resume");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<resume>
<uuid>357d7d32-d69d-4bc8-bc6d-e1423544d4d6</uuid>
<uuid>23b0b02e-9e06-484f-bd89-ea4161410f11</uuid>
<fullName>Name3</fullName>
<contacts/>
<sections/>
Expand Down
34 changes: 34 additions & 0 deletions storage/34f56565-f314-43d4-967d-7e6f4791a952
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<resume>
<uuid>34f56565-f314-43d4-967d-7e6f4791a952</uuid>
<fullName>Name2</fullName>
<contacts>
<entry>
<key>MOBILE_PHONE</key>
<value>1234567</value>
</entry>
<entry>
<key>SKYPE</key>
<value>@SecondMember</value>
</entry>
</contacts>
<sections>
<entry>
<key>EXPERIENCE</key>
<value xsi:type="sectionOrganization" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<organizations>
<homePage>
<name>Organization21</name>
<url>http://organization21.ru</url>
</homePage>
<positions>
<startDate>2018-07-01</startDate>
<endDate>3000-01-01</endDate>
<title>position21</title>
<description>content21</description>
</positions>
</organizations>
</value>
</entry>
</sections>
</resume>
Loading